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

100% Statements 13/13
100% Branches 6/6
100% Functions 5/5
100% Lines 13/13

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 41          11x 17x     17x       4x                 28x   28x 28x 28x 42x 25x       28x 19x       28x      
import { EntityInterface, SystemInterface } from "jga-patterns-ecs";
import { GameMap } from "jga-games-maps";
 
import { ComponentName } from "../components";
 
export abstract class System implements SystemInterface {
    protected entities: Map<string, EntityInterface> = new Map<string, EntityInterface>();
 
    public constructor(entities: Map<string, EntityInterface>, filters: ComponentName[]) {
        this.entities = this.filterEntities(entities, filters);
    }
 
    public getEntities(): Map<string, EntityInterface> {
        return this.entities;
    }
 
    public abstract run(map?: GameMap): void;
 
    protected filterEntities(
        entities: Map<string, EntityInterface>,
        filters: ComponentName[],
    ): Map<string, EntityInterface> {
        const filtered: Map<string, EntityInterface> = new Map<string, EntityInterface>();
 
        entities.forEach((entity, key): void => {
            let found = 0;
            filters.forEach((filter): void => {
                if (null != entity && entity.hasComponent(filter)) {
                    found++;
                }
            });
 
            if (found === filters.length) {
                filtered.set(key, entity);
            }
        });
 
        return filtered;
    }
}