-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtimescene.js
601 lines (601 loc) · 21.6 KB
/
runtimescene.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
var gdjs;
(function(gdjs2) {
class RuntimeScene {
constructor(runtimeGame) {
this._eventsFunction = null;
this._lastId = 0;
this._name = "";
this._gameStopRequested = false;
this._requestedScene = "";
this._isLoaded = false;
this._isJustResumed = false;
this._backgroundColor = 0;
this._allInstancesList = [];
this._layersCameraCoordinates = {};
this._instancesRemoved = [];
this._profiler = null;
this._onProfilerStopped = null;
this._instances = new Hashtable();
this._instancesCache = new Hashtable();
this._objects = new Hashtable();
this._objectsCtor = new Hashtable();
this._layers = new Hashtable();
this._initialBehaviorSharedData = new Hashtable();
this._renderer = new gdjs2.RuntimeSceneRenderer(this, runtimeGame ? runtimeGame.getRenderer() : null);
this._variables = new gdjs2.VariablesContainer();
this._runtimeGame = runtimeGame;
this._timeManager = new gdjs2.TimeManager();
this._requestedChange = SceneChangeRequest.CONTINUE;
this._onceTriggers = new gdjs2.OnceTriggers();
this.onGameResolutionResized();
}
onGameResolutionResized() {
for (const name in this._layers.items) {
if (this._layers.items.hasOwnProperty(name)) {
const theLayer = this._layers.items[name];
theLayer.onGameResolutionResized();
}
}
this._renderer.onGameResolutionResized();
}
loadFromScene(sceneData) {
if (!sceneData) {
console.error("loadFromScene was called without a scene");
return;
}
if (this._isLoaded) {
this.unloadScene();
}
if (this._runtimeGame) {
this._runtimeGame.getRenderer().setWindowTitle(sceneData.title);
}
this._name = sceneData.name;
this.setBackgroundColor(sceneData.r, sceneData.v, sceneData.b);
for (let i = 0, len = sceneData.layers.length; i < len; ++i) {
this.addLayer(sceneData.layers[i]);
}
this._variables = new gdjs2.VariablesContainer(sceneData.variables);
for (let i = 0, len = sceneData.behaviorsSharedData.length; i < len; ++i) {
const behaviorSharedData = sceneData.behaviorsSharedData[i];
this.setInitialSharedDataForBehavior(behaviorSharedData.name, behaviorSharedData);
}
const initialGlobalObjectsData = this.getGame().getInitialObjectsData();
for (let i = 0, len = initialGlobalObjectsData.length; i < len; ++i) {
this.registerObject(initialGlobalObjectsData[i]);
}
for (let i = 0, len = sceneData.objects.length; i < len; ++i) {
this.registerObject(sceneData.objects[i]);
}
this.createObjectsFrom(sceneData.instances, 0, 0, true);
this._setLayerDefaultZOrders();
this.setEventsGeneratedCodeFunction(sceneData);
this._onceTriggers = new gdjs2.OnceTriggers();
if (this._runtimeGame && !this._runtimeGame.wasFirstSceneLoaded()) {
for (let i = 0; i < gdjs2.callbacksFirstRuntimeSceneLoaded.length; ++i) {
gdjs2.callbacksFirstRuntimeSceneLoaded[i](this);
}
}
for (let i = 0; i < gdjs2.callbacksRuntimeSceneLoaded.length; ++i) {
gdjs2.callbacksRuntimeSceneLoaded[i](this);
}
if (sceneData.stopSoundsOnStartup && this._runtimeGame) {
this._runtimeGame.getSoundManager().clearAll();
}
this._isLoaded = true;
this._timeManager.reset();
}
isObjectRegistered(objectName) {
return this._objects.containsKey(objectName) && this._instances.containsKey(objectName) && this._objectsCtor.containsKey(objectName);
}
registerObject(objectData) {
this._objects.put(objectData.name, objectData);
this._instances.put(objectData.name, []);
const Ctor = gdjs2.getObjectConstructor(objectData.type);
this._objectsCtor.put(objectData.name, Ctor);
if (Ctor.supportsReinitialization) {
this._instancesCache.put(objectData.name, []);
}
}
updateObject(objectData) {
if (!this.isObjectRegistered(objectData.name)) {
console.warn("Tried to call updateObject for an object that was not registered (" + objectData.name + "). Call registerObject first.");
}
this._objects.put(objectData.name, objectData);
}
unregisterObject(objectName) {
const instances = this._instances.get(objectName);
if (instances) {
const instancesToRemove = instances.slice();
for (let i = 0; i < instancesToRemove.length; i++) {
this.markObjectForDeletion(instancesToRemove[i]);
}
this._cacheOrClearRemovedInstances();
}
this._objects.remove(objectName);
this._instances.remove(objectName);
this._instancesCache.remove(objectName);
this._objectsCtor.remove(objectName);
}
onPause() {
for (let i = 0; i < gdjs2.callbacksRuntimeScenePaused.length; ++i) {
gdjs2.callbacksRuntimeScenePaused[i](this);
}
}
onResume() {
this._isJustResumed = true;
for (let i = 0; i < gdjs2.callbacksRuntimeSceneResumed.length; ++i) {
gdjs2.callbacksRuntimeSceneResumed[i](this);
}
}
unloadScene() {
if (!this._isLoaded) {
return;
}
if (this._profiler) {
this.stopProfiler();
}
for (let i = 0; i < gdjs2.callbacksRuntimeSceneUnloading.length; ++i) {
gdjs2.callbacksRuntimeSceneUnloading[i](this);
}
this._constructListOfAllInstances();
for (let i = 0, len = this._allInstancesList.length; i < len; ++i) {
const object = this._allInstancesList[i];
object.onDestroyFromScene(this);
}
if (this._renderer) {
this._renderer.onSceneUnloaded();
}
for (let i = 0; i < gdjs2.callbacksRuntimeSceneUnloaded.length; ++i) {
gdjs2.callbacksRuntimeSceneUnloaded[i](this);
}
this._layers = new Hashtable();
this._variables = new gdjs2.VariablesContainer();
this._initialBehaviorSharedData = new Hashtable();
this._objects = new Hashtable();
this._instances = new Hashtable();
this._instancesCache = new Hashtable();
this._eventsFunction = null;
this._objectsCtor = new Hashtable();
this._allInstancesList = [];
this._instancesRemoved = [];
this._lastId = 0;
this._onceTriggers = null;
this._isLoaded = false;
this.onGameResolutionResized();
}
createObjectsFrom(data, xPos, yPos, trackByPersistentUuid) {
for (let i = 0, len = data.length; i < len; ++i) {
const instanceData = data[i];
const objectName = instanceData.name;
const newObject = this.createObject(objectName);
if (newObject !== null) {
if (trackByPersistentUuid) {
newObject.persistentUuid = instanceData.persistentUuid || null;
}
newObject.setPosition(instanceData.x + xPos, instanceData.y + yPos);
newObject.setZOrder(instanceData.zOrder);
newObject.setAngle(instanceData.angle);
newObject.setLayer(instanceData.layer);
newObject.getVariables().initFrom(instanceData.initialVariables, true);
newObject.extraInitializationFromInitialInstance(instanceData);
}
}
}
_setLayerDefaultZOrders() {
if (this._runtimeGame.getGameData().properties.useDeprecatedZeroAsDefaultZOrder) {
return;
}
const layerHighestZOrders = {};
const allInstances = this.getAdhocListOfAllInstances();
for (let i = 0, len = allInstances.length; i < len; ++i) {
const object = allInstances[i];
let layerName = object.getLayer();
const zOrder = object.getZOrder();
if (layerHighestZOrders[layerName] === void 0 || layerHighestZOrders[layerName] < zOrder) {
layerHighestZOrders[layerName] = zOrder;
}
}
for (let layerName in layerHighestZOrders) {
this.getLayer(layerName).setDefaultZOrder(layerHighestZOrders[layerName] + 1);
}
}
setEventsGeneratedCodeFunction(sceneData) {
const module = gdjs2[sceneData.mangledName + "Code"];
if (module && module.func) {
this._eventsFunction = module.func;
} else {
console.log("Warning: no function found for running logic of scene " + this._name);
this._eventsFunction = function() {
};
}
}
setEventsFunction(func) {
this._eventsFunction = func;
}
renderAndStep(elapsedTime) {
if (this._profiler) {
this._profiler.beginFrame();
}
this._requestedChange = SceneChangeRequest.CONTINUE;
this._timeManager.update(elapsedTime, this._runtimeGame.getMinimalFramerate());
if (this._profiler) {
this._profiler.begin("objects (pre-events)");
}
this._updateObjectsPreEvents();
if (this._profiler) {
this._profiler.end("objects (pre-events)");
}
if (this._profiler) {
this._profiler.begin("callbacks and extensions (pre-events)");
}
for (let i = 0; i < gdjs2.callbacksRuntimeScenePreEvents.length; ++i) {
gdjs2.callbacksRuntimeScenePreEvents[i](this);
}
if (this._profiler) {
this._profiler.end("callbacks and extensions (pre-events)");
}
if (this._profiler) {
this._profiler.begin("events");
}
if (this._eventsFunction !== null)
this._eventsFunction(this);
if (this._profiler) {
this._profiler.end("events");
}
if (this._profiler) {
this._profiler.begin("objects (post-events)");
}
this._updateObjectsPostEvents();
if (this._profiler) {
this._profiler.end("objects (post-events)");
}
if (this._profiler) {
this._profiler.begin("callbacks and extensions (post-events)");
}
for (let i = 0; i < gdjs2.callbacksRuntimeScenePostEvents.length; ++i) {
gdjs2.callbacksRuntimeScenePostEvents[i](this);
}
if (this._profiler) {
this._profiler.end("callbacks and extensions (post-events)");
}
if (this._profiler) {
this._profiler.begin("objects (pre-render)");
}
this._updateObjectsPreRender();
if (this._profiler) {
this._profiler.end("objects (pre-render)");
}
if (this._profiler) {
this._profiler.begin("layers (effects update)");
}
this._updateLayers();
if (this._profiler) {
this._profiler.end("layers (effects update)");
}
if (this._profiler) {
this._profiler.begin("render");
}
const renderDebugDraw = false;
if (renderDebugDraw && this._layersCameraCoordinates) {
this.getRenderer().renderDebugDraw(this._allInstancesList, this._layersCameraCoordinates);
}
this._isJustResumed = false;
this.render();
if (this._profiler) {
this._profiler.end("render");
}
if (this._profiler) {
this._profiler.endFrame();
}
return !!this.getRequestedChange();
}
render() {
this._renderer.render();
}
_updateLayersCameraCoordinates() {
this._layersCameraCoordinates = this._layersCameraCoordinates || {};
for (const name in this._layers.items) {
if (this._layers.items.hasOwnProperty(name)) {
const theLayer = this._layers.items[name];
this._layersCameraCoordinates[name] = this._layersCameraCoordinates[name] || [0, 0, 0, 0];
this._layersCameraCoordinates[name][0] = theLayer.getCameraX() - theLayer.getCameraWidth();
this._layersCameraCoordinates[name][1] = theLayer.getCameraY() - theLayer.getCameraHeight();
this._layersCameraCoordinates[name][2] = theLayer.getCameraX() + theLayer.getCameraWidth();
this._layersCameraCoordinates[name][3] = theLayer.getCameraY() + theLayer.getCameraHeight();
}
}
}
_updateLayers() {
for (const name in this._layers.items) {
if (this._layers.items.hasOwnProperty(name)) {
const theLayer = this._layers.items[name];
theLayer.update(this);
}
}
}
_updateObjectsPreRender() {
if (this._timeManager.isFirstFrame()) {
this._constructListOfAllInstances();
for (let i = 0, len = this._allInstancesList.length; i < len; ++i) {
const object = this._allInstancesList[i];
const rendererObject = object.getRendererObject();
if (rendererObject) {
object.getRendererObject().visible = !object.isHidden();
}
object.updatePreRender(this);
}
return;
} else {
this._updateLayersCameraCoordinates();
this._constructListOfAllInstances();
for (let i = 0, len = this._allInstancesList.length; i < len; ++i) {
const object = this._allInstancesList[i];
const cameraCoords = this._layersCameraCoordinates[object.getLayer()];
const rendererObject = object.getRendererObject();
if (!cameraCoords || !rendererObject) {
continue;
}
if (object.isHidden()) {
rendererObject.visible = false;
} else {
const aabb = object.getVisibilityAABB();
if (aabb && (aabb.min[0] > cameraCoords[2] || aabb.min[1] > cameraCoords[3] || aabb.max[0] < cameraCoords[0] || aabb.max[1] < cameraCoords[1])) {
rendererObject.visible = false;
} else {
rendererObject.visible = true;
}
}
object.updatePreRender(this);
}
}
}
_cacheOrClearRemovedInstances() {
for (let k = 0, lenk = this._instancesRemoved.length; k < lenk; ++k) {
const cache = this._instancesCache.get(this._instancesRemoved[k].getName());
if (cache) {
if (cache.length < 128) {
cache.push(this._instancesRemoved[k]);
}
}
}
this._instancesRemoved.length = 0;
}
_constructListOfAllInstances() {
let currentListSize = 0;
for (const name in this._instances.items) {
if (this._instances.items.hasOwnProperty(name)) {
const list = this._instances.items[name];
const oldSize = currentListSize;
currentListSize += list.length;
for (let j = 0, lenj = list.length; j < lenj; ++j) {
if (oldSize + j < this._allInstancesList.length) {
this._allInstancesList[oldSize + j] = list[j];
} else {
this._allInstancesList.push(list[j]);
}
}
}
}
this._allInstancesList.length = currentListSize;
}
_updateObjectsPreEvents() {
this._constructListOfAllInstances();
for (let i = 0, len = this._allInstancesList.length; i < len; ++i) {
const obj = this._allInstancesList[i];
const elapsedTime = obj.getElapsedTime(this);
if (!obj.hasNoForces()) {
const averageForce = obj.getAverageForce();
const elapsedTimeInSeconds = elapsedTime / 1e3;
obj.setX(obj.getX() + averageForce.getX() * elapsedTimeInSeconds);
obj.setY(obj.getY() + averageForce.getY() * elapsedTimeInSeconds);
obj.update(this);
obj.updateForces(elapsedTimeInSeconds);
} else {
obj.update(this);
}
obj.updateTimers(elapsedTime);
this._allInstancesList[i].stepBehaviorsPreEvents(this);
}
this._cacheOrClearRemovedInstances();
}
_updateObjectsPostEvents() {
this._cacheOrClearRemovedInstances();
this._constructListOfAllInstances();
for (let i = 0, len = this._allInstancesList.length; i < len; ++i) {
this._allInstancesList[i].stepBehaviorsPostEvents(this);
}
this._cacheOrClearRemovedInstances();
}
setBackgroundColor(r, g, b) {
this._backgroundColor = parseInt(gdjs2.rgbToHex(r, g, b), 16);
}
getBackgroundColor() {
return this._backgroundColor;
}
getName() {
return this._name;
}
updateObjectsForces() {
for (const name in this._instances.items) {
if (this._instances.items.hasOwnProperty(name)) {
const list = this._instances.items[name];
for (let j = 0, listLen = list.length; j < listLen; ++j) {
const obj = list[j];
if (!obj.hasNoForces()) {
const averageForce = obj.getAverageForce();
const elapsedTimeInSeconds = obj.getElapsedTime(this) / 1e3;
obj.setX(obj.getX() + averageForce.getX() * elapsedTimeInSeconds);
obj.setY(obj.getY() + averageForce.getY() * elapsedTimeInSeconds);
obj.updateForces(elapsedTimeInSeconds);
}
}
}
}
}
addObject(obj) {
if (!this._instances.containsKey(obj.name)) {
this._instances.put(obj.name, []);
}
this._instances.get(obj.name).push(obj);
}
getObjects(name) {
if (!this._instances.containsKey(name)) {
console.log('RuntimeScene.getObjects: No instances called "' + name + '"! Adding it.');
this._instances.put(name, []);
}
return this._instances.get(name);
}
createObject(objectName) {
if (!this._objectsCtor.containsKey(objectName) || !this._objects.containsKey(objectName)) {
return null;
}
const cache = this._instancesCache.get(objectName);
const ctor = this._objectsCtor.get(objectName);
let obj;
if (!cache || cache.length === 0) {
obj = new ctor(this, this._objects.get(objectName));
} else {
obj = cache.pop();
obj.reinitialize(this._objects.get(objectName));
}
this.addObject(obj);
return obj;
}
markObjectForDeletion(obj) {
if (this._instancesRemoved.indexOf(obj) === -1) {
this._instancesRemoved.push(obj);
}
if (this._instances.containsKey(obj.getName())) {
const objId = obj.id;
const allInstances = this._instances.get(obj.getName());
for (let i = 0, len = allInstances.length; i < len; ++i) {
if (allInstances[i].id == objId) {
allInstances.splice(i, 1);
break;
}
}
}
obj.onDestroyFromScene(this);
for (let j = 0; j < gdjs2.callbacksObjectDeletedFromScene.length; ++j) {
gdjs2.callbacksObjectDeletedFromScene[j](this, obj);
}
return;
}
createNewUniqueId() {
this._lastId++;
return this._lastId;
}
getRenderer() {
return this._renderer;
}
getGame() {
return this._runtimeGame;
}
getVariables() {
return this._variables;
}
getInitialSharedDataForBehavior(name) {
const behaviorSharedData = this._initialBehaviorSharedData.get(name);
if (behaviorSharedData) {
return behaviorSharedData;
}
console.error("Can't find shared data for behavior with name:", name);
return null;
}
setInitialSharedDataForBehavior(name, sharedData) {
this._initialBehaviorSharedData.put(name, sharedData);
}
getLayer(name) {
if (this._layers.containsKey(name)) {
return this._layers.get(name);
}
return this._layers.get("");
}
hasLayer(name) {
return this._layers.containsKey(name);
}
addLayer(layerData) {
this._layers.put(layerData.name, new gdjs2.Layer(layerData, this));
}
removeLayer(layerName) {
const allInstances = this.getAdhocListOfAllInstances();
for (let i = 0; i < allInstances.length; ++i) {
const runtimeObject = allInstances[i];
if (runtimeObject.getLayer() === layerName) {
runtimeObject.setLayer("");
}
}
this._layers.remove(layerName);
}
setLayerIndex(layerName, index) {
const layer = this._layers.get(layerName);
if (!layer) {
return;
}
this._renderer.setLayerIndex(layer, index);
}
getAllLayerNames(result) {
this._layers.keys(result);
}
getTimeManager() {
return this._timeManager;
}
getSoundManager() {
return this._runtimeGame.getSoundManager();
}
getRequestedChange() {
return this._requestedChange;
}
getRequestedScene() {
return this._requestedScene;
}
requestChange(change, sceneName) {
this._requestedChange = change;
if (sceneName)
this._requestedScene = sceneName;
}
getProfiler() {
return this._profiler;
}
startProfiler(onProfilerStopped) {
if (this._profiler) {
return;
}
this._profiler = new gdjs2.Profiler();
this._onProfilerStopped = onProfilerStopped;
}
stopProfiler() {
if (!this._profiler) {
return;
}
const oldProfiler = this._profiler;
const onProfilerStopped = this._onProfilerStopped;
this._profiler = null;
this._onProfilerStopped = null;
if (onProfilerStopped) {
onProfilerStopped(oldProfiler);
}
}
getOnceTriggers() {
return this._onceTriggers;
}
getAdhocListOfAllInstances() {
this._constructListOfAllInstances();
return this._allInstancesList;
}
sceneJustResumed() {
return this._isJustResumed;
}
}
gdjs2.RuntimeScene = RuntimeScene;
let SceneChangeRequest;
(function(SceneChangeRequest2) {
SceneChangeRequest2[SceneChangeRequest2["CONTINUE"] = 0] = "CONTINUE";
SceneChangeRequest2[SceneChangeRequest2["PUSH_SCENE"] = 1] = "PUSH_SCENE";
SceneChangeRequest2[SceneChangeRequest2["POP_SCENE"] = 2] = "POP_SCENE";
SceneChangeRequest2[SceneChangeRequest2["REPLACE_SCENE"] = 3] = "REPLACE_SCENE";
SceneChangeRequest2[SceneChangeRequest2["CLEAR_SCENES"] = 4] = "CLEAR_SCENES";
SceneChangeRequest2[SceneChangeRequest2["STOP_GAME"] = 5] = "STOP_GAME";
})(SceneChangeRequest = gdjs2.SceneChangeRequest || (gdjs2.SceneChangeRequest = {}));
})(gdjs || (gdjs = {}));
//# sourceMappingURL=runtimescene.js.map