-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_component.js
144 lines (125 loc) · 3.57 KB
/
system_component.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
'use strict';
const toPairs = require('lodash.topairs');
const { ASystemSymbol } = require('./internal/symbols');
const AComponent = require('./acomponent');
/**
* `SystemComponent` is a component which allow you to add systems to an entity. We recommend to add it
* only to world entities to keep simple and predictable behaviours.
*
* System are always executed in the order they are added: first added, first updated.
*
* To create your own system, take a look a the file `asystem.js`.
*/
class SystemComponent extends AComponent
{
constructor(parent)
{
super(parent);
/**
* All systems attached to this entity.
* @type {Object.<ASystem>}
* @private
*/
this._systems = [];
}
/**
* Add a system.
*
* System are always executed in the order they are added: first added, first executed.
*
* See `ASystem` documentation for more informations.
*
* @param {ASystem} SystemType System to add.
* @param {*} args Arguments to pass to system constructor
*/
add(SystemType, ...args)
{
this._assertASystemType(SystemType);
this._systems.push(new SystemType(this.parent, ...args));
}
/**
* Delete a system. If system is not found, an error will be thrown.
*
* @param {ASystem} SystemType System to remove.
*/
delete(SystemType)
{
this._assertASystemType(SystemType);
const systemIndex = this._findSystemIndex(SystemType);
if (systemIndex === -1)
{
throw new Error('Can not delete system: not found');
}
this._systems.splice(systemIndex, 1);
}
/**
* Call `earlyUpdate()` on all registered systems.
*/
earlyUpdate()
{
// Get all entities which comply to required components.
const rootEntity = this.parent;
this._systems.forEach(system =>
{
if (!system.earlyUpdate)
{
return;
}
const complyingEntities = [];
this._findComplyingEntities(rootEntity, system, complyingEntities);
system.earlyUpdate(complyingEntities);
});
}
/**
* Call `lateUpdate()` on all registered systems.
*/
lateUpdate()
{
// Get all entities which comply to required components.
const rootEntity = this.parent;
this._systems.forEach(system =>
{
if (!system.lateUpdate)
{
return;
}
const complyingEntities = [];
this._findComplyingEntities(rootEntity, system, complyingEntities);
system.lateUpdate(complyingEntities);
});
}
_findSystemIndex(SystemType)
{
return this._systems.findIndex(system =>
{
return system.constructor === SystemType;
});
}
_findComplyingEntities(entity, system, complyingEntities)
{
if (entity.has(system.requiredComponents))
{
complyingEntities.push(entity);
}
toPairs(entity._childs).forEach(child =>
{
this._findComplyingEntities(child[1], system, complyingEntities);
});
}
_assertASystemType(SystemType)
{
if (SystemType._ASystemSymbol !== ASystemSymbol)
{
throw new TypeError('SystemType must be a type which inherit ASystem');
}
}
get identity()
{
return SystemComponent.identity;
}
static get identity()
{
return 'systems';
}
}
module.exports = SystemComponent;