70 lines
1.9 KiB
JavaScript
70 lines
1.9 KiB
JavaScript
import {expect, test} from 'vitest';
|
|
|
|
import {RESOLUTION} from '@/constants.js'
|
|
import Server from '@/net/server/server.js';
|
|
|
|
import Engine from './engine.js';
|
|
|
|
class TestServer extends Server {
|
|
constructor() {
|
|
super();
|
|
this.data = {};
|
|
}
|
|
async readAsset() {
|
|
return new ArrayBuffer(0);
|
|
}
|
|
async readData(path) {
|
|
if (path in this.data) {
|
|
return this.data[path];
|
|
}
|
|
const error = new Error();
|
|
error.code = 'ENOENT';
|
|
throw error;
|
|
}
|
|
async writeData(path, view) {
|
|
this.data[path] = view;
|
|
}
|
|
}
|
|
|
|
test('visibility-based updates', async () => {
|
|
const engine = new Engine(TestServer);
|
|
// Connect an entity.
|
|
await engine.connectPlayer(0, 0);
|
|
const ecs = engine.ecses['homesteads/0'];
|
|
// Create an entity.
|
|
const entity = ecs.get(await ecs.create({
|
|
Forces: {forceX: 1},
|
|
Position: {x: (RESOLUTION.x * 1.5) + 32 - 3, y: 20},
|
|
VisibleAabb: {},
|
|
}));
|
|
const {entity: mainEntity} = engine.connectedPlayers.get(0);
|
|
// Tick and get update. Should be a full update.
|
|
engine.tick(1);
|
|
expect(engine.updateFor(0))
|
|
.to.deep.include({[mainEntity.id]: {MainEntity: {}, ...ecs.get(mainEntity.id).toJSON()}, [entity.id]: ecs.get(entity.id).toJSON()});
|
|
engine.setClean();
|
|
// Tick and get update. Should be a partial update.
|
|
engine.tick(1);
|
|
expect(engine.updateFor(0))
|
|
.to.deep.include({
|
|
[entity.id]: {
|
|
Position: {x: (RESOLUTION.x * 1.5) + 32 - 1},
|
|
VisibleAabb: {
|
|
x0: 1199,
|
|
x1: 1263,
|
|
},
|
|
},
|
|
});
|
|
engine.setClean();
|
|
// Tick and get update. Should remove the entity.
|
|
engine.tick(1);
|
|
expect(engine.updateFor(0))
|
|
.to.deep.include({[entity.id]: false});
|
|
// Aim back toward visible area and tick. Should be a full update for that entity.
|
|
engine.setClean();
|
|
entity.Forces.forceX = -1;
|
|
engine.tick(1);
|
|
expect(engine.updateFor(0))
|
|
.to.deep.include({[entity.id]: ecs.get(entity.id).toJSON()});
|
|
});
|