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