]> git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity/entity.js
2a4b03fbc6d323677d6855025aff78ce159080ea
[rbdr/serpentity] / lib / serpentity / entity.js
1 'use strict';
2
3 /*
4 * The entity gives the entity framework its name. It exists only
5 * to hold components.
6 */
7
8 const Entity = class Entity {
9 constructor(config) {
10
11 this._componentKeys = [];
12 this._components = [];
13
14 Object.assign(this, config);
15 }
16
17 /*
18 * Adds a component to the entity.
19 *
20 * returns true if added, false if already present
21 */
22 addComponent(component) {
23
24 if (this._componentKeys.indexOf(component.constructor) >= 0) {
25 return false;
26 }
27 this._componentKeys.push(component.constructor);
28 this._components.push(component);
29 return true;
30 }
31
32 /*
33 * returns true if component is included, false otherwise
34 */
35 hasComponent(componentClass) {
36
37 if (this._componentKeys.indexOf(componentClass) >= 0) {
38 return true;
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 };
54
55 module.exports = Entity;