All files / games/engine/src world.ts

93.48% Statements 43/46
87.5% Branches 7/8
90% Functions 18/20
93.48% Lines 43/46

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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136  16x 16x 16x 16x 16x 16x 16x     23x 23x 23x 23x 23x 23x     7x 7x     2x 2x     2x 2x 2x     3x     17x     1x     1x 1x       1x     4x 3x 3x   4x     1x     1x     1x         2x 2x 2x 2x   2x     2x     4x           2x       2x     16x                                                                                          
import { random } from "jga-algorithms-random";
import { World as BaseWorld, EntityInterface } from "jga-patterns-ecs";
import { Command, CommandManager, CommandResultInterface } from "jga-patterns-command";
import { GameMap, PositionInterface } from "jga-games-maps";
 
import { Scheduler } from "./scheduler";
import { Loop } from "./loop";
 
import { Entity, EntityFactoryInterface, Player } from "./entities";
import { ComponentName, PositionComponent } from "./components";
import { System } from "./systems";
 
export class World extends BaseWorld {
    private player!: Player;
    private scheduler: Scheduler;
    private map: GameMap;
    private loop: Loop;
    private commandManager: CommandManager;
    private entityFactory: EntityFactoryInterface;
 
    public constructor(
        scheduler: Scheduler,
        map: GameMap,
        commandManager: CommandManager,
        factory: EntityFactoryInterface,
    ) {
        super();
        this.scheduler = scheduler;
        this.map = map;
        this.loop = new Loop(this.scheduler);
        this.commandManager = commandManager;
        this.entityFactory = factory;
    }
 
    public addEntity(entity: Entity, repeatable = false): Map<string, EntityInterface> {
        this.scheduler.add(entity, repeatable);
 
        return super.addEntity(entity);
    }
 
    public removeEntity(entity: Entity): Map<string, EntityInterface> {
        this.scheduler.remove(entity);
 
        return super.removeEntity(entity);
    }
 
    public createPlayer(): Player {
        this.player = new Player(this.generateId());
 
        this.addEntityAtRandomPosition(this.player, true);
 
        return this.getPlayer();
    }
 
    public getPlayer(): Player {
        return this.player;
    }
 
    public getMap(): GameMap {
        return this.map;
    }
 
    public execute(command: Command): CommandResultInterface {
        return this.commandManager.execute(command);
    }
 
    public process(): void {
        return this.systems.forEach((system): void => {
            system.run(this);
        });
    }

    public run(system: System): void {
        system.run(this);
    }
 
    public addEntityAtRandomPosition(entity: Entity, repeatable = false): Map<string, EntityInterface> {
        if (entity.hasComponent(ComponentName.POSITION)) {
            (entity.getComponent(ComponentName.POSITION) as PositionComponent).setPosition(
                this.getRandomFloorPosition(),
            );
 
            this.addEntity(entity, repeatable);
        }

        return this.entities;
    }
 
    public lock(): Loop {
        return this.loop.lock();
    }
 
    public unlock(): Loop {
        return this.loop.unlock();
    }
 
    public async mainLoop(): Promise<Loop> {
        return this.loop.start();
    }
 
    public getRandomFloorPosition(): PositionInterface {
        let x: number;
        let y: number;
 
        do {
            x = random(0, this.map.getWidth() - 1);
            y = random(0, this.map.getHeight() - 1);
            console.log(this.isNotEmptyFloor({ x, y }));
        } while (!this.isEmptyFloor({ x, y }));
 
        return this.hasTargetAtPosition({ x, y }) ? { x, y } : this.getRandomFloorPosition();
    }
 
    public isEmptyFloor(position: PositionInterface): boolean {
        return !this.isNotEmptyFloor(position);
    }
 
    private isNotEmptyFloor(position: PositionInterface): boolean {
        return null !== this.map.getTile(position) && !this.map.getTile(position).isWalkable;
    }
 
    public getEntityFactory(): EntityFactoryInterface {
        return this.entityFactory;
    }
 
    private hasTargetAtPosition(position: PositionInterface): boolean {
        const targets: EntityInterface[] = Array.from(this.entities.values()).filter((entity) => {
            const entityPosition = entity.getComponent(ComponentName.POSITION) as PositionComponent;
 
            return position === entityPosition.getPosition();
        });
 
        return targets.length === 0;
    }
}