silphius/app/engine.test.js

63 lines
1.6 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';
2024-06-14 15:18:55 -05:00
2024-06-10 22:42:30 -05:00
import Engine from './engine.js';
2024-06-15 20:59:11 -05:00
class TestServer extends Server {
constructor() {
super();
this.data = {};
}
async readData(path) {
if (path in this.data) {
return this.data[path];
2024-06-14 15:18:55 -05:00
}
const error = new Error();
error.code = 'ENOENT';
throw error;
2024-06-15 20:59:11 -05:00
}
async writeData(path, view) {
this.data[path] = view;
}
}
test('visibility-based updates', async () => {
const engine = new Engine(TestServer);
2024-06-14 15:18:55 -05:00
// Connect an entity.
await engine.connectPlayer(0, 0);
const ecs = engine.ecses['homesteads/0'];
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: {},
}));
// Tick and get update. Should be a full update.
engine.tick(1);
2024-06-14 15:18:55 -05:00
expect(engine.updateFor(0))
.to.deep.include({2: {MainEntity: {}, ...ecs.get(2).toJSON()}, 3: ecs.get(3).toJSON()});
2024-06-10 22:42:30 -05:00
// Tick and get update. Should be a partial update.
engine.tick(1);
2024-06-14 15:18:55 -05:00
expect(engine.updateFor(0))
2024-06-13 23:51:06 -05:00
.to.deep.include({
2024-06-14 15:18:55 -05:00
3: {
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);
2024-06-14 15:18:55 -05:00
expect(engine.updateFor(0))
.to.deep.include({3: 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);
2024-06-14 15:18:55 -05:00
expect(engine.updateFor(0))
.to.deep.include({3: ecs.get(3).toJSON()});
2024-06-10 22:42:30 -05:00
});