-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
469 lines (404 loc) · 7.96 KB
/
index.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*!
*
* Manager for Entity/Component/Systems
*
* http://github.com/entity
*
* MIT
*
*/
/**
* Reference to slice.
*/
var slice = [].slice
/**
* Module dependencies.
*/
var Emitter = require('emitter')
var Entity = require('entity')
var Set = require('set')
/**
* Exports.
*/
module.exports = Manager
/**
* Manager class.
*/
function Manager (parent) {
this.parent = parent || this
this.root = this.parent.root || this
this.children = new Set()
this.systems = new Set()
this.entities = new Set()
this.components = new Set()
this.bindEvents()
this.state('ready')
}
/**
* Make Emitter.
*/
Emitter(Manager.prototype)
/**
* Determine whether this is the root manager.
*
* @return {Boolean}
* @api public
*/
Manager.prototype.isRoot = function () {
return this.root === this
}
/**
* Bind behavior on certain events.
*
* @return {Manager} this
* @api private
*/
Manager.prototype.bindEvents = function () {
var self = this
// lazily apply components on init
this.on('init', function () {
if (!this.hasAppliedComponents) {
this.applyComponents()
}
})
// return to ready
// after tear fires
this.on('tear', function () {
setTimeout(function () {
self.state('ready')
}, 0)
})
return this
}
/**
* Create an entity of components, and use it.
*
* @param {Object} c...
* @return {Entity} entity
* @api public
*/
Manager.prototype.createEntity = function () {
var comps = slice.call(arguments)
var e = new Entity()
comps.forEach(function (c) {
e.add(c)
})
this.use(e)
return e
}
/**
* Remove an entity.
*
* @param {Entity} e
* @return {Manager} this
* @api public
*/
Manager.prototype.removeEntity = function (e) {
var self = this
this.entities.remove(e)
e.components.each(function (c) {
c.entities.remove(e)
// remove component when
// it has no entities
if (!c.entities.size()) {
self.components.remove(c)
}
})
return this
}
/**
* Remove all entities.
*
* @return {Manager} this
* @api public
*/
Manager.prototype.removeAllEntities = function () {
var self = this
this.entities.values().slice().forEach(function (e) {
self.removeEntity(e)
})
return this
}
/**
* Register all the components of an entity.
*
* @param {Entity} e
* @return {Manager} this
* @api private
*/
Manager.prototype.registerComponents = function (e) {
var self = this
e.components.each(function (c) {
self.registerComponent(c, e)
})
return this
}
/**
* Register a component with an entity.
*
* @param {Object} c
* @param {Entity} e
* @return {Object} this
* @api private
*/
Manager.prototype.registerComponent = function (c, e) {
c.entities = c.entities || new Set()
c.entities.add(e)
this.components.add(c)
return this
}
/**
* Apply component data to an entity or all.
*
* @param {Entity} [e]
* @api public
*/
Manager.prototype.applyComponents = function (e) {
if (e) {
e.applyComponents()
return this
}
else {
this.entities.each(function (e) {
e.applyComponents()
})
this.hasAppliedComponents = true
}
return this
}
/**
* Create a child manager and use it.
*
* @return {Manager} new_manager
* @api public
*/
Manager.prototype.createManager = function () {
var manager = new Manager(this)
this.use(manager)
return manager
}
/**
* Attach listeners for the methods of an object.
*
* @param {Object} obj
* @return {Manager} this
* @api private
*/
Manager.prototype.addListeners = function (obj) {
var self = this
Object.keys(obj)
.filter(function (key) { // don't add private methods
return '_' != key.substr(0,1)
})
.forEach(function (key) {
var val = obj[key]
if ('function' == typeof val) {
var fn = val
self.on(key, function () {
var args = slice.call(arguments)
if (!fn.length) {
return fn.call(obj)
}
self.each(obj, function (e) {
fn.apply(obj, [e].concat(args))
})
})
}
})
return this
}
/**
* Get all entities using all
* the components in the array.
*
* @param {Array} arr
* @return {Set} entities
* @api public
*/
Manager.prototype.of = function (arr) {
var self = this
if (!arr.length) return new Set()
if (1===arr.length && arr[0].entities) {
return new Set(arr[0].entities.values())
}
var entities = arr.reduce(function (p, n) {
return p.intersect(n.entities || new Set())
}, this.entities)
return entities
}
/**
* Iterate entities of certain components,
* or through all if no component is passed.
*
* @param {Object} [c...]
* @param {Function} fn
* @return {Object} this
* @api public
*/
Manager.prototype.each = function (c, fn) {
var args
if ('function' == typeof c) {
this.entities.each(c)
return this
}
else if (Array.isArray(c)) {
args = c
}
else {
args = slice.call(arguments)
}
fn = args.pop()
this.of(args).each(fn)
return this
}
/**
* Get the first entity matching component.
*
* @param {Component} c
* @return {Entity}
* @api public
*/
Manager.prototype.get = function (c) {
return this.of([c]).values()[0]
}
/**
* Use a entity, system or manager.
*
* When entity:
* - Registers an entity (creating a
* new one or reusing the one passed).
*
* When system:
* - Registers a system to be used.
* Order matters.
*
* When manager:
* - Adds the manager to its children.
*
* @param {Entity|Mixed|Manager} item
* @param {Boolean} reuse
* @return {Manager} this
* @api public
*/
Manager.prototype.use = function (item, reuse) {
if (item instanceof Manager) {
this.children.add(item)
}
else if (item instanceof Entity) {
var e = item
e.on('add', function (c) {
self.registerComponent(c, e)
})
this.entities.add(e)
this.registerComponents(e)
}
else {
this.addListeners(item)
this.systems.add(item)
}
return this
}
/**
* Run the systems for an entity alone.
*
* @param {Entity} e
* @param {String} method
* @return {Manager} this
* @api private
*/
Manager.prototype.runSystems = function (e, method) {
e.components.each(function (c) {
if (method in c) c[method].call(c, e)
})
return this
}
/**
* Game state switches.
*
* States:
*
* init -> ( start - pause - stop ) -> tear
* (---------loop---------)
*
* @return {Manager} this
* @api public
*/
Manager.prototype.init = function () { return this.state('init') }
Manager.prototype.start = function () { return this.state('start') }
Manager.prototype.pause = function () { return this.state('pause') }
Manager.prototype.stop = function () { return this.state('stop') }
Manager.prototype.tear = function () { return this.state('tear') }
/**
* Reset state.
*
* Executes:
*
* stop -> tear -> init -> start
*
* @return {Manager} this
* @api public
*/
Manager.prototype.reset = function () {
this.stop()
this.tear()
this.once('ready', function () {
this.init()
this.start()
})
return this
}
/**
* State accessor. Also emits state on change.
*
* @param {String} [s]
* @return {Mixed}
* @api private
*/
Manager.prototype.state = function (s) {
if (null == s) return this._state
this._state = s
this.emit(s)
this.emit('state', s)
return this
}
/**
* Join (late) an entity.
*
* It will try to apply components and systems
* based on the current state.
*
* @param {Entity} e
* @return {Manager} this
* @api public
*/
Manager.prototype.join = function (e) {
this.use(e)
this.applyComponents(e)
var s = this.state()
if ('none' == s) return this
if ('init' == s || 'start' == s || 'pause' == s || 'stop' == s) {
this.runSystems(e, 'init')
}
if ('start' == s || 'pause' == s) {
this.runSystems(e, 'start')
}
if ('pause' == s) {
this.runSystems(e, 'pause')
}
return this
}
/**
* Generate an array snapshot of all entities
* property values.
*
* @return {Array}
* @api public
*/
Manager.prototype.snapshot = function () {
var snap = this.entities
.values()
.map(function (e) {
return e._properties
})
return snap
}