]> git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity/entity.js
c020f05514c0c8940a0d1079dacdb8d669fd2ee0
[rbdr/serpentity] / lib / serpentity / entity.js
1 /*
2 * The entity gives the entity framework its name. It exists only
3 * to hold components.
4 */
5 Class(Serpentity, "Entity")({
6 prototype : {
7 _components : null,
8 _componentKeys : null,
9
10 init : function init(config) {
11 var property;
12
13 this._componentKeys = [];
14 this._components = [];
15
16 for (property in config) {
17 if (config.hasOwnProperty(property)) {
18 this[property] = config[property];
19 }
20 }
21 },
22
23 /*
24 * Adds a component to the entity.
25 *
26 * returns true if added, false if already present
27 */
28 addComponent : function addComponent(component) {
29 if (this._componentKeys.indexOf(component.constructor) >= 0) {
30 return false;
31 }
32 this._componentKeys.push(component.constructor);
33 this._components.push(component);
34 return true;
35 },
36
37 /*
38 * returns true if component is included, false otherwise
39 */
40 hasComponent : function hasComponent(componentClass) {
41 if (this._componentKeys.indexOf(componentClass) >= 0) {
42 return true;
43 }
44 return false;
45 },
46
47 /*
48 * returns the component associated with that key
49 */
50 getComponent : function getComponent(componentClass) {
51 var position;
52 position = this._componentKeys.indexOf(componentClass);
53 if (position >= 0) {
54 return this._components[position];
55 }
56 }
57 }
58 });