blob: b016fc07418c952ca247fbb7266faec8a3982207 (
plain)
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
|
import { EntityNames, Player } from '.';
import type { Component } from '../components';
export abstract class Entity {
public id: string;
public components: Map<string, Component>;
public name: string;
constructor(name: string, id: string = crypto.randomUUID()) {
this.name = name;
this.id = id;
this.components = new Map();
}
public addComponent(component: Component) {
this.components.set(component.name, component);
}
public getComponent<T extends Component>(name: string): T {
if (!this.hasComponent(name)) {
throw new Error('Entity does not have component ' + name);
}
return this.components.get(name) as T;
}
public getComponents(): Component[] {
return Array.from(this.components.values());
}
public hasComponent(name: string): boolean {
return this.components.has(name);
}
static from(entityName: string, args: any): Entity {
switch (entityName) {
case EntityNames.Player:
const player = new Player(args.playerId);
player.id = args.id;
return player;
default:
throw new Error('.from() Entity type not implemented: ' + entityName);
}
}
}
|