silphius/app/engine/engine.test.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-06-10 22:42:30 -05:00
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);
2024-06-11 00:08:31 -05:00
const ecs = engine.ecses[1];
2024-06-10 22:42:30 -05:00
// Create an entity.
const entity = ecs.get(ecs.create({
Momentum: {x: 1, y: 0},
2024-06-11 19:45:37 -05:00
Position: {x: (RESOLUTION.x * 1.5) + 32 - 3, y: 20},
2024-06-10 22:42:30 -05:00
VisibleAabb: {},
}));
// Connect an entity.
await engine.connectPlayer(undefined);
// Tick and get update. Should be a full update.
engine.tick(1);
expect(engine.updateFor(undefined))
2024-06-12 13:19:16 -05:00
.to.deep.include({2: ecs.get(2).toJSON(), 3: {MainEntity: {}, ...ecs.get(3).toJSON()}});
2024-06-10 22:42:30 -05:00
// Tick and get update. Should be a partial update.
engine.tick(1);
expect(engine.updateFor(undefined))
2024-06-13 23:51:06 -05:00
.to.deep.include({
2024-06-10 22:42:30 -05:00
2: {
2024-06-11 19:45:37 -05:00
Position: {x: (RESOLUTION.x * 1.5) + 32 - 1},
2024-06-10 22:42:30 -05:00
VisibleAabb: {
2024-06-11 19:45:37 -05:00
x0: 1199,
x1: 1263,
2024-06-10 22:42:30 -05:00
},
},
});
// Tick and get update. Should remove the entity.
engine.tick(1);
expect(engine.updateFor(undefined))
2024-06-13 23:51:06 -05:00
.to.deep.include({2: false});
2024-06-10 22:42:30 -05:00
// 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))
2024-06-13 23:51:06 -05:00
.to.deep.include({2: ecs.get(2).toJSON()});
2024-06-10 22:42:30 -05:00
});