44 lines
1.3 KiB
JavaScript
44 lines
1.3 KiB
JavaScript
|
import {expect, test} from 'vitest';
|
||
|
|
||
|
import {RESOLUTION} from '@/constants.js'
|
||
|
import Server from '@/net/server/server.js';
|
||
|
import Engine from './engine.js';
|
||
|
|
||
|
test('visibility-based updates', async () => {
|
||
|
const engine = new Engine(Server);
|
||
|
const ecs = engine.ecses[0];
|
||
|
// Create an entity.
|
||
|
const entity = ecs.get(ecs.create({
|
||
|
Momentum: {x: 1, y: 0},
|
||
|
Position: {x: (RESOLUTION[0] / 2) + 32 - 3, y: 20},
|
||
|
VisibleAabb: {},
|
||
|
}));
|
||
|
// Connect an entity.
|
||
|
await engine.connectPlayer(undefined);
|
||
|
// Tick and get update. Should be a full update.
|
||
|
engine.tick(1);
|
||
|
expect(engine.updateFor(undefined))
|
||
|
.to.deep.equal({2: ecs.get(2).toJSON(), 3: ecs.get(3).toJSON()});
|
||
|
// Tick and get update. Should be a partial update.
|
||
|
engine.tick(1);
|
||
|
expect(engine.updateFor(undefined))
|
||
|
.to.deep.equal({
|
||
|
2: {
|
||
|
Position: {x: (RESOLUTION[0] / 2) + 32 - 1},
|
||
|
VisibleAabb: {
|
||
|
x0: 399,
|
||
|
x1: 463,
|
||
|
},
|
||
|
},
|
||
|
});
|
||
|
// Tick and get update. Should remove the entity.
|
||
|
engine.tick(1);
|
||
|
expect(engine.updateFor(undefined))
|
||
|
.to.deep.equal({2: false});
|
||
|
// Aim back toward visible area and tick. Should be a full update for that entity.
|
||
|
entity.Momentum.x = -1;
|
||
|
engine.tick(1);
|
||
|
expect(engine.updateFor(undefined))
|
||
|
.to.deep.equal({2: ecs.get(2).toJSON()});
|
||
|
});
|