All files / libs/games/src/engine world.ts

96.66% Statements 58/60
72.72% Branches 8/11
96.15% Functions 25/26
96.61% Lines 57/59

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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 17750x 50x           50x   50x 50x     50x                                 58x 58x 58x 58x 58x 58x 58x       55x   55x       2x 1x 1x     2x       1x   1x   1x       15x       18x   18x   18x       1x       3x 4x       39x       35x       6x       2x 3x         5x       50x 49x 49x 49x     49x     50x       2x       9x       4x               2x 2x 2x 2x     2x       2x       4x       1x           19x 19x     19x       2x           2x      
import { random } from "@jga/algorithms";
import { uuid } from "@jga/generators";
import { Command, CommandManager, CommandResultInterface, WorldInterface, SystemInterface } from "@jga/patterns";
 
import { GameMap, PositionInterface } from "../maps/index";
 
import { Scheduler } from "./scheduler";
import { Loop } from "./loop";
 
import { Entity, EntityFactoryInterface, Player } from "./entities";
import { isPositionComponent, ComponentName } from "./components";
import { System } from "./systems";
 
export class World implements WorldInterface {
    protected entities: Map<string, Entity>;
    protected systems: Set<SystemInterface>;
    private player!: Player;
    private readonly scheduler: Scheduler;
    private readonly map: GameMap;
    private readonly loop: Loop;
    private readonly commandManager: CommandManager;
    private readonly entityFactory: EntityFactoryInterface;
 
    // eslint-disable-next-line max-params
    public constructor(
        scheduler: Scheduler,
        map: GameMap,
        commandManager: CommandManager,
        factory: EntityFactoryInterface,
    ) {
        this.entities = new Map<string, Entity>();
        this.systems = new Set<SystemInterface>();
        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, Entity> {
        this.scheduler.add(entity, repeatable);
 
        return this.entities.set(entity.getId(), entity);
    }
 
    public removeEntity(entity: Entity): Map<string, Entity> {
        if (this.entities.has(entity.getId())) {
            this.scheduler.remove(entity);
            this.entities.delete(entity.getId());
        }
 
        return this.entities;
    }
 
    public createEntity(): Entity {
        const entity = new Entity(this.generateId());
 
        this.addEntity(entity);
 
        return entity;
    }
 
    public getEntities(): Map<string, Entity> {
        return this.entities;
    }
 
    public createPlayer(): Player {
        this.player = new Player(this.generateId());
 
        this.addEntityAtRandomPosition(this.player, true);
 
        return this.getPlayer();
    }
 
    public getSystems(): Set<SystemInterface> {
        return this.systems;
    }
 
    public registerSystems(systems: SystemInterface[]): void {
        this.systems.clear();
        systems.forEach((system) => this.systems.add(system));
    }
 
    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, Entity> {
        if (entity.hasComponent(ComponentName.POSITION)) {
            const component = entity.getComponent(ComponentName.POSITION);
            if (isPositionComponent(component)) {
                component.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;
 
        // eslint-disable-next-line no-loops/no-loops
        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;
    }
 
    protected generateId(): string {
        let id: string;
        // eslint-disable-next-line no-loops/no-loops
        do {
            id = uuid();
        } while (this.entities.has(id));
 
        return id;
    }
 
    private hasTargetAtPosition(position: PositionInterface): boolean {
        const targets: Entity[] = Array.from(this.entities.values()).filter((entity) => {
            const component = entity.getComponent(ComponentName.POSITION);
 
            return isPositionComponent(component) ? position === component.getPosition() : false;
        });
 
        return targets.length === 0;
    }
}