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 | 16x 16x 16x 16x 16x 16x 16x 23x 23x 23x 23x 23x 23x 23x 7x 7x 2x 1x 1x 2x 7x 2x 2x 2x 1x 1x 3x 17x 1x 1x 1x 1x 4x 3x 3x 4x 1x 1x 1x 2x 2x 2x 2x 2x 2x 4x 2x 2x 2x 2x 2x 16x | import { uuid } from "jga-generators-uuid";
import { random } from "jga-algorithms-random";
import { WorldInterface, SystemInterface } 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 implements WorldInterface {
protected entities: Map<string, Entity>;
protected systems: Set<SystemInterface>;
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,
) {
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)) {
(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;
}
protected generateId(): string {
let id: string;
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 entityPosition = entity.getComponent(ComponentName.POSITION) as PositionComponent;
return position === entityPosition.getPosition();
});
return targets.length === 0;
}
}
|