Newer
Older
rpg / packages / engine / src / ecs / __tests__ / ecs.test.ts
import { describe, it, expect } from 'vitest';
import { World, type System } from '../ecs';

describe('ECS', () => {
    it('создание, компоненты, запросы', () => {
        const world = new World();
        const a = world.createEntity();
        const b = world.createEntity();

        world.addComponent(a, 'pos', { x: 1, y: 2 });
        world.addComponent(a, 'hp', { value: 10 });
        world.addComponent(b, 'pos', { x: 0, y: 0 });

        expect(world.query('pos')).toHaveLength(2);
        expect(world.query('pos', 'hp')).toEqual([a]);
        expect(world.getComponent<{ value: number }>(a, 'hp')!.value).toBe(10);

        world.destroyEntity(a);
        expect(world.isAlive(a)).toBe(false);
        expect(world.query('pos')).toEqual([b]);
    });

    it('системы обновляются по порядку', () => {
        const world = new World();
        const log: string[] = [];
        const makeSys = (name: string): System => ({
            update(_w, _dt) {
                log.push(name);
            }
        });
        world.addSystem(makeSys('first'));
        world.addSystem(makeSys('second'));
        world.update(1 / 60);
        expect(log).toEqual(['first', 'second']);
    });
});