All files / patterns/src/ecs entity.ts

100% Statements 49/49
100% Branches 9/9
100% Functions 8/8
100% Lines 49/49

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 42 43 44 45 46 47 48 49 501x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 1x 1x 9x 9x 1x 1x 5x 5x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { ComponentInterface } from "./component";
 
export interface EntityInterface {
    getId(): string;
    getComponent(name: string): ComponentInterface | undefined;
    getComponents(): Map<string, ComponentInterface>;
    addComponent(component: ComponentInterface): void;
    removeComponent(component: ComponentInterface): void;
    hasComponent(name: string): boolean;
}
 
export class Entity implements EntityInterface {
    protected id: string;
 
    protected components: Map<string, ComponentInterface>;
 
    public constructor(id: string) {
        this.id = id;
        this.components = new Map<string, ComponentInterface>();
    }
 
    public getId(): string {
        return this.id;
    }
 
    public getComponents(): Map<string, ComponentInterface> {
        return this.components;
    }
 
    public getComponent(name: string): ComponentInterface | undefined {
        return this.components.get(name);
    }
 
    public addComponent(component: ComponentInterface): Map<string, ComponentInterface> {
        return this.components.set(component.getName(), component);
    }
 
    public removeComponent(component: ComponentInterface): boolean {
        if (this.components.has(component.getName())) {
            return this.components.delete(component.getName());
        }
 
        return true;
    }
 
    public hasComponent(name: string): boolean {
        return this.components.has(name);
    }
}