forked from qiao/ces.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.js
228 lines (196 loc) · 6.25 KB
/
world.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
var Class = require('./class'),
Family = require('./family'),
EntityList = require('./entitylist');
/**
* The world is the container of all the entities and systems.
* @class
*/
var World = module.exports = Class.extend({
/**
* @constructor
*/
init: function () {
/**
* A map from familyId to family
* @private
*/
this._families = {};
/**
* @private
*/
this._systems = [];
/**
* @private
*/
this._entities = new EntityList();
},
/**
* Add a system to this world.
* @public
* @param {System} system
*/
addSystem: function (system) {
this._systems.push(system);
system.addedToWorld(this);
return this;
},
/**
* Remove a system from this world.
* @public
* @param {System} system
*/
removeSystem: function (system) {
var systems, i, len;
systems = this._systems;
for (i = 0, len = systems.length; i < len; ++i) {
if (systems[i] === system) {
systems.splice(i, 1);
system.removedFromWorld();
}
}
},
/**
* Add an entity to this world.
* @public
* @param {Entity} entity
*/
addEntity: function (entity) {
var families, familyId, self;
// try to add the entity into each family
families = this._families;
for (familyId in families) {
families[familyId].addEntityIfMatch(entity);
}
self = this;
// update the entity-family relationship whenever components are
// added to or removed from the entities
entity.onComponentAdded.add(function (entity, component) {
self._onComponentAdded(entity, component);
});
entity.onComponentRemoved.add(function (entity, component) {
self._onComponentRemoved(entity, component);
});
this._entities.add(entity);
},
/**
* Remove and entity from this world.
* @public
* @param {Entity} entity
*/
removeEntity: function (entity) {
var families, familyId;
// try to remove the entity from each family
families = this._families;
for (familyId in families) {
families[familyId].removeEntity(entity);
}
this._entities.remove(entity);
},
/**
* Get the entities having all the specified componets.
* @public
* @param {...String} componentNames
* @return {Array} an array of entities.
*/
getEntities: function (/* componentNames */) {
var familyId, families;
familyId = this._getFamilyId(arguments);
this._ensureFamilyExists(arguments);
return this._families[familyId].getEntities();
},
/**
* For each system in the world, call its `update` method.
* @public
* @param {Number} dt time interval between updates.
*/
update: function (dt) {
var systems, i, len;
systems = this._systems;
for (i = 0, len = systems.length; i < len; ++i) {
systems[i].update(dt);
}
},
/**
* Returns the signal for entities added with the specified components. The
* signal is also emitted when a component is added to an entity causing it
* match the specified component names.
* @public
* @param {...String} componentNames
* @return {Signal} A signal which is emitted every time an entity with
* specified components is added.
*/
entityAdded: function(/* componentNames */) {
var familyId, families;
familyId = this._getFamilyId(arguments);
this._ensureFamilyExists(arguments);
return this._families[familyId].entityAdded;
},
/**
* Returns the signal for entities removed with the specified components.
* The signal is also emitted when a component is removed from an entity
* causing it to no longer match the specified component names.
* @public
* @param {...String} componentNames
* @return {Signal} A signal which is emitted every time an entity with
* specified components is removed.
*/
entityRemoved: function(/* componentNames */) {
var familyId, families;
familyId = this._getFamilyId(arguments);
this._ensureFamilyExists(arguments);
return this._families[familyId].entityRemoved;
},
/**
* Creates a family for the passed array of component names if it does not
* exist already.
* @param {Array.<String>} components
*/
_ensureFamilyExists: function(components) {
var families = this._families;
var familyId = this._getFamilyId(components);
if (!families[familyId]) {
families[familyId] = new Family(
Array.prototype.slice.call(components)
);
for (var node = this._entities.head; node; node = node.next) {
families[familyId].addEntityIfMatch(node.entity);
}
}
},
/**
* Returns the family ID for the passed array of component names. A family
* ID is a comma separated string of all component names with a '$'
* prepended.
* @param {Array.<String>} components
* @return {String} The family ID for the passed array of components.
*/
_getFamilyId: function(components) {
return '$' + Array.prototype.join.call(components, ',');
},
/**
* Handler to be called when a component is added to an entity.
* @private
* @param {Entity} entity
* @param {String} componentName
*/
_onComponentAdded: function (entity, componentName) {
var families, familyId;
families = this._families;
for (familyId in families) {
families[familyId].onComponentAdded(entity, componentName);
}
},
/**
* Handler to be called when component is removed from an entity.
* @private
* @param {Entity} entity
* @param {String} componentName
*/
_onComponentRemoved: function (entity, componentName) {
var families, familyId;
families = this._families;
for (familyId in families) {
families[familyId].onComponentRemoved(entity, componentName);
}
}
});