All files / libs/games/src/engine/systems system.ts

100% Statements 40/40
100% Branches 8/8
100% Functions 4/4
100% Lines 40/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 411x 1x 1x 1x 1x 1x 44x 44x 44x 44x 44x 44x 44x 44x 4x 4x 44x 44x 44x 44x 44x 69x 69x 69x 69x 73x 73x 126x 92x 92x 126x 73x 73x 55x 55x 73x 69x 69x 69x 44x  
import { SystemInterface, WorldInterface } from "@jga/patterns";
 
import { ComponentName } from "../components";
import { Entity } from "../entities";
 
export abstract class System implements SystemInterface {
    // eslint-disable-next-line compat/compat
    protected entities: Map<string, Entity> = new Map<string, Entity>();
 
    public constructor(entities: Map<string, Entity>, filters: ComponentName[]) {
        this.entities = this.filterEntities(entities, filters);
    }
 
    public getEntities(): Map<string, Entity> {
        return this.entities;
    }
 
    public abstract run(world?: WorldInterface): void;
 
    // eslint-disable-next-line class-methods-use-this,compat/compat
    protected filterEntities(entities: Map<string, Entity>, filters: ComponentName[]): Map<string, Entity> {
        // eslint-disable-next-line compat/compat
        const filtered: Map<string, Entity> = new Map<string, Entity>();
 
        for (const [key, entity] of entities.entries()) {
            let found = 0;
            for (const filter of filters) {
                if (entity != undefined && entity.hasComponent(filter)) {
                    found += 1;
                }
            }
 
            if (found === filters.length) {
                filtered.set(key, entity);
            }
        }
 
        return filtered;
    }
}