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 data = {}; engine.server.readData = async (path) => { if (path in data) { return data[path]; } const error = new Error(); error.code = 'ENOENT'; throw error; }; engine.server.writeData = async (path, view) => { data[path] = view; }; // Connect an entity. await engine.connectPlayer(0, 0); const ecs = engine.ecses['homesteads/0']; // Create an entity. const entity = ecs.get(ecs.create({ Momentum: {x: 1, y: 0}, Position: {x: (RESOLUTION.x * 1.5) + 32 - 3, y: 20}, VisibleAabb: {}, })); // Tick and get update. Should be a full update. engine.tick(1); expect(engine.updateFor(0)) .to.deep.include({2: {MainEntity: {}, ...ecs.get(2).toJSON()}, 3: ecs.get(3).toJSON()}); // Tick and get update. Should be a partial update. engine.tick(1); expect(engine.updateFor(0)) .to.deep.include({ 3: { Position: {x: (RESOLUTION.x * 1.5) + 32 - 1}, VisibleAabb: { x0: 1199, x1: 1263, }, }, }); // Tick and get update. Should remove the entity. engine.tick(1); expect(engine.updateFor(0)) .to.deep.include({3: 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(0)) .to.deep.include({3: ecs.get(3).toJSON()}); });