]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * The entity gives the entity framework its name. It exists only | |
3 | * to hold components. | |
4 | */ | |
5 | ||
6 | export class Entity { | |
7 | constructor(config) { | |
8 | ||
9 | this._componentKeys = []; | |
10 | this._components = []; | |
11 | ||
12 | Object.assign(this, config); | |
13 | } | |
14 | ||
15 | /* | |
16 | * Adds a component to the entity. | |
17 | * | |
18 | * returns true if added, false if already present | |
19 | */ | |
20 | addComponent(component) { | |
21 | ||
22 | if (this._componentKeys.indexOf(component.constructor) >= 0) { | |
23 | return false; | |
24 | } | |
25 | ||
26 | this._componentKeys.push(component.constructor); | |
27 | this._components.push(component); | |
28 | return true; | |
29 | } | |
30 | ||
31 | /* | |
32 | * returns true if component is included, false otherwise | |
33 | */ | |
34 | hasComponent(componentClass) { | |
35 | ||
36 | if (this._componentKeys.indexOf(componentClass) >= 0) { | |
37 | return true; | |
38 | } | |
39 | ||
40 | return false; | |
41 | } | |
42 | ||
43 | /* | |
44 | * returns the component associated with that key | |
45 | */ | |
46 | getComponent(componentClass) { | |
47 | ||
48 | const position = this._componentKeys.indexOf(componentClass); | |
49 | if (position >= 0) { | |
50 | return this._components[position]; | |
51 | } | |
52 | } | |
53 | } |