]> git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity/entity.js
ES6ifies serpentity
[rbdr/serpentity] / lib / serpentity / entity.js
1 'use strict';
2
3 /* global Serpentity */
4
5 /*
6 * The entity gives the entity framework its name. It exists only
7 * to hold components.
8 */
9
10 let Entity = class Entity {
11 constructor (config) {
12 this._componentKeys = [];
13 this._components = [];
14
15 Object.assign(this, config || {});
16 }
17
18 /*
19 * Adds a component to the entity.
20 *
21 * returns true if added, false if already present
22 */
23 addComponent (component) {
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 if (this._componentKeys.indexOf(componentClass) >= 0) {
37 return true;
38 }
39 return false;
40 }
41
42 /*
43 * returns the component associated with that key
44 */
45 getComponent (componentClass) {
46 let position;
47 position = this._componentKeys.indexOf(componentClass);
48 if (position >= 0) {
49 return this._components[position];
50 }
51 }
52 };
53
54 if (typeof module !== 'undefined' && this.module !== module) {
55 module.exports = Entity;
56 } else {
57 Serpentity.Entity = Entity;
58 }