forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystems.js
813 lines (710 loc) · 24 KB
/
Systems.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
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
/**
* @author Richard Davey <[email protected]>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../utils/Class');
var CONST = require('./const');
var DefaultPlugins = require('../plugins/DefaultPlugins');
var Events = require('./events');
var GetPhysicsPlugins = require('./GetPhysicsPlugins');
var GetScenePlugins = require('./GetScenePlugins');
var GLOBAL_CONST = require('../const');
var NOOP = require('../utils/NOOP');
var Settings = require('./Settings');
/**
* @classdesc
* The Scene Systems class.
*
* This class is available from within a Scene under the property `sys`.
* It is responsible for managing all of the plugins a Scene has running, including the display list, and
* handling the update step and renderer. It also contains references to global systems belonging to Game.
*
* @class Systems
* @memberof Phaser.Scenes
* @constructor
* @since 3.0.0
*
* @param {Phaser.Scene} scene - The Scene that owns this Systems instance.
* @param {(string|Phaser.Types.Scenes.SettingsConfig)} config - Scene specific configuration settings.
*/
var Systems = new Class({
initialize:
function Systems (scene, config)
{
/**
* A reference to the Scene that these Systems belong to.
*
* @name Phaser.Scenes.Systems#scene
* @type {Phaser.Scene}
* @since 3.0.0
*/
this.scene = scene;
/**
* A reference to the Phaser Game instance.
*
* @name Phaser.Scenes.Systems#game
* @type {Phaser.Game}
* @since 3.0.0
*/
this.game;
/**
* A reference to either the Canvas or WebGL Renderer that this Game is using.
*
* @name Phaser.Scenes.Systems#renderer
* @type {(Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer)}
* @since 3.17.0
*/
this.renderer;
if (typeof PLUGIN_FBINSTANT)
{
/**
* The Facebook Instant Games Plugin.
*
* @name Phaser.Scenes.Systems#facebook
* @type {Phaser.FacebookInstantGamesPlugin}
* @since 3.12.0
*/
this.facebook;
}
/**
* The Scene Configuration object, as passed in when creating the Scene.
*
* @name Phaser.Scenes.Systems#config
* @type {(string|Phaser.Types.Scenes.SettingsConfig)}
* @since 3.0.0
*/
this.config = config;
/**
* The Scene Settings. This is the parsed output based on the Scene configuration.
*
* @name Phaser.Scenes.Systems#settings
* @type {Phaser.Types.Scenes.SettingsObject}
* @since 3.0.0
*/
this.settings = Settings.create(config);
/**
* A handy reference to the Scene canvas / context.
*
* @name Phaser.Scenes.Systems#canvas
* @type {HTMLCanvasElement}
* @since 3.0.0
*/
this.canvas;
/**
* A reference to the Canvas Rendering Context being used by the renderer.
*
* @name Phaser.Scenes.Systems#context
* @type {CanvasRenderingContext2D}
* @since 3.0.0
*/
this.context;
// Global Systems - these are single-instance global managers that belong to Game
/**
* A reference to the global Animations Manager.
*
* In the default set-up you can access this from within a Scene via the `this.anims` property.
*
* @name Phaser.Scenes.Systems#anims
* @type {Phaser.Animations.AnimationManager}
* @since 3.0.0
*/
this.anims;
/**
* A reference to the global Cache. The Cache stores all files bought in to Phaser via
* the Loader, with the exception of images. Images are stored in the Texture Manager.
*
* In the default set-up you can access this from within a Scene via the `this.cache` property.
*
* @name Phaser.Scenes.Systems#cache
* @type {Phaser.Cache.CacheManager}
* @since 3.0.0
*/
this.cache;
/**
* A reference to the global Plugins Manager.
*
* In the default set-up you can access this from within a Scene via the `this.plugins` property.
*
* @name Phaser.Scenes.Systems#plugins
* @type {Phaser.Plugins.PluginManager}
* @since 3.0.0
*/
this.plugins;
/**
* A reference to the global registry. This is a game-wide instance of the Data Manager, allowing
* you to exchange data between Scenes via a universal and shared point.
*
* In the default set-up you can access this from within a Scene via the `this.registry` property.
*
* @name Phaser.Scenes.Systems#registry
* @type {Phaser.Data.DataManager}
* @since 3.0.0
*/
this.registry;
/**
* A reference to the global Scale Manager.
*
* In the default set-up you can access this from within a Scene via the `this.scale` property.
*
* @name Phaser.Scenes.Systems#scale
* @type {Phaser.Scale.ScaleManager}
* @since 3.15.0
*/
this.scale;
/**
* A reference to the global Sound Manager.
*
* In the default set-up you can access this from within a Scene via the `this.sound` property.
*
* @name Phaser.Scenes.Systems#sound
* @type {(Phaser.Sound.NoAudioSoundManager|Phaser.Sound.HTML5AudioSoundManager|Phaser.Sound.WebAudioSoundManager)}
* @since 3.0.0
*/
this.sound;
/**
* A reference to the global Texture Manager.
*
* In the default set-up you can access this from within a Scene via the `this.textures` property.
*
* @name Phaser.Scenes.Systems#textures
* @type {Phaser.Textures.TextureManager}
* @since 3.0.0
*/
this.textures;
// Core Plugins - these are non-optional Scene plugins, needed by lots of the other systems
/**
* A reference to the Scene's Game Object Factory.
*
* Use this to quickly and easily create new Game Object's.
*
* In the default set-up you can access this from within a Scene via the `this.add` property.
*
* @name Phaser.Scenes.Systems#add
* @type {Phaser.GameObjects.GameObjectFactory}
* @since 3.0.0
*/
this.add;
/**
* A reference to the Scene's Camera Manager.
*
* Use this to manipulate and create Cameras for this specific Scene.
*
* In the default set-up you can access this from within a Scene via the `this.cameras` property.
*
* @name Phaser.Scenes.Systems#cameras
* @type {Phaser.Cameras.Scene2D.CameraManager}
* @since 3.0.0
*/
this.cameras;
/**
* A reference to the Scene's Display List.
*
* Use this to organize the children contained in the display list.
*
* In the default set-up you can access this from within a Scene via the `this.children` property.
*
* @name Phaser.Scenes.Systems#displayList
* @type {Phaser.GameObjects.DisplayList}
* @since 3.0.0
*/
this.displayList;
/**
* A reference to the Scene's Event Manager.
*
* Use this to listen for Scene specific events, such as `pause` and `shutdown`.
*
* In the default set-up you can access this from within a Scene via the `this.events` property.
*
* @name Phaser.Scenes.Systems#events
* @type {Phaser.Events.EventEmitter}
* @since 3.0.0
*/
this.events;
/**
* A reference to the Scene's Game Object Creator.
*
* Use this to quickly and easily create new Game Object's. The difference between this and the
* Game Object Factory, is that the Creator just creates and returns Game Object instances, it
* doesn't then add them to the Display List or Update List.
*
* In the default set-up you can access this from within a Scene via the `this.make` property.
*
* @name Phaser.Scenes.Systems#make
* @type {Phaser.GameObjects.GameObjectCreator}
* @since 3.0.0
*/
this.make;
/**
* A reference to the Scene Manager Plugin.
*
* Use this to manipulate both this and other Scene's in your game, for example to launch a parallel Scene,
* or pause or resume a Scene, or switch from this Scene to another.
*
* In the default set-up you can access this from within a Scene via the `this.scene` property.
*
* @name Phaser.Scenes.Systems#scenePlugin
* @type {Phaser.Scenes.ScenePlugin}
* @since 3.0.0
*/
this.scenePlugin;
/**
* A reference to the Scene's Update List.
*
* Use this to organize the children contained in the update list.
*
* The Update List is responsible for managing children that need their `preUpdate` methods called,
* in order to process so internal components, such as Sprites with Animations.
*
* In the default set-up there is no reference to this from within the Scene itself.
*
* @name Phaser.Scenes.Systems#updateList
* @type {Phaser.GameObjects.UpdateList}
* @since 3.0.0
*/
this.updateList;
/**
* The Scene Update function.
*
* This starts out as NOOP during init, preload and create, and at the end of create
* it swaps to be whatever the Scene.update function is.
*
* @name Phaser.Scenes.Systems#sceneUpdate
* @type {function}
* @private
* @since 3.10.0
*/
this.sceneUpdate = NOOP;
},
/**
* This method is called only once by the Scene Manager when the Scene is instantiated.
* It is responsible for setting up all of the Scene plugins and references.
* It should never be called directly.
*
* @method Phaser.Scenes.Systems#init
* @protected
* @fires Phaser.Scenes.Events#BOOT
* @since 3.0.0
*
* @param {Phaser.Game} game - A reference to the Phaser Game instance.
*/
init: function (game)
{
this.settings.status = CONST.INIT;
// This will get replaced by the SceneManager with the actual update function, if it exists, once create is over.
this.sceneUpdate = NOOP;
this.game = game;
this.renderer = game.renderer;
this.canvas = game.canvas;
this.context = game.context;
var pluginManager = game.plugins;
this.plugins = pluginManager;
pluginManager.addToScene(this, DefaultPlugins.Global, [ DefaultPlugins.CoreScene, GetScenePlugins(this), GetPhysicsPlugins(this) ]);
this.events.emit(Events.BOOT, this);
this.settings.isBooted = true;
},
/**
* A single game step. Called automatically by the Scene Manager as a result of a Request Animation
* Frame or Set Timeout call to the main Game instance.
*
* @method Phaser.Scenes.Systems#step
* @fires Phaser.Scenes.Events#PRE_UPDATE
* @fires Phaser.Scenes.Events#UPDATE
* @fires Phaser.Scenes.Events#POST_UPDATE
* @since 3.0.0
*
* @param {number} time - The time value from the most recent Game step. Typically a high-resolution timer value, or Date.now().
* @param {number} delta - The delta value since the last frame. This is smoothed to avoid delta spikes by the TimeStep class.
*/
step: function (time, delta)
{
var events = this.events;
events.emit(Events.PRE_UPDATE, time, delta);
events.emit(Events.UPDATE, time, delta);
this.sceneUpdate.call(this.scene, time, delta);
events.emit(Events.POST_UPDATE, time, delta);
},
/**
* Called automatically by the Scene Manager.
* Instructs the Scene to render itself via its Camera Manager to the renderer given.
*
* @method Phaser.Scenes.Systems#render
* @fires Phaser.Scenes.Events#PRE_RENDER
* @fires Phaser.Scenes.Events#RENDER
* @since 3.0.0
*
* @param {(Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer)} renderer - The renderer that invoked the render call.
*/
render: function (renderer)
{
var displayList = this.displayList;
displayList.depthSort();
this.events.emit(Events.PRE_RENDER, renderer);
this.cameras.render(renderer, displayList);
this.events.emit(Events.RENDER, renderer);
},
/**
* Force a sort of the display list on the next render.
*
* @method Phaser.Scenes.Systems#queueDepthSort
* @since 3.0.0
*/
queueDepthSort: function ()
{
this.displayList.queueDepthSort();
},
/**
* Immediately sorts the display list if the flag is set.
*
* @method Phaser.Scenes.Systems#depthSort
* @since 3.0.0
*/
depthSort: function ()
{
this.displayList.depthSort();
},
/**
* Pause this Scene.
*
* A paused Scene still renders, it just doesn't run any of its update handlers or systems.
*
* @method Phaser.Scenes.Systems#pause
* @fires Phaser.Scenes.Events#PAUSE
* @since 3.0.0
*
* @param {object} [data] - A data object that will be passed in the 'pause' event.
*
* @return {Phaser.Scenes.Systems} This Systems object.
*/
pause: function (data)
{
var settings = this.settings;
var status = this.getStatus();
if (status !== CONST.CREATING && status !== CONST.RUNNING)
{
console.warn('Cannot pause non-running Scene', settings.key);
}
else if (this.settings.active)
{
settings.status = CONST.PAUSED;
settings.active = false;
this.events.emit(Events.PAUSE, this, data);
}
return this;
},
/**
* Resume this Scene from a paused state.
*
* @method Phaser.Scenes.Systems#resume
* @fires Phaser.Scenes.Events#RESUME
* @since 3.0.0
*
* @param {object} [data] - A data object that will be passed in the 'resume' event.
*
* @return {Phaser.Scenes.Systems} This Systems object.
*/
resume: function (data)
{
var events = this.events;
var settings = this.settings;
if (!this.settings.active)
{
settings.status = CONST.RUNNING;
settings.active = true;
events.emit(Events.RESUME, this, data);
}
return this;
},
/**
* Send this Scene to sleep.
*
* A sleeping Scene doesn't run its update step or render anything, but it also isn't shut down
* or has any of its systems or children removed, meaning it can be re-activated at any point and
* will carry on from where it left off. It also keeps everything in memory and events and callbacks
* from other Scenes may still invoke changes within it, so be careful what is left active.
*
* @method Phaser.Scenes.Systems#sleep
* @fires Phaser.Scenes.Events#SLEEP
* @since 3.0.0
*
* @param {object} [data] - A data object that will be passed in the 'sleep' event.
*
* @return {Phaser.Scenes.Systems} This Systems object.
*/
sleep: function (data)
{
var settings = this.settings;
var status = this.getStatus();
if (status !== CONST.CREATING && status !== CONST.RUNNING)
{
console.warn('Cannot sleep non-running Scene', settings.key);
}
else
{
settings.status = CONST.SLEEPING;
settings.active = false;
settings.visible = false;
this.events.emit(Events.SLEEP, this, data);
}
return this;
},
/**
* Wake-up this Scene if it was previously asleep.
*
* @method Phaser.Scenes.Systems#wake
* @fires Phaser.Scenes.Events#WAKE
* @since 3.0.0
*
* @param {object} [data] - A data object that will be passed in the 'wake' event.
*
* @return {Phaser.Scenes.Systems} This Systems object.
*/
wake: function (data)
{
var events = this.events;
var settings = this.settings;
settings.status = CONST.RUNNING;
settings.active = true;
settings.visible = true;
events.emit(Events.WAKE, this, data);
if (settings.isTransition)
{
events.emit(Events.TRANSITION_WAKE, settings.transitionFrom, settings.transitionDuration);
}
return this;
},
/**
* Returns any data that was sent to this Scene by another Scene.
*
* The data is also passed to `Scene.init` and in various Scene events, but
* you can access it at any point via this method.
*
* @method Phaser.Scenes.Systems#getData
* @since 3.22.0
*
* @return {any} The Scene Data.
*/
getData: function ()
{
return this.settings.data;
},
/**
* Returns the current status of this Scene.
*
* @method Phaser.Scenes.Systems#getStatus
* @since 3.60.0
*
* @return {number} The status of this Scene. One of the `Phaser.Scene` constants.
*/
getStatus: function ()
{
return this.settings.status;
},
/**
* Is this Scene sleeping?
*
* @method Phaser.Scenes.Systems#isSleeping
* @since 3.0.0
*
* @return {boolean} `true` if this Scene is asleep, otherwise `false`.
*/
isSleeping: function ()
{
return (this.settings.status === CONST.SLEEPING);
},
/**
* Is this Scene running?
*
* @method Phaser.Scenes.Systems#isActive
* @since 3.0.0
*
* @return {boolean} `true` if this Scene is running, otherwise `false`.
*/
isActive: function ()
{
return (this.settings.status === CONST.RUNNING);
},
/**
* Is this Scene paused?
*
* @method Phaser.Scenes.Systems#isPaused
* @since 3.13.0
*
* @return {boolean} `true` if this Scene is paused, otherwise `false`.
*/
isPaused: function ()
{
return (this.settings.status === CONST.PAUSED);
},
/**
* Is this Scene currently transitioning out to, or in from another Scene?
*
* @method Phaser.Scenes.Systems#isTransitioning
* @since 3.5.0
*
* @return {boolean} `true` if this Scene is currently transitioning, otherwise `false`.
*/
isTransitioning: function ()
{
return (this.settings.isTransition || this.scenePlugin._target !== null);
},
/**
* Is this Scene currently transitioning out from itself to another Scene?
*
* @method Phaser.Scenes.Systems#isTransitionOut
* @since 3.5.0
*
* @return {boolean} `true` if this Scene is in transition to another Scene, otherwise `false`.
*/
isTransitionOut: function ()
{
return (this.scenePlugin._target !== null && this.scenePlugin._duration > 0);
},
/**
* Is this Scene currently transitioning in from another Scene?
*
* @method Phaser.Scenes.Systems#isTransitionIn
* @since 3.5.0
*
* @return {boolean} `true` if this Scene is transitioning in from another Scene, otherwise `false`.
*/
isTransitionIn: function ()
{
return (this.settings.isTransition);
},
/**
* Is this Scene visible and rendering?
*
* @method Phaser.Scenes.Systems#isVisible
* @since 3.0.0
*
* @return {boolean} `true` if this Scene is visible, otherwise `false`.
*/
isVisible: function ()
{
return this.settings.visible;
},
/**
* Sets the visible state of this Scene.
* An invisible Scene will not render, but will still process updates.
*
* @method Phaser.Scenes.Systems#setVisible
* @since 3.0.0
*
* @param {boolean} value - `true` to render this Scene, otherwise `false`.
*
* @return {Phaser.Scenes.Systems} This Systems object.
*/
setVisible: function (value)
{
this.settings.visible = value;
return this;
},
/**
* Set the active state of this Scene.
*
* An active Scene will run its core update loop.
*
* @method Phaser.Scenes.Systems#setActive
* @since 3.0.0
*
* @param {boolean} value - If `true` the Scene will be resumed, if previously paused. If `false` it will be paused.
* @param {object} [data] - A data object that will be passed in the 'resume' or 'pause' events.
*
* @return {Phaser.Scenes.Systems} This Systems object.
*/
setActive: function (value, data)
{
if (value)
{
return this.resume(data);
}
else
{
return this.pause(data);
}
},
/**
* Start this Scene running and rendering.
* Called automatically by the SceneManager.
*
* @method Phaser.Scenes.Systems#start
* @fires Phaser.Scenes.Events#START
* @fires Phaser.Scenes.Events#READY
* @since 3.0.0
*
* @param {object} data - Optional data object that may have been passed to this Scene from another.
*/
start: function (data)
{
var events = this.events;
var settings = this.settings;
if (data)
{
settings.data = data;
}
settings.status = CONST.START;
settings.active = true;
settings.visible = true;
// For plugins to listen out for
events.emit(Events.START, this);
// For user-land code to listen out for
events.emit(Events.READY, this, data);
},
/**
* Shutdown this Scene and send a shutdown event to all of its systems.
* A Scene that has been shutdown will not run its update loop or render, but it does
* not destroy any of its plugins or references. It is put into hibernation for later use.
* If you don't ever plan to use this Scene again, then it should be destroyed instead
* to free-up resources.
*
* @method Phaser.Scenes.Systems#shutdown
* @fires Phaser.Scenes.Events#SHUTDOWN
* @since 3.0.0
*
* @param {object} [data] - A data object that will be passed in the 'shutdown' event.
*/
shutdown: function (data)
{
var events = this.events;
var settings = this.settings;
events.off(Events.TRANSITION_INIT);
events.off(Events.TRANSITION_START);
events.off(Events.TRANSITION_COMPLETE);
events.off(Events.TRANSITION_OUT);
settings.status = CONST.SHUTDOWN;
settings.active = false;
settings.visible = false;
if (this.renderer === GLOBAL_CONST.WEBGL)
{
this.renderer.resetTextures(true);
}
events.emit(Events.SHUTDOWN, this, data);
},
/**
* Destroy this Scene and send a destroy event all of its systems.
* A destroyed Scene cannot be restarted.
* You should not call this directly, instead use `SceneManager.remove`.
*
* @method Phaser.Scenes.Systems#destroy
* @private
* @fires Phaser.Scenes.Events#DESTROY
* @since 3.0.0
*/
destroy: function ()
{
var events = this.events;
var settings = this.settings;
settings.status = CONST.DESTROYED;
settings.active = false;
settings.visible = false;
events.emit(Events.DESTROY, this);
events.removeAllListeners();
var props = [ 'scene', 'game', 'anims', 'cache', 'plugins', 'registry', 'sound', 'textures', 'add', 'camera', 'displayList', 'events', 'make', 'scenePlugin', 'updateList' ];
for (var i = 0; i < props.length; i++)
{
this[props[i]] = null;
}
}
});
module.exports = Systems;