-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathsystem.js
More file actions
210 lines (171 loc) · 7.39 KB
/
system.js
File metadata and controls
210 lines (171 loc) · 7.39 KB
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
import { SortedLoopArray } from '../../../core/sorted-loop-array.js';
import { ComponentSystem } from '../system.js';
import { ScriptComponent } from './component.js';
import { ScriptComponentData } from './data.js';
/**
* @import { AppBase } from '../../app-base.js'
*/
const METHOD_INITIALIZE_ATTRIBUTES = '_onInitializeAttributes';
const METHOD_INITIALIZE = '_onInitialize';
const METHOD_POST_INITIALIZE = '_onPostInitialize';
const METHOD_UPDATE = '_onUpdate';
const METHOD_POST_UPDATE = '_onPostUpdate';
// Ever-increasing integer used as the execution order of new script components. We are using an
// ever-increasing number and not the order of the script component in the components array because
// if we ever remove components from the array, we would have to re-calculate the execution order
// for all subsequent script components in the array every time, which would be slow.
let executionOrderCounter = 0;
/**
* Allows scripts to be attached to an Entity and executed.
*
* @category Script
*/
class ScriptComponentSystem extends ComponentSystem {
/**
* Create a new ScriptComponentSystem.
*
* @param {AppBase} app - The application.
* @ignore
*/
constructor(app) {
super(app);
this.id = 'script';
this.ComponentType = ScriptComponent;
this.DataType = ScriptComponentData;
// list of all entities script components
// we are using pc.SortedLoopArray because it is
// safe to modify while looping through it
this._components = new SortedLoopArray({
sortBy: '_executionOrder'
});
// holds all the enabled script components
// (whose entities are also enabled). We are using pc.SortedLoopArray
// because it is safe to modify while looping through it. This array often
// change during update and postUpdate loops as entities and components get
// enabled or disabled
this._enabledComponents = new SortedLoopArray({
sortBy: '_executionOrder'
});
// if true then we are currently preloading scripts
this.preloading = true;
this.on('beforeremove', this._onBeforeRemove, this);
this.app.systems.on('initialize', this._onInitialize, this);
this.app.systems.on('postInitialize', this._onPostInitialize, this);
this.app.systems.on('update', this._onUpdate, this);
this.app.systems.on('postUpdate', this._onPostUpdate, this);
}
initializeComponentData(component, data) {
// Set execution order to an ever-increasing number
// and add to the end of the components array.
component._executionOrder = executionOrderCounter++;
this._components.append(component);
// check we don't overflow executionOrderCounter
if (executionOrderCounter > Number.MAX_SAFE_INTEGER) {
this._resetExecutionOrder();
}
component.enabled = data.hasOwnProperty('enabled') ? !!data.enabled : true;
// if enabled then add this component to the end of the enabledComponents array
// Note, we should be OK to just append this to the end instead of using insert()
// which will search for the right slot to insert the component based on execution order,
// because the execution order of this script should be larger than all the others in the
// enabledComponents array since it was just added.
if (component.enabled && component.entity.enabled) {
this._enabledComponents.append(component);
}
if (data.hasOwnProperty('order') && data.hasOwnProperty('scripts')) {
component._scriptsData = data.scripts;
for (let i = 0; i < data.order.length; i++) {
component.create(data.order[i], {
enabled: data.scripts[data.order[i]].enabled,
attributes: data.scripts[data.order[i]].attributes,
preloading: this.preloading
});
}
}
}
cloneComponent(entity, clone) {
const order = [];
const scripts = { };
for (let i = 0; i < entity.script._scripts.length; i++) {
const scriptInstance = entity.script._scripts[i];
const scriptName = scriptInstance.__scriptType.__name;
order.push(scriptName);
const attributes = entity.script._attributeDataMap?.get(scriptName) || { };
for (const key in scriptInstance.__attributes) {
attributes[key] = scriptInstance.__attributes[key];
}
scripts[scriptName] = {
enabled: scriptInstance._enabled,
attributes: attributes
};
}
for (const key in entity.script._scriptsIndex) {
const scriptEntry = entity.script._scriptsIndex[key];
if (scriptEntry.awaiting) {
order.splice(scriptEntry.ind, 0, key);
}
}
const data = {
enabled: entity.script.enabled,
order: order,
scripts: scripts
};
return this.addComponent(clone, data);
}
_resetExecutionOrder() {
executionOrderCounter = 0;
for (let i = 0, len = this._components.length; i < len; i++) {
this._components.items[i]._executionOrder = executionOrderCounter++;
}
}
_callComponentMethod(components, name, dt) {
for (components.loopIndex = 0; components.loopIndex < components.length; components.loopIndex++) {
components.items[components.loopIndex][name](dt);
}
}
_onInitialize() {
this.preloading = false;
// initialize attributes on all components
this._callComponentMethod(this._components, METHOD_INITIALIZE_ATTRIBUTES);
// call onInitialize on enabled components
this._callComponentMethod(this._enabledComponents, METHOD_INITIALIZE);
}
_onPostInitialize() {
// call onPostInitialize on enabled components
this._callComponentMethod(this._enabledComponents, METHOD_POST_INITIALIZE);
}
_onUpdate(dt) {
// call onUpdate on enabled components
this._callComponentMethod(this._enabledComponents, METHOD_UPDATE, dt);
}
_onPostUpdate(dt) {
// call onPostUpdate on enabled components
this._callComponentMethod(this._enabledComponents, METHOD_POST_UPDATE, dt);
}
// inserts the component into the enabledComponents array
// which finds the right slot based on component._executionOrder
_addComponentToEnabled(component) {
this._enabledComponents.insert(component);
}
// removes the component from the enabledComponents array
_removeComponentFromEnabled(component) {
this._enabledComponents.remove(component);
}
_onBeforeRemove(entity, component) {
const ind = this._components.items.indexOf(component);
if (ind >= 0) {
component._onBeforeRemove();
}
this._removeComponentFromEnabled(component);
// remove from components array
this._components.remove(component);
}
destroy() {
super.destroy();
this.app.systems.off('initialize', this._onInitialize, this);
this.app.systems.off('postInitialize', this._onPostInitialize, this);
this.app.systems.off('update', this._onUpdate, this);
this.app.systems.off('postUpdate', this._onPostUpdate, this);
}
}
export { ScriptComponentSystem };