]> git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity/entity.js
add823e422fdf1a831ff40759e5dcaa686f6b14d
[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 = this._componentKeys.indexOf(componentClass);
47 if (position >= 0) {
48 return this._components[position];
49 }
50 }
51 };
52
53 if (typeof module !== 'undefined' && this.module !== module) {
54 module.exports = Entity;
55 } else {
56 Serpentity.Entity = Entity;
57 }