-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
DisplayList.js
270 lines (233 loc) · 7.31 KB
/
DisplayList.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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013-2023 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../utils/Class');
var List = require('../structs/List');
var PluginCache = require('../plugins/PluginCache');
var GameObjectEvents = require('./events');
var SceneEvents = require('../scene/events');
var StableSort = require('../utils/array/StableSort');
/**
* @classdesc
* The Display List plugin.
*
* Display Lists belong to a Scene and maintain the list of Game Objects to render every frame.
*
* Some of these Game Objects may also be part of the Scene's [Update List]{@link Phaser.GameObjects.UpdateList}, for updating.
*
* @class DisplayList
* @extends Phaser.Structs.List.<Phaser.GameObjects.GameObject>
* @memberof Phaser.GameObjects
* @constructor
* @since 3.0.0
*
* @param {Phaser.Scene} scene - The Scene that this Display List belongs to.
*/
var DisplayList = new Class({
Extends: List,
initialize:
function DisplayList (scene)
{
List.call(this, scene);
/**
* The flag the determines whether Game Objects should be sorted when `depthSort()` is called.
*
* @name Phaser.GameObjects.DisplayList#sortChildrenFlag
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.sortChildrenFlag = false;
/**
* The Scene that this Display List belongs to.
*
* @name Phaser.GameObjects.DisplayList#scene
* @type {Phaser.Scene}
* @since 3.0.0
*/
this.scene = scene;
/**
* The Scene's Systems.
*
* @name Phaser.GameObjects.DisplayList#systems
* @type {Phaser.Scenes.Systems}
* @since 3.0.0
*/
this.systems = scene.sys;
/**
* The Scene's Event Emitter.
*
* @name Phaser.GameObjects.DisplayList#events
* @type {Phaser.Events.EventEmitter}
* @since 3.50.0
*/
this.events = scene.sys.events;
// Set the List callbacks
this.addCallback = this.addChildCallback;
this.removeCallback = this.removeChildCallback;
this.events.once(SceneEvents.BOOT, this.boot, this);
this.events.on(SceneEvents.START, this.start, this);
},
/**
* This method is called automatically, only once, when the Scene is first created.
* Do not invoke it directly.
*
* @method Phaser.GameObjects.DisplayList#boot
* @private
* @since 3.5.1
*/
boot: function ()
{
this.events.once(SceneEvents.DESTROY, this.destroy, this);
},
/**
* Internal method called from `List.addCallback`.
*
* @method Phaser.GameObjects.DisplayList#addChildCallback
* @private
* @fires Phaser.Scenes.Events#ADDED_TO_SCENE
* @fires Phaser.GameObjects.Events#ADDED_TO_SCENE
* @since 3.50.0
*
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object that was added to the list.
*/
addChildCallback: function (gameObject)
{
if (gameObject.displayList && gameObject.displayList !== this)
{
gameObject.removeFromDisplayList();
}
if (gameObject.parentContainer)
{
gameObject.parentContainer.remove(gameObject);
}
if (!gameObject.displayList)
{
this.queueDepthSort();
gameObject.displayList = this;
gameObject.emit(GameObjectEvents.ADDED_TO_SCENE, gameObject, this.scene);
this.events.emit(SceneEvents.ADDED_TO_SCENE, gameObject, this.scene);
}
},
/**
* Internal method called from `List.removeCallback`.
*
* @method Phaser.GameObjects.DisplayList#removeChildCallback
* @private
* @fires Phaser.Scenes.Events#REMOVED_FROM_SCENE
* @fires Phaser.GameObjects.Events#REMOVED_FROM_SCENE
* @since 3.50.0
*
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object that was removed from the list.
*/
removeChildCallback: function (gameObject)
{
this.queueDepthSort();
gameObject.displayList = null;
gameObject.emit(GameObjectEvents.REMOVED_FROM_SCENE, gameObject, this.scene);
this.events.emit(SceneEvents.REMOVED_FROM_SCENE, gameObject, this.scene);
},
/**
* This method is called automatically by the Scene when it is starting up.
* It is responsible for creating local systems, properties and listening for Scene events.
* Do not invoke it directly.
*
* @method Phaser.GameObjects.DisplayList#start
* @private
* @since 3.5.0
*/
start: function ()
{
this.events.once(SceneEvents.SHUTDOWN, this.shutdown, this);
},
/**
* Force a sort of the display list on the next call to depthSort.
*
* @method Phaser.GameObjects.DisplayList#queueDepthSort
* @since 3.0.0
*/
queueDepthSort: function ()
{
this.sortChildrenFlag = true;
},
/**
* Immediately sorts the display list if the flag is set.
*
* @method Phaser.GameObjects.DisplayList#depthSort
* @since 3.0.0
*/
depthSort: function ()
{
if (this.sortChildrenFlag)
{
StableSort(this.list, this.sortByDepth);
this.sortChildrenFlag = false;
}
},
/**
* Compare the depth of two Game Objects.
*
* @method Phaser.GameObjects.DisplayList#sortByDepth
* @since 3.0.0
*
* @param {Phaser.GameObjects.GameObject} childA - The first Game Object.
* @param {Phaser.GameObjects.GameObject} childB - The second Game Object.
*
* @return {number} The difference between the depths of each Game Object.
*/
sortByDepth: function (childA, childB)
{
return childA._depth - childB._depth;
},
/**
* Returns an array which contains all objects currently on the Display List.
* This is a reference to the main list array, not a copy of it, so be careful not to modify it.
*
* @method Phaser.GameObjects.DisplayList#getChildren
* @since 3.12.0
*
* @return {Phaser.GameObjects.GameObject[]} The group members.
*/
getChildren: function ()
{
return this.list;
},
/**
* The Scene that owns this plugin is shutting down.
*
* We need to kill and reset all internal properties as well as stop listening to Scene events.
*
* @method Phaser.GameObjects.DisplayList#shutdown
* @private
* @since 3.0.0
*/
shutdown: function ()
{
var list = this.list;
while (list.length)
{
list[0].destroy(true);
}
this.events.off(SceneEvents.SHUTDOWN, this.shutdown, this);
},
/**
* The Scene that owns this plugin is being destroyed.
* We need to shutdown and then kill off all external references.
*
* @method Phaser.GameObjects.DisplayList#destroy
* @private
* @since 3.0.0
*/
destroy: function ()
{
this.shutdown();
this.events.off(SceneEvents.START, this.start, this);
this.scene = null;
this.systems = null;
this.events = null;
}
});
PluginCache.register('DisplayList', DisplayList, 'displayList');
module.exports = DisplayList;