mani-game-engine 1.0.0-pre.8 → 1.0.0-pre.80
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 +1 -1
- package/lib/clock.d.ts +63 -0
- package/lib/clock.js +147 -0
- package/lib/clock.js.map +1 -0
- package/lib/ecsInjector.d.ts +45 -0
- package/lib/ecsInjector.js +220 -0
- package/lib/ecsInjector.js.map +1 -0
- package/lib/entity.d.ts +40 -0
- package/lib/entity.js +194 -0
- package/lib/entity.js.map +1 -0
- package/lib/gameEngine.d.ts +96 -0
- package/lib/gameEngine.js +445 -0
- package/lib/gameEngine.js.map +1 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +37 -0
- package/lib/index.js.map +1 -0
- package/lib/injector.d.ts +125 -0
- package/lib/injector.js +305 -0
- package/lib/injector.js.map +1 -0
- package/lib/scope/scopeContext.d.ts +76 -0
- package/lib/scope/scopeContext.js +306 -0
- package/lib/scope/scopeContext.js.map +1 -0
- package/lib/systemContext.d.ts +18 -0
- package/lib/systemContext.js +54 -0
- package/lib/systemContext.js.map +1 -0
- package/lib/types.d.ts +80 -0
- package/lib/types.js +20 -0
- package/lib/types.js.map +1 -0
- package/lib/utils/map2k.d.ts +6 -0
- package/lib/utils/map2k.js +44 -0
- package/lib/utils/map2k.js.map +1 -0
- package/package.json +12 -15
- package/src/clock.ts +163 -82
- package/src/ecsInjector.ts +131 -36
- package/src/entity.ts +126 -6
- package/src/gameEngine.ts +492 -364
- package/src/index.ts +24 -9
- package/src/injector.ts +410 -0
- package/src/scope/scopeContext.ts +122 -35
- package/src/systemContext.ts +50 -14
- package/src/types.ts +20 -10
package/src/gameEngine.ts
CHANGED
|
@@ -1,413 +1,541 @@
|
|
|
1
1
|
import {Action, Class, EngineSignals, GameEnginePlugin, OnFixedUpdateParams, OnUpdateParams, Service, System} from './types';
|
|
2
2
|
import {Signal, SignalBinding} from 'mani-signal';
|
|
3
3
|
import {Entity} from './entity';
|
|
4
|
-
import {ID, putIfAbsent} from 'mani-injector';
|
|
5
4
|
import {EcsInjector, signalHandlers, staticSignalHandlers} from './ecsInjector';
|
|
6
5
|
import {Clock, GameClock} from './clock';
|
|
7
6
|
import {SystemContext} from './systemContext';
|
|
7
|
+
import {ID, putIfAbsent} from './injector';
|
|
8
8
|
|
|
9
9
|
type AddEntry = [Entity, () => void];
|
|
10
10
|
type RemoveEntry = [Entity, () => void];
|
|
11
11
|
|
|
12
12
|
const defaultOptions = {
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
fixedDeltaTime: 1 / 60,
|
|
14
|
+
plugins: [] as GameEnginePlugin[],
|
|
15
15
|
};
|
|
16
16
|
|
|
17
17
|
export type GameEngineOptions = Partial<typeof defaultOptions>;
|
|
18
18
|
declare global {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
interface GameEngineActions {
|
|
20
|
+
'testAction': string;
|
|
21
|
+
}
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
type GameEngineActionParams<K extends keyof GameEngineActions> = GameEngineActions[K];
|
|
25
25
|
type GameAction<K extends keyof GameEngineActions> = (engine: GameEngine, params: GameEngineActionParams<K>) => Promise<any> | any;
|
|
26
26
|
|
|
27
|
+
let id = 0;
|
|
27
28
|
export class GameEngine {
|
|
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
|
-
|
|
29
|
+
readonly id = id++;
|
|
30
|
+
readonly onPrepare: Signal;
|
|
31
|
+
readonly onStart: Signal;
|
|
32
|
+
readonly onEnd: Signal;
|
|
33
|
+
readonly onRender: Signal<OnUpdateParams>;
|
|
34
|
+
readonly onUpdate: Signal<OnUpdateParams>;
|
|
35
|
+
readonly onFixedUpdate: Signal<OnFixedUpdateParams>;
|
|
36
|
+
readonly onPrePhysicsUpdate: Signal<OnFixedUpdateParams>;
|
|
37
|
+
readonly onPhysicsUpdate: Signal<OnFixedUpdateParams>;
|
|
38
|
+
readonly onPostPhysicsUpdate: Signal<OnFixedUpdateParams>;
|
|
39
|
+
readonly onLateFixedUpdate: Signal<OnFixedUpdateParams>;
|
|
40
|
+
readonly onLateUpdate: Signal<OnUpdateParams>;
|
|
41
|
+
readonly onAddEntity: Signal<Entity>;
|
|
42
|
+
readonly onRemoveEntity: Signal<Entity>;
|
|
43
|
+
|
|
44
|
+
private readonly onEarlyUpdateSet = new Set<System>();
|
|
45
|
+
private readonly onUpdateSet = new Set<System>();
|
|
46
|
+
private readonly onFixedUpdateSet = new Set<System>();
|
|
47
|
+
private readonly onPreFixedUpdateSet = new Set<System>();
|
|
48
|
+
private readonly onPhysicsUpdateSet = new Set<System>();
|
|
49
|
+
private readonly onPrePhysicsUpdateSet = new Set<System>();
|
|
50
|
+
private readonly onPostPhysicsUpdateSet = new Set<System>();
|
|
51
|
+
private readonly onLateFixedUpdateSet = new Set<System>();
|
|
52
|
+
private readonly onLateUpdateSet = new Set<System>();
|
|
53
|
+
private readonly onRenderUpdateSet = new Set<System>();
|
|
54
|
+
private readonly onAddEntitySet = new Set<System>();
|
|
55
|
+
private readonly onRemoveEntitySet = new Set<System>();
|
|
56
|
+
|
|
57
|
+
// TODO: for performance, we could store the systems directly in the entity
|
|
58
|
+
private readonly entitySystemMap = new Map<Entity, Set<SystemContext>>();
|
|
59
|
+
|
|
60
|
+
private readonly registeredServices = new Set<Class<Service>>();
|
|
61
|
+
private readonly services: Service[] = [];
|
|
62
|
+
private readonly systemChildInjectors = new Map<object, EcsInjector>();
|
|
63
|
+
|
|
64
|
+
private framePromise!: Promise<any>;
|
|
65
|
+
private resolveFrame: () => void = () => undefined;
|
|
66
|
+
|
|
67
|
+
private addQueue: AddEntry[] = [];
|
|
68
|
+
private removeQueue: RemoveEntry[] = [];
|
|
69
|
+
|
|
70
|
+
private _started = false;
|
|
71
|
+
|
|
72
|
+
get started(): boolean { return this._started; }
|
|
73
|
+
|
|
74
|
+
private _frameCount = 0;
|
|
75
|
+
get frameCount(): number { return this._frameCount; }
|
|
76
|
+
|
|
77
|
+
private _fixedFrameCount = 0;
|
|
78
|
+
get fixedFrameCount(): number { return this._fixedFrameCount; }
|
|
79
|
+
|
|
80
|
+
get numEntities(): number { return this.entitySystemMap.size;}
|
|
70
81
|
|
|
71
82
|
// TODO inject clock into game engine
|
|
72
83
|
// TODO inject space into game engine
|
|
73
84
|
|
|
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
|
-
|
|
85
|
+
constructor(
|
|
86
|
+
readonly injector: EcsInjector<Class<System>>,
|
|
87
|
+
readonly clock: Clock,
|
|
88
|
+
) {
|
|
89
|
+
this.injector.map(GameEngine).toValue(this);
|
|
90
|
+
|
|
91
|
+
this.onPrepare = this.injector.getSignal(EngineSignals.OnPrepare);
|
|
92
|
+
this.onStart = this.injector.getSignal(EngineSignals.OnStart);
|
|
93
|
+
this.onEnd = this.injector.getSignal(EngineSignals.OnEnd);
|
|
94
|
+
this.onRender = this.injector.getSignal(EngineSignals.OnRender);
|
|
95
|
+
this.onFixedUpdate = this.injector.getSignal(EngineSignals.OnFixedUpdate);
|
|
96
|
+
this.onUpdate = this.injector.getSignal(EngineSignals.OnUpdate);
|
|
97
|
+
this.onLateUpdate = this.injector.getSignal(EngineSignals.OnLateUpdate);
|
|
98
|
+
this.onPrePhysicsUpdate = this.injector.getSignal(EngineSignals.OnPrePhysicsUpdate);
|
|
99
|
+
this.onPhysicsUpdate = this.injector.getSignal(EngineSignals.OnPhysicsUpdate);
|
|
100
|
+
this.onPostPhysicsUpdate = this.injector.getSignal(EngineSignals.OnPostPhysicsUpdate);
|
|
101
|
+
this.onLateFixedUpdate = this.injector.getSignal(EngineSignals.onLateFixedUpdate);
|
|
102
|
+
|
|
103
|
+
this.onAddEntity = this.injector.getSignal(EngineSignals.OnAddEntity);
|
|
104
|
+
this.onRemoveEntity = this.injector.getSignal(EngineSignals.OnRemoveEntity);
|
|
105
|
+
|
|
106
|
+
this.setupClock();
|
|
107
|
+
this.prepareFrame();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
static createDefault(options?: GameEngineOptions) {
|
|
111
|
+
const opts = {...defaultOptions, ...options};
|
|
112
|
+
const clock = new GameClock({
|
|
113
|
+
fixedDeltaTime: opts.fixedDeltaTime,
|
|
114
|
+
autoStart: false,
|
|
115
|
+
});
|
|
116
|
+
const injector = new EcsInjector<Class<System>>();
|
|
117
|
+
const engine = new GameEngine(injector, clock);
|
|
118
|
+
engine.installPlugins(opts.plugins);
|
|
119
|
+
return engine;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
static createDefaultWithInjector(injector: EcsInjector<Class<System>>, options?: GameEngineOptions) {
|
|
123
|
+
const opts = {...defaultOptions, ...options};
|
|
124
|
+
const clock = new GameClock({
|
|
125
|
+
fixedDeltaTime: opts.fixedDeltaTime,
|
|
126
|
+
autoStart: false,
|
|
127
|
+
});
|
|
128
|
+
const engine = new GameEngine(injector, clock);
|
|
129
|
+
engine.installPlugins(opts.plugins);
|
|
130
|
+
return engine;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// TODO: test
|
|
134
|
+
addServiceClass<T extends Class<Service>>(serviceClass: T, mapping?: (injector: EcsInjector) => void): InstanceType<T> {
|
|
135
|
+
let childInjector: EcsInjector | undefined;
|
|
136
|
+
if (mapping) {
|
|
137
|
+
childInjector = this.injector.createChild();
|
|
138
|
+
mapping(childInjector);
|
|
103
139
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
autoStart: false,
|
|
110
|
-
});
|
|
111
|
-
const engine = new GameEngine(injector, clock);
|
|
112
|
-
engine.installPlugins(opts.plugins);
|
|
113
|
-
return engine;
|
|
140
|
+
this.mapStaticAnnotatedSignals(serviceClass);
|
|
141
|
+
const service = childInjector?.createInstance(serviceClass) || this.injector.createInstance(serviceClass);
|
|
142
|
+
this.addService(service);
|
|
143
|
+
if (childInjector) {
|
|
144
|
+
this.systemChildInjectors.set(service, childInjector);
|
|
114
145
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
146
|
+
return service;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private addService(service: Service): void {
|
|
150
|
+
this.mapAnnotatedSignals(service);
|
|
151
|
+
service.onStart && service.onStart();
|
|
152
|
+
service.onPreFixedUpdate && this.onPreFixedUpdateSet.add(service);
|
|
153
|
+
service.onFixedUpdate && this.onFixedUpdateSet.add(service);
|
|
154
|
+
service.onUpdate && this.onUpdateSet.add(service);
|
|
155
|
+
service.onLateUpdate && this.onLateUpdateSet.add(service);
|
|
156
|
+
service.onPrePhysicsUpdate && this.onPrePhysicsUpdateSet.add(service);
|
|
157
|
+
service.onPhysicsUpdate && this.onPhysicsUpdateSet.add(service);
|
|
158
|
+
service.onPostPhysicsUpdate && this.onPostPhysicsUpdateSet.add(service);
|
|
159
|
+
service.onLateFixedUpdate && this.onLateFixedUpdateSet.add(service);
|
|
160
|
+
service.onRender && this.onRenderUpdateSet.add(service);
|
|
161
|
+
service.onAddEntity && this.onAddEntitySet.add(service);
|
|
162
|
+
service.onRemoveEntity && this.onRemoveEntitySet.add(service);
|
|
163
|
+
this.services.push(service);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
removeService(service: Service): void {
|
|
167
|
+
const index = this.services.indexOf(service);
|
|
168
|
+
if (index === -1) return;
|
|
169
|
+
this.services.splice(index, 1);
|
|
170
|
+
this.unmapAnnotatedSignals(service);
|
|
171
|
+
service.onPreFixedUpdate && this.onPreFixedUpdateSet.delete(service);
|
|
172
|
+
service.onFixedUpdate && this.onFixedUpdateSet.delete(service);
|
|
173
|
+
service.onUpdate && this.onUpdateSet.delete(service);
|
|
174
|
+
service.onLateUpdate && this.onLateUpdateSet.delete(service);
|
|
175
|
+
service.onPrePhysicsUpdate && this.onPrePhysicsUpdateSet.delete(service);
|
|
176
|
+
service.onPhysicsUpdate && this.onPhysicsUpdateSet.delete(service);
|
|
177
|
+
service.onPostPhysicsUpdate && this.onPostPhysicsUpdateSet.delete(service);
|
|
178
|
+
service.onLateFixedUpdate && this.onLateFixedUpdateSet.delete(service);
|
|
179
|
+
service.onRender && this.onRenderUpdateSet.delete(service);
|
|
180
|
+
service.onAddEntity && this.onAddEntitySet.delete(service);
|
|
181
|
+
service.onRemoveEntity && this.onRemoveEntitySet.delete(service);
|
|
182
|
+
service.onEnd && service.onEnd();
|
|
183
|
+
const childInjector = this.systemChildInjectors.get(service);
|
|
184
|
+
if (childInjector) {
|
|
185
|
+
childInjector.dispose();
|
|
186
|
+
this.systemChildInjectors.delete(service);
|
|
128
187
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
installPlugins(plugins: GameEnginePlugin[]) {
|
|
191
|
+
plugins.forEach(plugin => {
|
|
192
|
+
plugin.onPrepare && plugin.onPrepare(this);
|
|
193
|
+
});
|
|
194
|
+
plugins.forEach(plugin => {
|
|
195
|
+
plugin.onStart && plugin.onStart(this);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
registerSystem(systemClass: Class<System>): void {
|
|
200
|
+
this.mapStaticAnnotatedSignals(systemClass);
|
|
201
|
+
if (this._started) throw new Error('Register Systems before engine is started');
|
|
202
|
+
this.injector.registerSystem(systemClass);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
registerService(serviceClass: Class<Service>): void {
|
|
206
|
+
this.mapStaticAnnotatedSignals(serviceClass);
|
|
207
|
+
if (this._started) throw new Error('Register Services before engine is started');
|
|
208
|
+
this.injector.map(serviceClass).toSingleton();
|
|
209
|
+
this.registeredServices.add(serviceClass);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
getService<T extends Class<Service>>(serviceClass: T): InstanceType<T> {
|
|
213
|
+
return this.injector.get(serviceClass);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
start() {
|
|
217
|
+
this._started = true;
|
|
218
|
+
//TODO: throw when registering services or systems when engine is running
|
|
219
|
+
for (const serviceClass of this.registeredServices) {
|
|
220
|
+
const service = this.injector.get(serviceClass);
|
|
221
|
+
this.addService(service);
|
|
137
222
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
223
|
+
this.clock.start();
|
|
224
|
+
this.onStart.dispatch();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private setupClock() {
|
|
228
|
+
this.clock.onPrepare.add(this.prepareFrame, {context: this});
|
|
229
|
+
|
|
230
|
+
this.clock.onEarlyUpdate.add(({time, deltaTime}) => {
|
|
231
|
+
for (const system of this.onEarlyUpdateSet) {
|
|
232
|
+
system.onEarlyUpdate!(time, deltaTime);
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
this.clock.onFixedUpdate.add((params) => {
|
|
237
|
+
const {time, deltaTime} = params;
|
|
238
|
+
for (const system of this.onPreFixedUpdateSet) {
|
|
239
|
+
system.onPreFixedUpdate!(time, deltaTime);
|
|
240
|
+
}
|
|
241
|
+
this.onFixedUpdate.dispatch(params);
|
|
242
|
+
for (const system of this.onFixedUpdateSet) {
|
|
243
|
+
system.onFixedUpdate!(time, deltaTime);
|
|
244
|
+
}
|
|
245
|
+
this.onPhysicsUpdate.dispatch(params);
|
|
246
|
+
for (const system of this.onPhysicsUpdateSet) {
|
|
247
|
+
system.onPhysicsUpdate!(time, deltaTime);
|
|
248
|
+
}
|
|
249
|
+
this.onPostPhysicsUpdate.dispatch(params);
|
|
250
|
+
for (const system of this.onPostPhysicsUpdateSet) {
|
|
251
|
+
system.onPostPhysicsUpdate!(time, deltaTime);
|
|
252
|
+
}
|
|
253
|
+
this.onLateFixedUpdate.dispatch(params);
|
|
254
|
+
for (const system of this.onLateFixedUpdateSet) {
|
|
255
|
+
system.onLateFixedUpdate!(time, deltaTime);
|
|
256
|
+
}
|
|
257
|
+
this._fixedFrameCount++;
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// TODO: update from outside
|
|
261
|
+
this.clock.onUpdate.add(onUpdateParams => {
|
|
262
|
+
const {time, deltaTime, alpha} = onUpdateParams;
|
|
263
|
+
this.onUpdate.dispatch(onUpdateParams);
|
|
264
|
+
for (const system of this.onUpdateSet) {
|
|
265
|
+
system.onUpdate!(time, deltaTime, alpha);
|
|
266
|
+
}
|
|
267
|
+
//TODO: merge with framerate to single signal?
|
|
268
|
+
this.onLateUpdate.dispatch(onUpdateParams);
|
|
269
|
+
for (const system of this.onLateUpdateSet) {
|
|
270
|
+
system.onLateUpdate!(time, deltaTime, alpha);
|
|
271
|
+
}
|
|
272
|
+
this.onRender.dispatch(onUpdateParams);
|
|
273
|
+
for (const system of this.onRenderUpdateSet) {
|
|
274
|
+
system.onRender!(time, deltaTime, alpha);
|
|
275
|
+
}
|
|
276
|
+
this._frameCount++;
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
add(entity: Entity) {
|
|
281
|
+
return new Promise<void>(resolve => this.addQueue.push([entity, resolve]));
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
addAll(entity: Entity[]) {
|
|
285
|
+
return Promise.all(entity.map(entity => new Promise<void>(resolve => this.addQueue.push([entity, resolve]))));
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
waitFrame() {
|
|
289
|
+
return this.framePromise;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
async remove(entity: Entity) {
|
|
293
|
+
if ((<any>entity).markRemoval) return;
|
|
294
|
+
(<any>entity).markRemoval = true;
|
|
295
|
+
return new Promise<void>(resolve => this.removeQueue.push([entity, resolve]));
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
async removeAll(entities: Entity[]) {
|
|
299
|
+
return Promise.all(entities.map(entity => this.remove(entity)));
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
stop() {
|
|
303
|
+
// this.injector.dispose();
|
|
304
|
+
this.clock.stop();
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
runAction<T extends Action>(action: T): ReturnType<T> {
|
|
308
|
+
return action(this);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
getSignal<T>(signalId: ID): Signal<T> {
|
|
312
|
+
return this.injector.getSignal<T>(signalId);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
private signalMappings = new Map<Class, [string, Signal][]>();
|
|
316
|
+
private staticSignalMappings = new Map<Class, [string, Signal][]>();
|
|
317
|
+
private signalMapBindings = new Map<Object, SignalBinding[]>();
|
|
318
|
+
|
|
319
|
+
// TODO: staticSignalMapBindings needed if we want to unregister Services or Systems
|
|
320
|
+
|
|
321
|
+
private mapStaticAnnotatedSignals(target: Class) {
|
|
322
|
+
let staticSignalMap = this.getStaticSignalMap(target);
|
|
323
|
+
if (staticSignalMap.length === 0) return;
|
|
324
|
+
|
|
325
|
+
const bindings = [];
|
|
326
|
+
for (const [key, signal] of staticSignalMap) {
|
|
327
|
+
bindings.push(signal.add((<any>target)[key], {context: target}));
|
|
143
328
|
}
|
|
329
|
+
// TODO: ignore bindings for now (store them if we implement unregister Service or Systems
|
|
330
|
+
}
|
|
144
331
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
this.injector.map(serviceClass).toSingleton();
|
|
149
|
-
this.serviceClasses.add(serviceClass);
|
|
150
|
-
}
|
|
332
|
+
private mapAnnotatedSignals(target: Object) {
|
|
333
|
+
const signalMap = this.getSignalMap(target.constructor);
|
|
334
|
+
if (signalMap.length === 0) return;
|
|
151
335
|
|
|
152
|
-
|
|
153
|
-
|
|
336
|
+
const bindings = [];
|
|
337
|
+
for (const [key, signal] of signalMap) {
|
|
338
|
+
bindings.push(signal.add((<any>target)[key], {context: target}));
|
|
154
339
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
this.clock.start();
|
|
164
|
-
this.onStart.dispatch();
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
private setupClock() {
|
|
168
|
-
this.clock.onPrepare.add(this.prepareFrame, this);
|
|
169
|
-
|
|
170
|
-
this.clock.onEarlyUpdate.add(({time, deltaTime}) => {
|
|
171
|
-
for (const system of this.onEarlyUpdateSet) {
|
|
172
|
-
system.onEarlyUpdate!(time, deltaTime);
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
this.clock.onFixedUpdate.add((params) => {
|
|
177
|
-
const {time, deltaTime} = params;
|
|
178
|
-
for (const system of this.onPreFixedUpdateSet) {
|
|
179
|
-
system.onPreFixedUpdate!();
|
|
180
|
-
}
|
|
181
|
-
for (const system of this.onFixedUpdateSet) {
|
|
182
|
-
system.onFixedUpdate!(time, deltaTime);
|
|
183
|
-
}
|
|
184
|
-
this.onFixedUpdate.dispatch(params);
|
|
185
|
-
for (const system of this.onPhysicsUpdateSet) {
|
|
186
|
-
system.onPhysicsUpdate!(time, deltaTime);
|
|
187
|
-
}
|
|
188
|
-
this.onPostPhysics.dispatch(params);
|
|
189
|
-
for (const system of this.onPostPhysicsUpdateSet) {
|
|
190
|
-
system.onPostPhysicsUpdate!(time, deltaTime);
|
|
191
|
-
}
|
|
192
|
-
this.onLateFixedUpdate.dispatch(params);
|
|
193
|
-
for (const system of this.onLateFixedUpdateSet) {
|
|
194
|
-
system.onLateFixedUpdate!(time, deltaTime);
|
|
195
|
-
}
|
|
196
|
-
this._fixedFrameCount++;
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
// TODO: update from outside
|
|
200
|
-
this.clock.onUpdate.add(onUpdateParams => {
|
|
201
|
-
const {time, deltaTime, alpha} = onUpdateParams;
|
|
202
|
-
for (const system of this.onUpdateSet) {
|
|
203
|
-
system.onUpdate!(time, deltaTime, alpha);
|
|
204
|
-
}
|
|
205
|
-
//TODO: merge with framerate to single signal?
|
|
206
|
-
|
|
207
|
-
this.onUpdate.dispatch(onUpdateParams);
|
|
208
|
-
for (const system of this.onLateUpdateSet) {
|
|
209
|
-
system.onLateUpdate!(time, deltaTime, alpha);
|
|
210
|
-
}
|
|
211
|
-
this.onLateUpdate.dispatch(onUpdateParams);
|
|
212
|
-
|
|
213
|
-
for (const system of this.onRenderUpdateSet) {
|
|
214
|
-
system.onRender!(time, deltaTime, alpha);
|
|
215
|
-
}
|
|
216
|
-
this.onRender.dispatch(onUpdateParams);
|
|
217
|
-
this._frameCount++;
|
|
218
|
-
});
|
|
340
|
+
this.signalMapBindings.set(target, bindings);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
private unmapAnnotatedSignals(target: Object) {
|
|
344
|
+
const signalBindings = this.signalMapBindings.get(target);
|
|
345
|
+
if (!signalBindings) return;
|
|
346
|
+
for (const binding of signalBindings) {
|
|
347
|
+
binding.detach();
|
|
219
348
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
private getSignalMap(target: Function) {
|
|
352
|
+
// TODO: we could speed thinks up by introducing "invisible" flags on the systems/services to avoid a map check
|
|
353
|
+
return putIfAbsent(this.signalMappings, target, (): [string, Signal][] => {
|
|
354
|
+
const handlers = signalHandlers.get(target);
|
|
355
|
+
if (!handlers) return [];
|
|
356
|
+
const result: [string, Signal][] = [];
|
|
357
|
+
for (const [key, signalId] of handlers) {
|
|
358
|
+
result.push([key, this.getSignal(signalId)]);
|
|
359
|
+
}
|
|
360
|
+
return result;
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
private getStaticSignalMap(target: Class) {
|
|
365
|
+
// TODO: we could speed thinks up by introducing "invisible" flags on the systems/services to avoid a map check
|
|
366
|
+
return putIfAbsent(this.staticSignalMappings, target, (): [string, Signal][] => {
|
|
367
|
+
const handlers = staticSignalHandlers.get(target);
|
|
368
|
+
if (!handlers) return [];
|
|
369
|
+
const result: [string, Signal][] = [];
|
|
370
|
+
for (const [key, signalId] of handlers) {
|
|
371
|
+
result.push([key, this.getSignal(signalId)]);
|
|
372
|
+
}
|
|
373
|
+
return result;
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
dispose() {
|
|
378
|
+
this.clock.stop();
|
|
379
|
+
this.clock.dispose();
|
|
380
|
+
for (const systems of this.entitySystemMap.values()) {
|
|
381
|
+
for (const context of systems) {
|
|
382
|
+
context.system.onEnd && context.system.onEnd();
|
|
383
|
+
this.unmapAnnotatedSignals(context.system);
|
|
384
|
+
}
|
|
223
385
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
386
|
+
for (const service of this.services) {
|
|
387
|
+
service.onEnd && service.onEnd();
|
|
388
|
+
this.unmapAnnotatedSignals(service);
|
|
227
389
|
}
|
|
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
|
-
|
|
390
|
+
this.onEnd.dispatch();
|
|
391
|
+
|
|
392
|
+
this.onAddEntity.detachAll();
|
|
393
|
+
this.onRemoveEntity.detachAll();
|
|
394
|
+
this.onPrepare.detachAll();
|
|
395
|
+
this.onStart.detachAll();
|
|
396
|
+
this.onEnd.detachAll();
|
|
397
|
+
this.onRender.detachAll();
|
|
398
|
+
this.onUpdate.detachAll();
|
|
399
|
+
this.onFixedUpdate.detachAll();
|
|
400
|
+
this.onPrePhysicsUpdate.detachAll();
|
|
401
|
+
this.onPhysicsUpdate.detachAll();
|
|
402
|
+
this.onPostPhysicsUpdate.detachAll();
|
|
403
|
+
this.onLateFixedUpdate.detachAll();
|
|
404
|
+
this.onPhysicsUpdateSet.clear();
|
|
405
|
+
this.onPrePhysicsUpdateSet.clear();
|
|
406
|
+
this.onPostPhysicsUpdateSet.clear();
|
|
407
|
+
this.onLateFixedUpdateSet.clear();
|
|
408
|
+
this.onLateUpdate.detachAll();
|
|
409
|
+
this.onUpdate.detachAll();
|
|
410
|
+
this.onFixedUpdate.detachAll();
|
|
411
|
+
this.onRender.detachAll();
|
|
412
|
+
this.onAddEntity.detachAll();
|
|
413
|
+
this.onRemoveEntity.detachAll();
|
|
414
|
+
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
private prepareFrame() {
|
|
418
|
+
for (const [entity, resolve] of this.removeQueue) {
|
|
419
|
+
const entities = entity.getAllEntities();
|
|
420
|
+
for (const entity of entities) {
|
|
421
|
+
(<any>entity).markRemoval = false;
|
|
422
|
+
if (!(<any>entity).gameEngine) {
|
|
423
|
+
console.log('Entity not active', entity);
|
|
424
|
+
// throw new Error('Entity not active');
|
|
261
425
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
for (const [key, signal] of signalMap) {
|
|
271
|
-
bindings.push(signal.add((<any>target)[key], target));
|
|
426
|
+
(<any>entity).gameEngine = undefined;
|
|
427
|
+
|
|
428
|
+
const systemContexts = this.entitySystemMap.get(entity);
|
|
429
|
+
this.entitySystemMap.delete(entity);
|
|
430
|
+
if (systemContexts) {
|
|
431
|
+
for (const context of systemContexts) {
|
|
432
|
+
this.disposeContext(context);
|
|
433
|
+
}
|
|
272
434
|
}
|
|
273
|
-
|
|
274
|
-
|
|
435
|
+
entity.onRemove.dispatch();
|
|
436
|
+
entity.onRemove.detachAll();
|
|
437
|
+
entity._signalMap.forEach(signal => signal.detachAll());
|
|
275
438
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
if (!signalBindings) return;
|
|
279
|
-
for (const binding of signalBindings) {
|
|
280
|
-
binding.detach();
|
|
439
|
+
for (const system of this.onRemoveEntitySet) {
|
|
440
|
+
system.onRemoveEntity!(entity);
|
|
281
441
|
}
|
|
282
|
-
|
|
442
|
+
}
|
|
283
443
|
|
|
284
|
-
|
|
285
|
-
// TODO: we could speed thinks up by introducing "invisible" flags on the systems/services to avoid a map check
|
|
286
|
-
return putIfAbsent(this.signalMappings, target, (): [string, Signal][] => {
|
|
287
|
-
const handlers = signalHandlers.get(target);
|
|
288
|
-
if (!handlers) return [];
|
|
289
|
-
const result: [string, Signal][] = [];
|
|
290
|
-
for (const [key, signalId] of handlers) {
|
|
291
|
-
result.push([key, this.getSignal(signalId)]);
|
|
292
|
-
}
|
|
293
|
-
return result;
|
|
294
|
-
});
|
|
444
|
+
resolve();
|
|
295
445
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
dispose() {
|
|
311
|
-
this.clock.stop();
|
|
312
|
-
for (const systems of this.entitySystemMap.values()) {
|
|
313
|
-
for (const system of systems) {
|
|
314
|
-
system.system.onEnd && system.system.onEnd();
|
|
315
|
-
}
|
|
446
|
+
this.removeQueue = [];
|
|
447
|
+
|
|
448
|
+
for (const [entity, resolve] of this.addQueue) {
|
|
449
|
+
const preparePromises = [];
|
|
450
|
+
const nextSystemContexts: SystemContext[] = [];
|
|
451
|
+
const entities = entity.getAllEntities();
|
|
452
|
+
for (const entity of entities) {
|
|
453
|
+
if ((<any>entity).gameEngine) throw new Error('Entity already added to a gameEngine');
|
|
454
|
+
(<any>entity).gameEngine = this;
|
|
455
|
+
const systemContexts = this.injector.createSystemsForEntity(entity);
|
|
456
|
+
for (const context of systemContexts) {
|
|
457
|
+
// TODO: implement synced mode where async onPrepares aren't allowed
|
|
458
|
+
context.system.onPrepare && preparePromises.push(context.system.onPrepare());
|
|
459
|
+
nextSystemContexts.push(context);
|
|
316
460
|
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
}
|
|
320
|
-
}
|
|
461
|
+
this.entitySystemMap.set(entity, systemContexts);
|
|
462
|
+
}
|
|
321
463
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
const entities = entity.getAllEntities();
|
|
326
|
-
for (const entity of entities) {
|
|
327
|
-
(<any>entity).markRemoval = false;
|
|
328
|
-
if (!(<any>entity).gameEngine) {
|
|
329
|
-
console.log(entity);
|
|
330
|
-
// throw new Error('Entity not active');
|
|
331
|
-
}
|
|
332
|
-
(<any>entity).gameEngine = undefined;
|
|
333
|
-
|
|
334
|
-
const systemContexts = this.entitySystemMap.get(entity);
|
|
335
|
-
this.entitySystemMap.delete(entity);
|
|
336
|
-
if (systemContexts) {
|
|
337
|
-
for (const context of systemContexts) {
|
|
338
|
-
context.dispose();
|
|
339
|
-
const system = context.system;
|
|
340
|
-
this.unmapAnnotatedSignals(system);
|
|
341
|
-
system.onEnd && system.onEnd();
|
|
342
|
-
system.onPreFixedUpdate && this.onPreFixedUpdateSet.delete(system);
|
|
343
|
-
system.onFixedUpdate && this.onFixedUpdateSet.delete(system);
|
|
344
|
-
system.onEarlyUpdate && this.onEarlyUpdateSet.delete(system);
|
|
345
|
-
system.onUpdate && this.onUpdateSet.delete(system);
|
|
346
|
-
system.onLateUpdate && this.onLateUpdateSet.delete(system);
|
|
347
|
-
system.onPhysicsUpdate && this.onPhysicsUpdateSet.delete(system);
|
|
348
|
-
system.onPostPhysicsUpdate && this.onPostPhysicsUpdateSet.delete(system);
|
|
349
|
-
system.onLateFixedUpdate && this.onLateFixedUpdateSet.delete(system);
|
|
350
|
-
system.onRender && this.onRenderUpdateSet.delete(system);
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
entity.onRemove.dispatch();
|
|
354
|
-
entity.onRemove.detachAll();
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
resolve();
|
|
358
|
-
}
|
|
359
|
-
this.removeQueue = [];
|
|
360
|
-
|
|
361
|
-
for (const [entity, resolve] of this.addQueue) {
|
|
362
|
-
const preparePromises = [];
|
|
363
|
-
const nexSystemContexts: SystemContext[] = [];
|
|
364
|
-
const entities = entity.getAllEntities();
|
|
365
|
-
for (const entity of entities) {
|
|
366
|
-
if ((<any>entity).gameEngine) throw new Error('Entity already added to a gameEngine');
|
|
367
|
-
(<any>entity).gameEngine = this;
|
|
368
|
-
const systemContexts = this.injector.createSystems(entity);
|
|
369
|
-
for (const context of systemContexts) {
|
|
370
|
-
// TODO: implement synced mode where async onPrepares aren't allowed
|
|
371
|
-
context.system.onPrepare && preparePromises.push(context.system.onPrepare());
|
|
372
|
-
nexSystemContexts.push(context);
|
|
373
|
-
}
|
|
374
|
-
this.entitySystemMap.set(entity, systemContexts);
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
const startSystems = () => {
|
|
378
|
-
// TODO: check if a "global" method is faster than this inner method
|
|
379
|
-
for (const context of nexSystemContexts) {
|
|
380
|
-
const system = context.system;
|
|
381
|
-
this.mapAnnotatedSignals(system);
|
|
382
|
-
system.onStart && system.onStart();
|
|
383
|
-
system.onPreFixedUpdate && this.onPreFixedUpdateSet.add(system);
|
|
384
|
-
system.onFixedUpdate && this.onFixedUpdateSet.add(system);
|
|
385
|
-
system.onEarlyUpdate && this.onEarlyUpdateSet.add(system);
|
|
386
|
-
system.onUpdate && this.onUpdateSet.add(system);
|
|
387
|
-
system.onLateUpdate && this.onLateUpdateSet.add(system);
|
|
388
|
-
system.onPhysicsUpdate && this.onPhysicsUpdateSet.add(system);
|
|
389
|
-
system.onPostPhysicsUpdate && this.onPostPhysicsUpdateSet.add(system);
|
|
390
|
-
system.onLateFixedUpdate && this.onLateFixedUpdateSet.add(system);
|
|
391
|
-
system.onRender && this.onRenderUpdateSet.add(system);
|
|
392
|
-
}
|
|
393
|
-
resolve();
|
|
394
|
-
};
|
|
395
|
-
|
|
396
|
-
// Promise.all doesnt resolve immediately when there are no promises bu
|
|
397
|
-
if (preparePromises.length === 0) {
|
|
398
|
-
startSystems();
|
|
399
|
-
} else {
|
|
400
|
-
Promise.all(preparePromises).then(startSystems);
|
|
401
|
-
}
|
|
464
|
+
const entityWasAdded = () => {
|
|
465
|
+
for (const system of this.onAddEntitySet) {
|
|
466
|
+
system.onAddEntity!(entity);
|
|
402
467
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
468
|
+
resolve();
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
// Promise.all doesnt resolve immediately when there are no promises bu
|
|
472
|
+
if (preparePromises.length === 0) {
|
|
473
|
+
this.startSystems(nextSystemContexts, entityWasAdded);
|
|
474
|
+
} else {
|
|
475
|
+
Promise.all(preparePromises).then(() => this.startSystems(nextSystemContexts, entityWasAdded));
|
|
476
|
+
}
|
|
410
477
|
}
|
|
478
|
+
this.addQueue = [];
|
|
479
|
+
// create promise for next frame
|
|
480
|
+
this.resolveFrame();
|
|
481
|
+
this.framePromise = new Promise<void>(resolve => {
|
|
482
|
+
this.resolveFrame = resolve;
|
|
483
|
+
});
|
|
484
|
+
this.onPrepare.dispatch();
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
private startSystems(contexts: Iterable<SystemContext>, then?: () => void) {
|
|
488
|
+
for (const context of contexts) {
|
|
489
|
+
const system = context.system;
|
|
490
|
+
this.mapAnnotatedSignals(system);
|
|
491
|
+
system.onStart && system.onStart();
|
|
492
|
+
system.onPreFixedUpdate && this.onPreFixedUpdateSet.add(system);
|
|
493
|
+
system.onFixedUpdate && this.onFixedUpdateSet.add(system);
|
|
494
|
+
system.onUpdate && this.onUpdateSet.add(system);
|
|
495
|
+
system.onLateUpdate && this.onLateUpdateSet.add(system);
|
|
496
|
+
system.onPhysicsUpdate && this.onPhysicsUpdateSet.add(system);
|
|
497
|
+
system.onPrePhysicsUpdate && this.onPrePhysicsUpdateSet.add(system);
|
|
498
|
+
system.onPostPhysicsUpdate && this.onPostPhysicsUpdateSet.add(system);
|
|
499
|
+
system.onLateFixedUpdate && this.onLateFixedUpdateSet.add(system);
|
|
500
|
+
system.onRender && this.onRenderUpdateSet.add(system);
|
|
501
|
+
system.onAddEntity && this.onAddEntitySet.add(system);
|
|
502
|
+
system.onRemoveEntity && this.onRemoveEntitySet.add(system);
|
|
503
|
+
}
|
|
504
|
+
then?.();
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
getAllEntities(): Entity[] {
|
|
508
|
+
return Array.from(this.entitySystemMap.keys());
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
removeAllEntities() {
|
|
512
|
+
return this.removeAll(this.getAllEntities());
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
private disposeContext(context: SystemContext<System>) {
|
|
516
|
+
const system = context.system;
|
|
517
|
+
|
|
518
|
+
// if onEnd returns a promise, we need to wait for it to finish before we can dispose the system
|
|
519
|
+
const onEndPromise = system.onEnd && system.onEnd();
|
|
520
|
+
if (onEndPromise instanceof Promise) {
|
|
521
|
+
onEndPromise.then(() => {
|
|
522
|
+
context.dispose();
|
|
523
|
+
});
|
|
524
|
+
} else {
|
|
525
|
+
context.dispose();
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
this.unmapAnnotatedSignals(system);
|
|
529
|
+
system.onPreFixedUpdate && this.onPreFixedUpdateSet.delete(system);
|
|
530
|
+
system.onFixedUpdate && this.onFixedUpdateSet.delete(system);
|
|
531
|
+
system.onUpdate && this.onUpdateSet.delete(system);
|
|
532
|
+
system.onLateUpdate && this.onLateUpdateSet.delete(system);
|
|
533
|
+
system.onPhysicsUpdate && this.onPhysicsUpdateSet.delete(system);
|
|
534
|
+
system.onPrePhysicsUpdate && this.onPrePhysicsUpdateSet.delete(system);
|
|
535
|
+
system.onPostPhysicsUpdate && this.onPostPhysicsUpdateSet.delete(system);
|
|
536
|
+
system.onLateFixedUpdate && this.onLateFixedUpdateSet.delete(system);
|
|
537
|
+
system.onRender && this.onRenderUpdateSet.delete(system);
|
|
538
|
+
}
|
|
411
539
|
}
|
|
412
540
|
//
|
|
413
541
|
// type RunSpread<T> = T extends undefined
|