All files / games/engine/src engine.ts

100% Statements 15/15
100% Branches 0/0
100% Functions 5/5
100% Lines 15/15

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 4610x       10x   10x         5x 5x       6x   6x   6x       3x   3x   3x       4x           1x 4x   4x        
import { Engine as BaseEngine, EntityInterface } from "jga-patterns-ecs";
 
import { Scheduler } from "./scheduler";
 
import { Entity, Player } from "./entities";
 
export class Engine extends BaseEngine {
    private player!: Player;
    private scheduler: Scheduler;
 
    public constructor(scheduler: Scheduler) {
        super();
        this.scheduler = scheduler;
    }
 
    public addEntity(entity: Entity): Map<string, EntityInterface> {
        const entities = super.addEntity(entity);
 
        this.scheduler.add(entity, false);
 
        return entities;
    }
 
    public createPlayer(): Player {
        this.player = new Player(this.generateId());
 
        this.addEntity(this.player);
 
        return this.getPlayer();
    }
 
    public getPlayer(): Player {
        return this.player;
    }
 
    public async mainLoop(): Promise<void> {
        let actor: Entity | null;
 
        do {
            actor = this.scheduler.next() as Entity;
 
            await actor.act();
        } while (null != actor);
    }
}