forked from qiao/ces.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.js
84 lines (74 loc) · 1.8 KB
/
entity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var Class = require('./class'),
Signal = require('./signal');
/**
* The entity is the container of components.
* @class
*/
var Entity = module.exports = Class.extend({
/**
* @constructor
*/
init: function () {
/**
* @public
* @readonly
*/
this.id = Entity._id++;
/**
* Map from component names to components.
* @private
* @property
*/
this._components = {};
/**
* @public
* @readonly
*/
this.onComponentAdded = new Signal();
/**
* @public
* @readonly
*/
this.onComponentRemoved = new Signal();
},
/**
* Check if this entity has a component by name.
* @public
* @param {String} componentName
* @return {Boolean}
*/
hasComponent: function (componentName) {
return this._components['$' + componentName] !== undefined;
},
/**
* Get a component of this entity by name.
* @public
* @param {String} componentName
* @return {Component}
*/
getComponent: function (componentName) {
return this._components['$' + componentName];
},
/**
* Add a component to this entity.
* @public
* @param {Component} component
*/
addComponent: function (component) {
this._components['$' + component.name] = component;
this.onComponentAdded.emit(this, component.name);
},
/**
* Remove a component from this entity by name.
* @public
* @param {String} componentName
*/
removeComponent: function (componentName) {
this._components['$' + componentName] = undefined;
this.onComponentRemoved.emit(this, componentName);
}
});
/**
* @static
*/
Entity._id = 0;