mani-game-engine 1.0.0-pre.5 → 1.0.0-pre.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +69 -69
- package/lib/clock.d.ts +55 -31
- package/lib/clock.js +117 -78
- package/lib/clock.js.map +1 -1
- package/lib/ecsInjector.d.ts +46 -24
- package/lib/ecsInjector.js +222 -138
- package/lib/ecsInjector.js.map +1 -1
- package/lib/entity.d.ts +35 -21
- package/lib/entity.js +167 -85
- package/lib/entity.js.map +1 -1
- package/lib/gameEngine.d.ts +89 -76
- package/lib/gameEngine.js +368 -316
- package/lib/gameEngine.js.map +1 -1
- package/lib/index.d.ts +12 -10
- package/lib/index.js +35 -10
- package/lib/index.js.map +1 -1
- package/lib/injector.d.ts +130 -0
- package/lib/injector.js +324 -0
- package/lib/injector.js.map +1 -0
- package/lib/scope/scopeContext.d.ts +71 -53
- package/lib/scope/scopeContext.js +297 -206
- package/lib/scope/scopeContext.js.map +1 -1
- package/lib/systemContext.d.ts +15 -11
- package/lib/systemContext.js +38 -16
- package/lib/systemContext.js.map +1 -1
- package/lib/types.d.ts +83 -67
- package/lib/types.js +19 -9
- package/lib/types.js.map +1 -1
- package/lib/utils/map2k.d.ts +6 -6
- package/lib/utils/map2k.js +43 -39
- package/lib/utils/map2k.js.map +1 -1
- package/package.json +39 -40
- package/src/clock.ts +163 -103
- package/src/ecsInjector.ts +279 -183
- package/src/entity.ts +202 -116
- package/src/gameEngine.ts +472 -411
- package/src/index.ts +33 -14
- package/src/injector.ts +410 -0
- package/src/scope/scopeContext.ts +380 -267
- package/src/systemContext.ts +40 -22
- package/src/types.ts +99 -82
- package/src/utils/map2k.ts +52 -52
package/lib/gameEngine.js
CHANGED
|
@@ -1,317 +1,369 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
25
|
-
this.
|
|
26
|
-
this.
|
|
27
|
-
this.
|
|
28
|
-
this.
|
|
29
|
-
this.
|
|
30
|
-
this.
|
|
31
|
-
this.
|
|
32
|
-
this.
|
|
33
|
-
this.
|
|
34
|
-
this.
|
|
35
|
-
this.
|
|
36
|
-
this.
|
|
37
|
-
this.
|
|
38
|
-
this.
|
|
39
|
-
this.
|
|
40
|
-
this.
|
|
41
|
-
this.
|
|
42
|
-
this.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
});
|
|
65
|
-
const
|
|
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
|
-
this.
|
|
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
|
-
this.
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
this.
|
|
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
|
-
return
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
this.
|
|
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
|
-
if (
|
|
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
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GameEngine = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
const ecsInjector_1 = require("./ecsInjector");
|
|
6
|
+
const clock_1 = require("./clock");
|
|
7
|
+
const injector_1 = require("./injector");
|
|
8
|
+
const defaultOptions = {
|
|
9
|
+
fixedDeltaTime: 1 / 60,
|
|
10
|
+
plugins: [],
|
|
11
|
+
};
|
|
12
|
+
let id = 0;
|
|
13
|
+
class GameEngine {
|
|
14
|
+
get started() { return this._started; }
|
|
15
|
+
get frameCount() { return this._frameCount; }
|
|
16
|
+
get fixedFrameCount() { return this._fixedFrameCount; }
|
|
17
|
+
get numEntities() { return this.entitySystemMap.size; }
|
|
18
|
+
constructor(injector, clock) {
|
|
19
|
+
this.injector = injector;
|
|
20
|
+
this.clock = clock;
|
|
21
|
+
this.id = id++;
|
|
22
|
+
this.onEarlyUpdateSet = new Set();
|
|
23
|
+
this.onUpdateSet = new Set();
|
|
24
|
+
this.onFixedUpdateSet = new Set();
|
|
25
|
+
this.onPreFixedUpdateSet = new Set();
|
|
26
|
+
this.onPhysicsUpdateSet = new Set();
|
|
27
|
+
this.onPrePhysicsUpdateSet = new Set();
|
|
28
|
+
this.onPostPhysicsUpdateSet = new Set();
|
|
29
|
+
this.onLateFixedUpdateSet = new Set();
|
|
30
|
+
this.onLateUpdateSet = new Set();
|
|
31
|
+
this.onRenderUpdateSet = new Set();
|
|
32
|
+
this.onAddEntitySet = new Set();
|
|
33
|
+
this.onRemoveEntitySet = new Set();
|
|
34
|
+
this.entitySystemMap = new Map();
|
|
35
|
+
this.serviceClasses = new Set();
|
|
36
|
+
this.services = [];
|
|
37
|
+
this.resolveFrame = () => undefined;
|
|
38
|
+
this.addQueue = [];
|
|
39
|
+
this.removeQueue = [];
|
|
40
|
+
this._started = false;
|
|
41
|
+
this._frameCount = 0;
|
|
42
|
+
this._fixedFrameCount = 0;
|
|
43
|
+
this.signalMappings = new Map();
|
|
44
|
+
this.staticSignalMappings = new Map();
|
|
45
|
+
this.signalMapBindings = new Map();
|
|
46
|
+
this.injector.map(GameEngine).toValue(this);
|
|
47
|
+
this.onPrepare = this.injector.getSignal(types_1.EngineSignals.OnPrepare);
|
|
48
|
+
this.onStart = this.injector.getSignal(types_1.EngineSignals.OnStart);
|
|
49
|
+
this.onEnd = this.injector.getSignal(types_1.EngineSignals.OnEnd);
|
|
50
|
+
this.onRender = this.injector.getSignal(types_1.EngineSignals.OnRender);
|
|
51
|
+
this.onFixedUpdate = this.injector.getSignal(types_1.EngineSignals.OnFixedUpdate);
|
|
52
|
+
this.onUpdate = this.injector.getSignal(types_1.EngineSignals.OnUpdate);
|
|
53
|
+
this.onLateUpdate = this.injector.getSignal(types_1.EngineSignals.OnLateUpdate);
|
|
54
|
+
this.onPrePhysicsUpdate = this.injector.getSignal(types_1.EngineSignals.OnPrePhysicsUpdate);
|
|
55
|
+
this.onPhysicsUpdate = this.injector.getSignal(types_1.EngineSignals.OnPhysicsUpdate);
|
|
56
|
+
this.onPostPhysicsUpdate = this.injector.getSignal(types_1.EngineSignals.OnPostPhysicsUpdate);
|
|
57
|
+
this.onLateFixedUpdate = this.injector.getSignal(types_1.EngineSignals.onLateFixedUpdate);
|
|
58
|
+
this.onAddEntity = this.injector.getSignal(types_1.EngineSignals.OnAddEntity);
|
|
59
|
+
this.onRemoveEntity = this.injector.getSignal(types_1.EngineSignals.OnRemoveEntity);
|
|
60
|
+
this.setupClock();
|
|
61
|
+
this.prepareFrame();
|
|
62
|
+
}
|
|
63
|
+
static createDefault(options) {
|
|
64
|
+
const opts = Object.assign(Object.assign({}, defaultOptions), options);
|
|
65
|
+
const clock = new clock_1.GameClock({
|
|
66
|
+
fixedDeltaTime: opts.fixedDeltaTime,
|
|
67
|
+
autoStart: false,
|
|
68
|
+
});
|
|
69
|
+
const injector = new ecsInjector_1.EcsInjector();
|
|
70
|
+
const engine = new GameEngine(injector, clock);
|
|
71
|
+
engine.installPlugins(opts.plugins);
|
|
72
|
+
return engine;
|
|
73
|
+
}
|
|
74
|
+
static createDefaultWithInjector(injector, options) {
|
|
75
|
+
const opts = Object.assign(Object.assign({}, defaultOptions), options);
|
|
76
|
+
const clock = new clock_1.GameClock({
|
|
77
|
+
fixedDeltaTime: opts.fixedDeltaTime,
|
|
78
|
+
autoStart: false,
|
|
79
|
+
});
|
|
80
|
+
const engine = new GameEngine(injector, clock);
|
|
81
|
+
engine.installPlugins(opts.plugins);
|
|
82
|
+
return engine;
|
|
83
|
+
}
|
|
84
|
+
addService(service) {
|
|
85
|
+
this.mapAnnotatedSignals(service);
|
|
86
|
+
service.onStart && service.onStart();
|
|
87
|
+
service.onPreFixedUpdate && this.onPreFixedUpdateSet.add(service);
|
|
88
|
+
service.onFixedUpdate && this.onFixedUpdateSet.add(service);
|
|
89
|
+
service.onUpdate && this.onUpdateSet.add(service);
|
|
90
|
+
service.onLateUpdate && this.onLateUpdateSet.add(service);
|
|
91
|
+
service.onPrePhysicsUpdate && this.onPrePhysicsUpdateSet.add(service);
|
|
92
|
+
service.onPhysicsUpdate && this.onPhysicsUpdateSet.add(service);
|
|
93
|
+
service.onPostPhysicsUpdate && this.onPostPhysicsUpdateSet.add(service);
|
|
94
|
+
service.onLateFixedUpdate && this.onLateFixedUpdateSet.add(service);
|
|
95
|
+
service.onRender && this.onRenderUpdateSet.add(service);
|
|
96
|
+
service.onAddEntity && this.onAddEntitySet.add(service);
|
|
97
|
+
service.onRemoveEntity && this.onRemoveEntitySet.add(service);
|
|
98
|
+
this.services.push(service);
|
|
99
|
+
}
|
|
100
|
+
installPlugins(plugins) {
|
|
101
|
+
plugins.forEach(plugin => {
|
|
102
|
+
plugin.onPrepare && plugin.onPrepare(this);
|
|
103
|
+
});
|
|
104
|
+
plugins.forEach(plugin => {
|
|
105
|
+
plugin.onStart && plugin.onStart(this);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
registerSystem(systemClass) {
|
|
109
|
+
this.mapStaticAnnotatedSignals(systemClass);
|
|
110
|
+
if (this._started)
|
|
111
|
+
throw new Error('Register Systems before engine is started');
|
|
112
|
+
this.injector.registerSystem(systemClass);
|
|
113
|
+
}
|
|
114
|
+
registerService(serviceClass) {
|
|
115
|
+
this.mapStaticAnnotatedSignals(serviceClass);
|
|
116
|
+
if (this._started)
|
|
117
|
+
throw new Error('Register Services before engine is started');
|
|
118
|
+
this.injector.map(serviceClass).toSingleton();
|
|
119
|
+
this.serviceClasses.add(serviceClass);
|
|
120
|
+
}
|
|
121
|
+
getService(serviceClass) {
|
|
122
|
+
return this.injector.get(serviceClass);
|
|
123
|
+
}
|
|
124
|
+
start() {
|
|
125
|
+
this._started = true;
|
|
126
|
+
for (const serviceClass of this.serviceClasses) {
|
|
127
|
+
const service = this.injector.get(serviceClass);
|
|
128
|
+
this.addService(service);
|
|
129
|
+
}
|
|
130
|
+
this.clock.start();
|
|
131
|
+
this.onStart.dispatch();
|
|
132
|
+
}
|
|
133
|
+
setupClock() {
|
|
134
|
+
this.clock.onPrepare.add(this.prepareFrame, this);
|
|
135
|
+
this.clock.onEarlyUpdate.add(({ time, deltaTime }) => {
|
|
136
|
+
for (const system of this.onEarlyUpdateSet) {
|
|
137
|
+
system.onEarlyUpdate(time, deltaTime);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
this.clock.onFixedUpdate.add((params) => {
|
|
141
|
+
const { time, deltaTime } = params;
|
|
142
|
+
for (const system of this.onPreFixedUpdateSet) {
|
|
143
|
+
system.onPreFixedUpdate(time, deltaTime);
|
|
144
|
+
}
|
|
145
|
+
for (const system of this.onFixedUpdateSet) {
|
|
146
|
+
system.onFixedUpdate(time, deltaTime);
|
|
147
|
+
}
|
|
148
|
+
this.onFixedUpdate.dispatch(params);
|
|
149
|
+
for (const system of this.onPhysicsUpdateSet) {
|
|
150
|
+
system.onPhysicsUpdate(time, deltaTime);
|
|
151
|
+
}
|
|
152
|
+
this.onPhysicsUpdate.dispatch(params);
|
|
153
|
+
this.onPostPhysicsUpdate.dispatch(params);
|
|
154
|
+
for (const system of this.onPostPhysicsUpdateSet) {
|
|
155
|
+
system.onPostPhysicsUpdate(time, deltaTime);
|
|
156
|
+
}
|
|
157
|
+
this.onLateFixedUpdate.dispatch(params);
|
|
158
|
+
for (const system of this.onLateFixedUpdateSet) {
|
|
159
|
+
system.onLateFixedUpdate(time, deltaTime);
|
|
160
|
+
}
|
|
161
|
+
this._fixedFrameCount++;
|
|
162
|
+
});
|
|
163
|
+
this.clock.onUpdate.add(onUpdateParams => {
|
|
164
|
+
const { time, deltaTime, alpha } = onUpdateParams;
|
|
165
|
+
for (const system of this.onUpdateSet) {
|
|
166
|
+
system.onUpdate(time, deltaTime, alpha);
|
|
167
|
+
}
|
|
168
|
+
this.onUpdate.dispatch(onUpdateParams);
|
|
169
|
+
for (const system of this.onLateUpdateSet) {
|
|
170
|
+
system.onLateUpdate(time, deltaTime, alpha);
|
|
171
|
+
}
|
|
172
|
+
this.onLateUpdate.dispatch(onUpdateParams);
|
|
173
|
+
for (const system of this.onRenderUpdateSet) {
|
|
174
|
+
system.onRender(time, deltaTime, alpha);
|
|
175
|
+
}
|
|
176
|
+
this.onRender.dispatch(onUpdateParams);
|
|
177
|
+
this._frameCount++;
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
add(entity) {
|
|
181
|
+
return new Promise(resolve => this.addQueue.push([entity, resolve]));
|
|
182
|
+
}
|
|
183
|
+
waitFrame() {
|
|
184
|
+
return this.framePromise;
|
|
185
|
+
}
|
|
186
|
+
async remove(entity) {
|
|
187
|
+
if (entity.markRemoval)
|
|
188
|
+
return;
|
|
189
|
+
entity.markRemoval = true;
|
|
190
|
+
return new Promise(resolve => this.removeQueue.push([entity, resolve]));
|
|
191
|
+
}
|
|
192
|
+
stop() {
|
|
193
|
+
this.clock.stop();
|
|
194
|
+
}
|
|
195
|
+
runAction(action) {
|
|
196
|
+
return action(this);
|
|
197
|
+
}
|
|
198
|
+
getSignal(signalId) {
|
|
199
|
+
return this.injector.getSignal(signalId);
|
|
200
|
+
}
|
|
201
|
+
mapStaticAnnotatedSignals(target) {
|
|
202
|
+
let staticSignalMap = this.getStaticSignalMap(target);
|
|
203
|
+
if (staticSignalMap.length === 0)
|
|
204
|
+
return;
|
|
205
|
+
const bindings = [];
|
|
206
|
+
for (const [key, signal] of staticSignalMap) {
|
|
207
|
+
bindings.push(signal.add(target[key], target));
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
mapAnnotatedSignals(target) {
|
|
211
|
+
const signalMap = this.getSignalMap(target.constructor);
|
|
212
|
+
if (signalMap.length === 0)
|
|
213
|
+
return;
|
|
214
|
+
const bindings = [];
|
|
215
|
+
for (const [key, signal] of signalMap) {
|
|
216
|
+
bindings.push(signal.add(target[key], target));
|
|
217
|
+
}
|
|
218
|
+
this.signalMapBindings.set(target, bindings);
|
|
219
|
+
}
|
|
220
|
+
unmapAnnotatedSignals(target) {
|
|
221
|
+
const signalBindings = this.signalMapBindings.get(target);
|
|
222
|
+
if (!signalBindings)
|
|
223
|
+
return;
|
|
224
|
+
for (const binding of signalBindings) {
|
|
225
|
+
binding.detach();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
getSignalMap(target) {
|
|
229
|
+
return (0, injector_1.putIfAbsent)(this.signalMappings, target, () => {
|
|
230
|
+
const handlers = ecsInjector_1.signalHandlers.get(target);
|
|
231
|
+
if (!handlers)
|
|
232
|
+
return [];
|
|
233
|
+
const result = [];
|
|
234
|
+
for (const [key, signalId] of handlers) {
|
|
235
|
+
result.push([key, this.getSignal(signalId)]);
|
|
236
|
+
}
|
|
237
|
+
return result;
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
getStaticSignalMap(target) {
|
|
241
|
+
return (0, injector_1.putIfAbsent)(this.staticSignalMappings, target, () => {
|
|
242
|
+
const handlers = ecsInjector_1.staticSignalHandlers.get(target);
|
|
243
|
+
if (!handlers)
|
|
244
|
+
return [];
|
|
245
|
+
const result = [];
|
|
246
|
+
for (const [key, signalId] of handlers) {
|
|
247
|
+
result.push([key, this.getSignal(signalId)]);
|
|
248
|
+
}
|
|
249
|
+
return result;
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
dispose() {
|
|
253
|
+
this.clock.stop();
|
|
254
|
+
this.clock.dispose();
|
|
255
|
+
for (const systems of this.entitySystemMap.values()) {
|
|
256
|
+
for (const system of systems) {
|
|
257
|
+
system.system.onEnd && system.system.onEnd();
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
for (const service of this.services) {
|
|
261
|
+
service.onEnd && service.onEnd();
|
|
262
|
+
}
|
|
263
|
+
this.onEnd.dispatch();
|
|
264
|
+
}
|
|
265
|
+
prepareFrame() {
|
|
266
|
+
for (const [entity, resolve] of this.removeQueue) {
|
|
267
|
+
const entities = entity.getAllEntities();
|
|
268
|
+
for (const entity of entities) {
|
|
269
|
+
entity.markRemoval = false;
|
|
270
|
+
if (!entity.gameEngine) {
|
|
271
|
+
console.log(entity);
|
|
272
|
+
}
|
|
273
|
+
entity.gameEngine = undefined;
|
|
274
|
+
const systemContexts = this.entitySystemMap.get(entity);
|
|
275
|
+
this.entitySystemMap.delete(entity);
|
|
276
|
+
if (systemContexts) {
|
|
277
|
+
for (const context of systemContexts) {
|
|
278
|
+
this.disposeContext(context);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
entity.onRemove.dispatch();
|
|
282
|
+
entity.onRemove.detachAll();
|
|
283
|
+
for (const system of this.onRemoveEntitySet) {
|
|
284
|
+
system.onRemoveEntity(entity);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
resolve();
|
|
288
|
+
}
|
|
289
|
+
this.removeQueue = [];
|
|
290
|
+
for (const [entity, resolve] of this.addQueue) {
|
|
291
|
+
const preparePromises = [];
|
|
292
|
+
const nextSystemContexts = [];
|
|
293
|
+
const entities = entity.getAllEntities();
|
|
294
|
+
for (const entity of entities) {
|
|
295
|
+
if (entity.gameEngine)
|
|
296
|
+
throw new Error('Entity already added to a gameEngine');
|
|
297
|
+
entity.gameEngine = this;
|
|
298
|
+
const systemContexts = this.injector.createSystemsForEntity(entity);
|
|
299
|
+
for (const context of systemContexts) {
|
|
300
|
+
context.system.onPrepare && preparePromises.push(context.system.onPrepare());
|
|
301
|
+
nextSystemContexts.push(context);
|
|
302
|
+
}
|
|
303
|
+
this.entitySystemMap.set(entity, systemContexts);
|
|
304
|
+
}
|
|
305
|
+
const entityWasAdded = () => {
|
|
306
|
+
for (const system of this.onAddEntitySet) {
|
|
307
|
+
system.onAddEntity(entity);
|
|
308
|
+
}
|
|
309
|
+
resolve();
|
|
310
|
+
};
|
|
311
|
+
if (preparePromises.length === 0) {
|
|
312
|
+
this.startSystems(nextSystemContexts, entityWasAdded);
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
Promise.all(preparePromises).then(() => this.startSystems(nextSystemContexts, entityWasAdded));
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
this.addQueue = [];
|
|
319
|
+
this.resolveFrame();
|
|
320
|
+
this.framePromise = new Promise(resolve => {
|
|
321
|
+
this.resolveFrame = resolve;
|
|
322
|
+
});
|
|
323
|
+
this.onPrepare.dispatch();
|
|
324
|
+
}
|
|
325
|
+
startSystems(contexts, then) {
|
|
326
|
+
for (const context of contexts) {
|
|
327
|
+
const system = context.system;
|
|
328
|
+
this.mapAnnotatedSignals(system);
|
|
329
|
+
system.onStart && system.onStart();
|
|
330
|
+
system.onPreFixedUpdate && this.onPreFixedUpdateSet.add(system);
|
|
331
|
+
system.onFixedUpdate && this.onFixedUpdateSet.add(system);
|
|
332
|
+
system.onUpdate && this.onUpdateSet.add(system);
|
|
333
|
+
system.onLateUpdate && this.onLateUpdateSet.add(system);
|
|
334
|
+
system.onPhysicsUpdate && this.onPhysicsUpdateSet.add(system);
|
|
335
|
+
system.onPrePhysicsUpdate && this.onPrePhysicsUpdateSet.add(system);
|
|
336
|
+
system.onPostPhysicsUpdate && this.onPostPhysicsUpdateSet.add(system);
|
|
337
|
+
system.onLateFixedUpdate && this.onLateFixedUpdateSet.add(system);
|
|
338
|
+
system.onRender && this.onRenderUpdateSet.add(system);
|
|
339
|
+
system.onAddEntity && this.onAddEntitySet.add(system);
|
|
340
|
+
system.onRemoveEntity && this.onRemoveEntitySet.add(system);
|
|
341
|
+
}
|
|
342
|
+
then === null || then === void 0 ? void 0 : then();
|
|
343
|
+
}
|
|
344
|
+
;
|
|
345
|
+
disposeContext(context) {
|
|
346
|
+
const system = context.system;
|
|
347
|
+
const onEndPromise = system.onEnd && system.onEnd();
|
|
348
|
+
if (onEndPromise instanceof Promise) {
|
|
349
|
+
onEndPromise.then(() => {
|
|
350
|
+
context.dispose();
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
context.dispose();
|
|
355
|
+
}
|
|
356
|
+
this.unmapAnnotatedSignals(system);
|
|
357
|
+
system.onPreFixedUpdate && this.onPreFixedUpdateSet.delete(system);
|
|
358
|
+
system.onFixedUpdate && this.onFixedUpdateSet.delete(system);
|
|
359
|
+
system.onUpdate && this.onUpdateSet.delete(system);
|
|
360
|
+
system.onLateUpdate && this.onLateUpdateSet.delete(system);
|
|
361
|
+
system.onPhysicsUpdate && this.onPhysicsUpdateSet.delete(system);
|
|
362
|
+
system.onPrePhysicsUpdate && this.onPrePhysicsUpdateSet.delete(system);
|
|
363
|
+
system.onPostPhysicsUpdate && this.onPostPhysicsUpdateSet.delete(system);
|
|
364
|
+
system.onLateFixedUpdate && this.onLateFixedUpdateSet.delete(system);
|
|
365
|
+
system.onRender && this.onRenderUpdateSet.delete(system);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
exports.GameEngine = GameEngine;
|
|
317
369
|
//# sourceMappingURL=gameEngine.js.map
|