/
runtimescene.ts
748 lines (667 loc) · 23.6 KB
/
runtimescene.ts
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/*
* GDevelop JS Platform
* Copyright 2013-2016 Florian Rival (Florian.Rival@gmail.com). All rights reserved.
* This project is released under the MIT License.
*/
namespace gdjs {
const logger = new gdjs.Logger('RuntimeScene');
const setupWarningLogger = new gdjs.Logger('RuntimeScene (setup warnings)');
/**
* A scene being played, containing instances of objects rendered on screen.
*/
export class RuntimeScene extends gdjs.RuntimeInstanceContainer {
_eventsFunction: null | ((runtimeScene: RuntimeScene) => void) = null;
_renderer: RuntimeSceneRenderer;
_debuggerRenderer: gdjs.DebuggerRenderer;
_variables: gdjs.VariablesContainer;
_runtimeGame: gdjs.RuntimeGame;
_lastId: integer = 0;
_name: string = '';
_timeManager: TimeManager;
_gameStopRequested: boolean = false;
_requestedScene: string = '';
private _asyncTasksManager = new gdjs.AsyncTasksManager();
/** True if loadFromScene was called and the scene is being played. */
_isLoaded: boolean = false;
/** True in the first frame after resuming the paused scene */
_isJustResumed: boolean = false;
_requestedChange: SceneChangeRequest;
/** Black background by default. */
_backgroundColor: integer = 0;
/** Should the canvas be cleared before this scene rendering. */
_clearCanvas: boolean = true;
_onceTriggers: OnceTriggers;
_profiler: gdjs.Profiler | null = null;
// Set to `new gdjs.Profiler()` to have profiling done on the scene.
_onProfilerStopped: null | ((oldProfiler: gdjs.Profiler) => void) = null;
_cachedGameResolutionWidth: integer;
_cachedGameResolutionHeight: integer;
/**
* @param runtimeGame The game associated to this scene.
*/
constructor(runtimeGame: gdjs.RuntimeGame) {
super();
this._runtimeGame = runtimeGame;
this._variables = new gdjs.VariablesContainer();
this._timeManager = new gdjs.TimeManager();
this._onceTriggers = new gdjs.OnceTriggers();
this._requestedChange = SceneChangeRequest.CONTINUE;
this._cachedGameResolutionWidth = runtimeGame
? runtimeGame.getGameResolutionWidth()
: 0;
this._cachedGameResolutionHeight = runtimeGame
? runtimeGame.getGameResolutionHeight()
: 0;
this._renderer = new gdjs.RuntimeSceneRenderer(
this,
// @ts-ignore This is needed because of test. They should mock RuntimeGame instead.
runtimeGame ? runtimeGame.getRenderer() : null
);
this._debuggerRenderer = new gdjs.DebuggerRenderer(this);
// What to do after the frame is rendered.
// The callback function to call when the profiler is stopped.
this.onGameResolutionResized();
}
addLayer(layerData: LayerData) {
const layer = new gdjs.Layer(layerData, this);
this._layers.put(layerData.name, layer);
this._orderedLayers.push(layer);
}
/**
* Should be called when the canvas where the scene is rendered has been resized.
* See gdjs.RuntimeGame.startGameLoop in particular.
*/
onGameResolutionResized() {
const oldGameResolutionOriginX = this.getViewportOriginX();
const oldGameResolutionOriginY = this.getViewportOriginY();
this._cachedGameResolutionWidth = this._runtimeGame
? this._runtimeGame.getGameResolutionWidth()
: 0;
this._cachedGameResolutionHeight = this._runtimeGame
? this._runtimeGame.getGameResolutionHeight()
: 0;
for (const name in this._layers.items) {
if (this._layers.items.hasOwnProperty(name)) {
const theLayer: gdjs.RuntimeLayer = this._layers.items[name];
theLayer.onGameResolutionResized(
oldGameResolutionOriginX,
oldGameResolutionOriginY
);
}
}
this._renderer.onGameResolutionResized();
}
/**
* Load the runtime scene from the given scene.
* @param sceneData An object containing the scene data.
* @see gdjs.RuntimeGame#getSceneData
*/
loadFromScene(sceneData: LayoutData | null) {
if (!sceneData) {
logger.error('loadFromScene was called without a scene');
return;
}
if (this._isLoaded) {
this.unloadScene();
}
//Setup main properties
if (this._runtimeGame) {
this._runtimeGame.getRenderer().setWindowTitle(sceneData.title);
}
this._name = sceneData.name;
this.setBackgroundColor(sceneData.r, sceneData.v, sceneData.b);
//Load layers
for (let i = 0, len = sceneData.layers.length; i < len; ++i) {
this.addLayer(sceneData.layers[i]);
}
//Load variables
this._variables = new gdjs.VariablesContainer(sceneData.variables);
//Cache the initial shared data of the behaviors
for (
let i = 0, len = sceneData.behaviorsSharedData.length;
i < len;
++i
) {
const behaviorSharedData = sceneData.behaviorsSharedData[i];
this.setInitialSharedDataForBehavior(
behaviorSharedData.name,
behaviorSharedData
);
}
//Registering objects: Global objects first...
const initialGlobalObjectsData = this.getGame().getInitialObjectsData();
for (let i = 0, len = initialGlobalObjectsData.length; i < len; ++i) {
this.registerObject(initialGlobalObjectsData[i]);
}
//...then the scene objects
for (let i = 0, len = sceneData.objects.length; i < len; ++i) {
this.registerObject(sceneData.objects[i]);
}
//Create initial instances of objects
this.createObjectsFrom(
sceneData.instances,
0,
0,
0,
/*trackByPersistentUuid=*/
true
);
// Set up the default z order (for objects created from events)
this._setLayerDefaultZOrders();
//Set up the function to be executed at each tick
this.setEventsGeneratedCodeFunction(sceneData);
this._onceTriggers = new gdjs.OnceTriggers();
// Notify the global callbacks
if (this._runtimeGame && !this._runtimeGame.wasFirstSceneLoaded()) {
for (let i = 0; i < gdjs.callbacksFirstRuntimeSceneLoaded.length; ++i) {
gdjs.callbacksFirstRuntimeSceneLoaded[i](this);
}
}
for (let i = 0; i < gdjs.callbacksRuntimeSceneLoaded.length; ++i) {
gdjs.callbacksRuntimeSceneLoaded[i](this);
}
if (sceneData.stopSoundsOnStartup && this._runtimeGame) {
this._runtimeGame.getSoundManager().clearAll();
}
this._isLoaded = true;
this._timeManager.reset();
}
getInitialSharedDataForBehavior(name: string): BehaviorSharedData | null {
// TODO Move this error in RuntimeInstanceContainer after deciding
// what to do with shared data in custom object.
const behaviorSharedData = super.getInitialSharedDataForBehavior(name);
if (!behaviorSharedData) {
logger.error("Can't find shared data for behavior with name: " + name);
}
return behaviorSharedData;
}
/**
* Called when a scene is "paused", i.e it will be not be rendered again
* for some time, until it's resumed or unloaded.
*/
onPause() {
// Notify the objects that the scene is being paused. Objects should not
// do anything special, but some object renderers might want to know about this.
const allInstancesList = this.getAdhocListOfAllInstances();
for (let i = 0, len = allInstancesList.length; i < len; ++i) {
const object = allInstancesList[i];
object.onScenePaused(this);
}
for (let i = 0; i < gdjs.callbacksRuntimeScenePaused.length; ++i) {
gdjs.callbacksRuntimeScenePaused[i](this);
}
}
/**
* Called when a scene is "resumed", i.e it will be rendered again
* on screen after having being paused.
*/
onResume() {
this._isJustResumed = true;
// Notify the objects that the scene is being resumed. Objects should not
// do anything special, but some object renderers might want to know about this.
const allInstancesList = this.getAdhocListOfAllInstances();
for (let i = 0, len = allInstancesList.length; i < len; ++i) {
const object = allInstancesList[i];
object.onSceneResumed(this);
}
for (let i = 0; i < gdjs.callbacksRuntimeSceneResumed.length; ++i) {
gdjs.callbacksRuntimeSceneResumed[i](this);
}
}
/**
* Called before a scene is removed from the stack of scenes
* rendered on the screen.
*/
unloadScene() {
if (!this._isLoaded) {
return;
}
if (this._profiler) {
this.stopProfiler();
}
// Notify the global callbacks (which should not release resources yet,
// as other callbacks might still refer to the objects/scene).
for (let i = 0; i < gdjs.callbacksRuntimeSceneUnloading.length; ++i) {
gdjs.callbacksRuntimeSceneUnloading[i](this);
}
// Notify the objects they are being destroyed
const allInstancesList = this.getAdhocListOfAllInstances();
for (let i = 0, len = allInstancesList.length; i < len; ++i) {
const object = allInstancesList[i];
object.onDeletedFromScene(this);
object.onDestroyed();
}
// Notify the renderer
if (this._renderer) {
this._renderer.onSceneUnloaded();
}
// Notify the global callbacks (after notifying objects and renderer, because
// callbacks from extensions might want to free resources - which can't be done
// safely before destroying objects and the renderer).
for (let i = 0; i < gdjs.callbacksRuntimeSceneUnloaded.length; ++i) {
gdjs.callbacksRuntimeSceneUnloaded[i](this);
}
this._destroy();
this._isLoaded = false;
this.onGameResolutionResized();
}
_destroy() {
// It should not be necessary to reset these variables, but this help
// ensuring that all memory related to the RuntimeScene is released immediately.
super._destroy();
this._variables = new gdjs.VariablesContainer();
this._initialBehaviorSharedData = new Hashtable();
this._eventsFunction = null;
this._lastId = 0;
// @ts-ignore We are deleting the object
this._onceTriggers = null;
}
/**
* Set the function called each time the scene is stepped to be the events generated code,
* which is by convention assumed to be a function in `gdjs` with a name based on the scene
* mangled name.
*
* @param sceneData The scene data, used to find where the code was generated.
*/
setEventsGeneratedCodeFunction(sceneData: LayoutData): void {
const module = gdjs[sceneData.mangledName + 'Code'];
if (module && module.func) {
this._eventsFunction = module.func;
} else {
setupWarningLogger.warn(
'No function found for running logic of scene ' + this._name
);
this._eventsFunction = function () {};
}
}
/**
* Set the function called each time the scene is stepped.
* The function will be passed the `runtimeScene` as argument.
*
* Note that this is already set up by the gdjs.RuntimeScene constructor and that you should
* not need to use this method.
*
* @param func The function to be called.
*/
setEventsFunction(func: () => void): void {
this._eventsFunction = func;
}
/**
* Step and render the scene.
* @param elapsedTime In milliseconds
* @return true if the game loop should continue, false if a scene change/push/pop
* or a game stop was requested.
*/
renderAndStep(elapsedTime: float): boolean {
if (this._profiler) {
this._profiler.beginFrame();
}
this._requestedChange = SceneChangeRequest.CONTINUE;
this._timeManager.update(
elapsedTime,
this._runtimeGame.getMinimalFramerate()
);
if (this._profiler) {
this._profiler.begin('asynchronous actions (wait action, etc...)');
}
this._asyncTasksManager.processTasks(this);
if (this._profiler) {
this._profiler.end('asynchronous actions (wait action, etc...)');
}
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 < gdjs.callbacksRuntimeScenePreEvents.length; ++i) {
gdjs.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 < gdjs.callbacksRuntimeScenePostEvents.length; ++i) {
gdjs.callbacksRuntimeScenePostEvents[i](this);
}
if (this._profiler) {
this._profiler.end('callbacks and extensions (post-events)');
}
if (this._profiler) {
this._profiler.begin('objects (pre-render, effects update)');
}
this._updateObjectsPreRender();
if (this._profiler) {
this._profiler.end('objects (pre-render, effects update)');
}
if (this._profiler) {
this._profiler.begin('layers (effects update)');
}
this._updateLayersPreRender();
if (this._profiler) {
this._profiler.end('layers (effects update)');
}
if (this._profiler) {
this._profiler.begin('render');
}
// Set to true to enable debug rendering (look for the implementation in the renderer
// to see what is rendered).
if (this._debugDrawEnabled) {
this._debuggerRenderer.renderDebugDraw(
this.getAdhocListOfAllInstances(),
this._debugDrawShowHiddenInstances,
this._debugDrawShowPointsNames,
this._debugDrawShowCustomPoints
);
}
this._isJustResumed = false;
this.render();
if (this._profiler) {
this._profiler.end('render');
}
if (this._profiler) {
this._profiler.endFrame();
}
return !!this.getRequestedChange();
}
/**
* Render the PIXI container associated to the runtimeScene.
*/
render() {
this._renderer.render();
}
/**
* Called to update visibility of the renderers of objects
* rendered on the scene ("culling"), update effects (of visible objects)
* and give a last chance for objects to update before rendering.
*
* Visibility is set to false if object is hidden, or if
* object is too far from the camera of its layer ("culling").
*/
_updateObjectsPreRender() {
if (this._timeManager.isFirstFrame()) {
super._updateObjectsPreRender();
return;
} else {
// After first frame, optimise rendering by setting only objects
// near camera as visible.
// TODO: For compatibility, pass a scale of `2`,
// meaning that size of cameras will be multiplied by 2 and so objects
// will be hidden if they are outside of this *larger* camera area.
// This is useful for:
// - objects not properly reporting their visibility AABB,
// (so we have a "safety margin") but these objects should be fixed
// instead.
// - objects having effects rendering outside of their visibility AABB.
// TODO (3D) culling - add support for 3D object culling?
this._updateLayersCameraCoordinates(2);
const allInstancesList = this.getAdhocListOfAllInstances();
for (let i = 0, len = allInstancesList.length; i < len; ++i) {
const object = allInstancesList[i];
const rendererObject = object.getRendererObject();
if (rendererObject) {
if (object.isHidden()) {
rendererObject.visible = false;
} else {
const cameraCoords = this._layersCameraCoordinates[
object.getLayer()
];
if (!cameraCoords) {
continue;
}
const aabb = object.getVisibilityAABB();
rendererObject.visible =
// If no AABB is returned, the object should always be visible
!aabb ||
// If an AABB is there, it must be at least partially inside
// the camera bounds.
!(
aabb.min[0] > cameraCoords[2] ||
aabb.min[1] > cameraCoords[3] ||
aabb.max[0] < cameraCoords[0] ||
aabb.max[1] < cameraCoords[1]
);
}
// Update effects, only for visible objects.
if (rendererObject.visible) {
this._runtimeGame
.getEffectsManager()
.updatePreRender(object.getRendererEffects(), object);
// Perform pre-render update only if the object is visible
// (including if there is no visibility AABB returned previously).
object.updatePreRender(this);
}
} else {
// Perform pre-render update, always for objects not having an
// associated renderer object (so it must handle visibility on its own).
object.updatePreRender(this);
}
}
}
}
/**
* Change the background color, by setting the RGB components.
* Internally, the color is stored as an hexadecimal number.
*
* @param r The color red component (0-255).
* @param g The color green component (0-255).
* @param b The color blue component (0-255).
*/
setBackgroundColor(r: integer, g: integer, b: integer): void {
this._backgroundColor = parseInt(gdjs.rgbToHex(r, g, b), 16);
}
/**
* Get the background color, as an hexadecimal number.
* @returns The current background color.
*/
getBackgroundColor(): number {
return this._backgroundColor;
}
/**
* Set whether the canvas should be cleared before this scene rendering.
* This is experimental: if possible, try to avoid relying on this and use
* custom objects to build complex scenes.
*/
setClearCanvas(shouldClearCanvas: boolean): void {
this._clearCanvas = shouldClearCanvas;
}
/**
* Get whether the canvas should be cleared before this scene rendering.
*/
getClearCanvas(): boolean {
return this._clearCanvas;
}
/**
* Get the name of the scene.
*/
getName(): string {
return this._name;
}
/**
* Create an identifier for a new object of the scene.
*/
createNewUniqueId(): integer {
this._lastId++;
return this._lastId;
}
getRenderer(): gdjs.RuntimeScenePixiRenderer {
return this._renderer;
}
getDebuggerRenderer() {
return this._debuggerRenderer;
}
getGame() {
return this._runtimeGame;
}
getScene() {
return this;
}
getViewportWidth(): float {
return this._cachedGameResolutionWidth;
}
getViewportHeight(): float {
return this._cachedGameResolutionHeight;
}
getViewportOriginX(): float {
return this._cachedGameResolutionWidth / 2;
}
getViewportOriginY(): float {
return this._cachedGameResolutionHeight / 2;
}
convertCoords(x: float, y: float, result: FloatPoint): FloatPoint {
// The result parameter used to be optional.
const point = result || [0, 0];
point[0] = x;
point[1] = y;
return point;
}
convertInverseCoords(
sceneX: float,
sceneY: float,
result: FloatPoint
): FloatPoint {
const point = result || [0, 0];
point[0] = sceneX;
point[1] = sceneY;
return point;
}
onChildrenLocationChanged(): void {
// Scenes don't maintain bounds.
}
/**
* Get the variables of the runtimeScene.
* @return The container holding the variables of the scene.
*/
getVariables() {
return this._variables;
}
/**
* Get the TimeManager of the scene.
* @return The gdjs.TimeManager of the scene.
*/
getTimeManager(): gdjs.TimeManager {
return this._timeManager;
}
/**
* Return the time elapsed since the last frame,
* in milliseconds, for objects on the layer.
*/
getElapsedTime(): float {
return this._timeManager.getElapsedTime();
}
/**
* Shortcut to get the SoundManager of the game.
* @return The gdjs.SoundManager of the game.
*/
getSoundManager(): gdjs.SoundManager {
return this._runtimeGame.getSoundManager();
}
/**
* @returns The scene's async tasks manager.
*/
getAsyncTasksManager() {
return this._asyncTasksManager;
}
/**
* Return the value of the scene change that is requested.
*/
getRequestedChange(): SceneChangeRequest {
return this._requestedChange;
}
/**
* Return the name of the new scene to be launched.
*
* See requestChange.
*/
getRequestedScene(): string {
return this._requestedScene;
}
/**
* Request a scene change to be made. The change is handled externally (see gdjs.SceneStack)
* thanks to getRequestedChange and getRequestedScene methods.
* @param change One of RuntimeScene.CONTINUE|PUSH_SCENE|POP_SCENE|REPLACE_SCENE|CLEAR_SCENES|STOP_GAME.
* @param sceneName The name of the new scene to launch, if applicable.
*/
requestChange(change: SceneChangeRequest, sceneName?: string) {
this._requestedChange = change;
if (sceneName) this._requestedScene = sceneName;
}
/**
* Get the profiler associated with the scene, or null if none.
*/
getProfiler(): gdjs.Profiler | null {
return this._profiler;
}
/**
* Start a new profiler to measures the time passed in sections of the engine
* in the scene.
* @param onProfilerStopped Function to be called when the profiler is stopped. Will be passed the profiler as argument.
*/
startProfiler(onProfilerStopped: (oldProfiler: gdjs.Profiler) => void) {
if (this._profiler) {
return;
}
this._profiler = new gdjs.Profiler();
this._onProfilerStopped = onProfilerStopped;
}
/**
* Stop the profiler being run on the scene.
*/
stopProfiler() {
if (!this._profiler) {
return;
}
const oldProfiler = this._profiler;
const onProfilerStopped = this._onProfilerStopped;
this._profiler = null;
this._onProfilerStopped = null;
if (onProfilerStopped) {
onProfilerStopped(oldProfiler);
}
}
/**
* Get the structure containing the triggers for "Trigger once" conditions.
*/
getOnceTriggers() {
return this._onceTriggers;
}
/**
* Check if the scene was just resumed.
* This is true during the first frame after the scene has been unpaused.
*
* @returns true if the scene was just resumed
*/
sceneJustResumed(): boolean {
return this._isJustResumed;
}
}
//The flags to describe the change request by a scene:
export enum SceneChangeRequest {
CONTINUE,
PUSH_SCENE,
POP_SCENE,
REPLACE_SCENE,
CLEAR_SCENES,
STOP_GAME,
}
}