-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitRegistry.ts
49 lines (41 loc) · 1.18 KB
/
UnitRegistry.ts
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
import {
IEntityRegistry,
EntityRegistry,
} from '@civ-clone/core-registry/EntityRegistry';
import City from '@civ-clone/core-city/City';
import Player from '@civ-clone/core-player/Player';
import Tile from '@civ-clone/core-world/Tile';
import Unit from './Unit';
export interface IUnitRegistry extends IEntityRegistry<Unit> {
getByCity(city: City): Unit[];
getByPlayer(player: Player, includeDestroyed?: boolean): Unit[];
getByTile(tile: Tile): Unit[];
}
export class UnitRegistry
extends EntityRegistry<Unit>
implements IUnitRegistry
{
constructor() {
super(Unit);
}
getByCity(city: City): Unit[] {
return this.filter(
(unit: Unit) => unit.city() === city && !unit.destroyed()
);
}
getByPlayer(player: Player, includeDestroyed: boolean = false): Unit[] {
if (includeDestroyed) {
return this.getBy('player', player);
}
return this.filter(
(unit: Unit) => unit.player() === player && !unit.destroyed()
);
}
getByTile(tile: Tile): Unit[] {
return this.filter(
(unit: Unit) => unit.tile() === tile && !unit.destroyed()
);
}
}
export const instance: UnitRegistry = new UnitRegistry();
export default UnitRegistry;