All files / projects-dev/libs/games/src/engine world.ts

98.36% Statements 181/184
96.66% Branches 29/30
100% Functions 24/24
98.36% Lines 181/184

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 177 178 179 180 181 182 183 184 1851x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 60x 60x 60x 60x 65x 65x 6x 5x 5x 5x 6x 6x 6x 65x 65x 1x 1x 1x 1x 1x 1x 65x 65x 15x 15x 65x 65x 23x 23x 23x 23x 23x 23x 65x 65x 1x 1x 65x 65x 3x 3x 3x 65x 65x 44x 44x 65x 65x 35x 35x 65x 65x 6x 6x 65x 65x 2x 3x 2x 2x 65x 65x 5x 5x 65x 65x 55x 54x 54x 54x 54x 54x 54x 54x 55x 55x 55x 65x 65x 2x 2x 65x 65x 9x 9x 65x 65x 4x 4x 65x 65x 7x 7x 7x 7x 7x 16x 16x 7x 7x 7x 7x 65x 65x 16x 16x 65x 65x 16x 16x 65x 65x 1x 1x 65x 65x 24x 24x 24x 24x 24x 24x 24x 24x 65x 65x 7x 7x       7x 7x 7x 7x 65x  
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";
 
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,
    ) {
        // eslint-disable-next-line compat/compat
        this.entities = new Map<string, Entity>();
        // eslint-disable-next-line compat/compat
        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();
        for (const system of systems) 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: SystemInterface): 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);
        } 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 this.map.getTile(position) !== null && !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 {
        // eslint-disable-next-line compat/compat
        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;
    }
}