-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
UpdateList.js
298 lines (257 loc) · 7.89 KB
/
UpdateList.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
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../utils/Class');
var ProcessQueue = require('../structs/ProcessQueue');
var PluginCache = require('../plugins/PluginCache');
var SceneEvents = require('../scene/events');
/**
* @classdesc
* The Update List plugin.
*
* Update Lists belong to a Scene and maintain the list Game Objects to be updated every frame.
*
* Some or all of these Game Objects may also be part of the Scene's [Display List]{@link Phaser.GameObjects.DisplayList}, for Rendering.
*
* @class UpdateList
* @extends Phaser.Structs.ProcessQueue.<Phaser.GameObjects.GameObject>
* @memberof Phaser.GameObjects
* @constructor
* @since 3.0.0
*
* @param {Phaser.Scene} scene - The Scene that the Update List belongs to.
*/
var UpdateList = new Class({
Extends: ProcessQueue,
initialize:
function UpdateList (scene)
{
ProcessQueue.call(this);
// No duplicates in this list
this.checkQueue = true;
/**
* The Scene that the Update List belongs to.
*
* @name Phaser.GameObjects.UpdateList#scene
* @type {Phaser.Scene}
* @since 3.0.0
*/
this.scene = scene;
/**
* The Scene's Systems.
*
* @name Phaser.GameObjects.UpdateList#systems
* @type {Phaser.Scenes.Systems}
* @since 3.0.0
*/
this.systems = scene.sys;
/**
* The `pending` list is a selection of items which are due to be made 'active' in the next update.
*
* @name Phaser.GameObjects.UpdateList#_pending
* @type {Array.<*>}
* @private
* @default []
* @since 3.20.0
*/
/**
* The `active` list is a selection of items which are considered active and should be updated.
*
* @name Phaser.GameObjects.UpdateList#_active
* @type {Array.<*>}
* @private
* @default []
* @since 3.20.0
*/
/**
* The `destroy` list is a selection of items that were active and are awaiting being destroyed in the next update.
*
* @name Phaser.GameObjects.UpdateList#_destroy
* @type {Array.<*>}
* @private
* @default []
* @since 3.20.0
*/
/**
* The total number of items awaiting processing.
*
* @name Phaser.GameObjects.UpdateList#_toProcess
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
scene.sys.events.once(SceneEvents.BOOT, this.boot, this);
scene.sys.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.UpdateList#boot
* @private
* @since 3.5.1
*/
boot: function ()
{
this.systems.events.once(SceneEvents.DESTROY, this.destroy, this);
},
/**
* 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.UpdateList#start
* @private
* @since 3.5.0
*/
start: function ()
{
var eventEmitter = this.systems.events;
eventEmitter.on(SceneEvents.PRE_UPDATE, this.update, this);
eventEmitter.on(SceneEvents.UPDATE, this.sceneUpdate, this);
eventEmitter.once(SceneEvents.SHUTDOWN, this.shutdown, this);
},
/**
* The update step.
*
* Pre-updates every active Game Object in the list.
*
* @method Phaser.GameObjects.UpdateList#sceneUpdate
* @since 3.20.0
*
* @param {number} time - The current timestamp.
* @param {number} delta - The delta time elapsed since the last frame.
*/
sceneUpdate: function (time, delta)
{
var list = this._active;
var length = list.length;
for (var i = 0; i < length; i++)
{
var gameObject = list[i];
if (gameObject.active)
{
gameObject.preUpdate.call(gameObject, time, delta);
}
}
},
/**
* 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.UpdateList#shutdown
* @since 3.0.0
*/
shutdown: function ()
{
var i = this._active.length;
while (i--)
{
this._active[i].destroy(true);
}
i = this._pending.length;
while (i--)
{
this._pending[i].destroy(true);
}
i = this._destroy.length;
while (i--)
{
this._destroy[i].destroy(true);
}
this._toProcess = 0;
this._pending = [];
this._active = [];
this._destroy = [];
this.removeAllListeners();
var eventEmitter = this.systems.events;
eventEmitter.off(SceneEvents.PRE_UPDATE, this.update, this);
eventEmitter.off(SceneEvents.UPDATE, this.sceneUpdate, this);
eventEmitter.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.UpdateList#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.shutdown();
this.systems.events.off(SceneEvents.START, this.start, this);
this.scene = null;
this.systems = null;
}
/**
* Adds a new item to the Update List.
*
* The item is added to the pending list and made active in the next update.
*
* @method Phaser.GameObjects.UpdateList#add
* @since 3.0.0
*
* @param {*} item - The item to add to the queue.
*
* @return {*} The item that was added.
*/
/**
* Removes an item from the Update List.
*
* The item is added to the pending destroy and fully removed in the next update.
*
* @method Phaser.GameObjects.UpdateList#remove
* @since 3.0.0
*
* @param {*} item - The item to be removed from the queue.
*
* @return {*} The item that was removed.
*/
/**
* Removes all active items from this Update List.
*
* All the items are marked as 'pending destroy' and fully removed in the next update.
*
* @method Phaser.GameObjects.UpdateList#removeAll
* @since 3.20.0
*
* @return {this} This Update List object.
*/
/**
* Update this queue. First it will process any items awaiting destruction, and remove them.
*
* Then it will check to see if there are any items pending insertion, and move them to an
* active state. Finally, it will return a list of active items for further processing.
*
* @method Phaser.GameObjects.UpdateList#update
* @since 3.0.0
*
* @return {Array.<*>} A list of active items.
*/
/**
* Returns the current list of active items.
*
* This method returns a reference to the active list array, not a copy of it.
* Therefore, be careful to not modify this array outside of the ProcessQueue.
*
* @method Phaser.GameObjects.UpdateList#getActive
* @since 3.0.0
*
* @return {Array.<*>} A list of active items.
*/
/**
* The number of entries in the active list.
*
* @name Phaser.GameObjects.UpdateList#length
* @type {number}
* @readonly
* @since 3.20.0
*/
});
PluginCache.register('UpdateList', UpdateList, 'updateList');
module.exports = UpdateList;