2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2022-02-28 14:29:51 +00:00
|
|
|
* @copyright 2022 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2018-01-16 19:49:13 +00:00
|
|
|
var Class = require('../utils/Class');
|
2019-10-02 11:13:43 +00:00
|
|
|
var ProcessQueue = require('../structs/ProcessQueue');
|
2018-05-15 11:51:50 +00:00
|
|
|
var PluginCache = require('../plugins/PluginCache');
|
2019-01-18 13:41:43 +00:00
|
|
|
var SceneEvents = require('../scene/events');
|
2017-07-04 15:44:16 +00:00
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
2018-06-01 10:31:37 +00:00
|
|
|
* 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.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @class UpdateList
|
2019-10-16 05:39:06 +00:00
|
|
|
* @extends Phaser.Structs.ProcessQueue.<Phaser.GameObjects.GameObject>
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser.GameObjects
|
2018-02-12 21:54:51 +00:00
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-01 10:31:37 +00:00
|
|
|
* @param {Phaser.Scene} scene - The Scene that the Update List belongs to.
|
2018-02-12 21:54:51 +00:00
|
|
|
*/
|
2017-07-04 15:44:16 +00:00
|
|
|
var UpdateList = new Class({
|
|
|
|
|
2019-10-02 11:13:43 +00:00
|
|
|
Extends: ProcessQueue,
|
|
|
|
|
2017-07-04 15:44:16 +00:00
|
|
|
initialize:
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
function UpdateList (scene)
|
2017-07-04 15:44:16 +00:00
|
|
|
{
|
2019-10-02 11:13:43 +00:00
|
|
|
ProcessQueue.call(this);
|
|
|
|
|
2020-08-24 18:21:35 +00:00
|
|
|
// No duplicates in this list
|
|
|
|
this.checkQueue = true;
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
2018-06-01 10:31:37 +00:00
|
|
|
* The Scene that the Update List belongs to.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.UpdateList#scene
|
|
|
|
* @type {Phaser.Scene}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
this.scene = scene;
|
|
|
|
|
|
|
|
/**
|
2018-06-01 10:31:37 +00:00
|
|
|
* The Scene's Systems.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.UpdateList#systems
|
|
|
|
* @type {Phaser.Scenes.Systems}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
this.systems = scene.sys;
|
|
|
|
|
2019-10-11 17:53:11 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2020-11-23 10:22:13 +00:00
|
|
|
* @type {number}
|
2019-10-11 17:53:11 +00:00
|
|
|
* @private
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
|
2019-01-18 13:41:43 +00:00
|
|
|
scene.sys.events.once(SceneEvents.BOOT, this.boot, this);
|
|
|
|
scene.sys.events.on(SceneEvents.START, this.start, this);
|
2017-07-04 15:44:16 +00:00
|
|
|
},
|
|
|
|
|
2018-04-17 11:25:45 +00:00
|
|
|
/**
|
|
|
|
* 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 ()
|
|
|
|
{
|
2019-01-18 13:41:43 +00:00
|
|
|
this.systems.events.once(SceneEvents.DESTROY, this.destroy, this);
|
2018-04-17 11:25:45 +00:00
|
|
|
},
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
2018-04-13 16:12:17 +00:00
|
|
|
* 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.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
2018-04-13 16:12:17 +00:00
|
|
|
* @method Phaser.GameObjects.UpdateList#start
|
|
|
|
* @private
|
2018-04-15 11:44:47 +00:00
|
|
|
* @since 3.5.0
|
2018-02-12 21:54:51 +00:00
|
|
|
*/
|
2018-04-13 16:12:17 +00:00
|
|
|
start: function ()
|
2018-02-12 21:54:51 +00:00
|
|
|
{
|
|
|
|
var eventEmitter = this.systems.events;
|
|
|
|
|
2019-10-02 11:13:43 +00:00
|
|
|
eventEmitter.on(SceneEvents.PRE_UPDATE, this.update, this);
|
|
|
|
eventEmitter.on(SceneEvents.UPDATE, this.sceneUpdate, this);
|
2019-01-18 13:41:43 +00:00
|
|
|
eventEmitter.once(SceneEvents.SHUTDOWN, this.shutdown, this);
|
2018-02-12 21:54:51 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-06-01 10:31:37 +00:00
|
|
|
* The update step.
|
|
|
|
*
|
|
|
|
* Pre-updates every active Game Object in the list.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
2019-10-02 11:13:43 +00:00
|
|
|
* @method Phaser.GameObjects.UpdateList#sceneUpdate
|
|
|
|
* @since 3.20.0
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
2018-06-01 10:31:37 +00:00
|
|
|
* @param {number} time - The current timestamp.
|
|
|
|
* @param {number} delta - The delta time elapsed since the last frame.
|
2018-02-12 21:54:51 +00:00
|
|
|
*/
|
2019-10-02 11:13:43 +00:00
|
|
|
sceneUpdate: function (time, delta)
|
2017-07-04 15:44:16 +00:00
|
|
|
{
|
2019-10-02 11:13:43 +00:00
|
|
|
var list = this._active;
|
2019-07-03 14:47:40 +00:00
|
|
|
var length = list.length;
|
|
|
|
|
|
|
|
for (var i = 0; i < length; i++)
|
2017-07-04 15:44:16 +00:00
|
|
|
{
|
2019-07-03 14:47:40 +00:00
|
|
|
var gameObject = list[i];
|
2017-07-04 15:44:16 +00:00
|
|
|
|
2017-07-07 17:14:27 +00:00
|
|
|
if (gameObject.active)
|
|
|
|
{
|
|
|
|
gameObject.preUpdate.call(gameObject, time, delta);
|
|
|
|
}
|
2017-07-04 15:44:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
2018-04-13 16:12:17 +00:00
|
|
|
* The Scene that owns this plugin is shutting down.
|
2020-08-24 18:21:35 +00:00
|
|
|
*
|
2018-04-13 16:12:17 +00:00
|
|
|
* We need to kill and reset all internal properties as well as stop listening to Scene events.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.UpdateList#shutdown
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-08-07 16:14:13 +00:00
|
|
|
shutdown: function ()
|
|
|
|
{
|
2019-10-02 11:13:43 +00:00
|
|
|
var i = this._active.length;
|
2018-09-13 08:29:33 +00:00
|
|
|
|
|
|
|
while (i--)
|
|
|
|
{
|
2019-10-02 11:13:43 +00:00
|
|
|
this._active[i].destroy(true);
|
2018-09-13 08:29:33 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 11:13:43 +00:00
|
|
|
i = this._pending.length;
|
2018-09-13 08:29:33 +00:00
|
|
|
|
|
|
|
while (i--)
|
|
|
|
{
|
2019-10-02 11:13:43 +00:00
|
|
|
this._pending[i].destroy(true);
|
2018-09-13 08:29:33 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 11:13:43 +00:00
|
|
|
i = this._destroy.length;
|
2018-09-13 08:29:33 +00:00
|
|
|
|
|
|
|
while (i--)
|
|
|
|
{
|
2019-10-02 11:13:43 +00:00
|
|
|
this._destroy[i].destroy(true);
|
2018-09-13 08:29:33 +00:00
|
|
|
}
|
2017-08-07 16:14:13 +00:00
|
|
|
|
2019-10-02 11:13:43 +00:00
|
|
|
this._toProcess = 0;
|
|
|
|
|
|
|
|
this._pending = [];
|
|
|
|
this._active = [];
|
|
|
|
this._destroy = [];
|
2018-04-13 16:12:17 +00:00
|
|
|
|
2019-10-15 10:18:36 +00:00
|
|
|
this.removeAllListeners();
|
|
|
|
|
2018-04-13 16:12:17 +00:00
|
|
|
var eventEmitter = this.systems.events;
|
|
|
|
|
2021-04-03 02:02:14 +00:00
|
|
|
eventEmitter.off(SceneEvents.PRE_UPDATE, this.update, this);
|
2019-10-15 10:18:36 +00:00
|
|
|
eventEmitter.off(SceneEvents.UPDATE, this.sceneUpdate, this);
|
2019-01-18 13:41:43 +00:00
|
|
|
eventEmitter.off(SceneEvents.SHUTDOWN, this.shutdown, this);
|
2017-07-04 15:44:16 +00:00
|
|
|
},
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
2018-04-13 16:12:17 +00:00
|
|
|
* The Scene that owns this plugin is being destroyed.
|
2020-08-24 18:21:35 +00:00
|
|
|
*
|
2018-04-13 16:12:17 +00:00
|
|
|
* We need to shutdown and then kill off all external references.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.UpdateList#destroy
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 15:44:16 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.shutdown();
|
2018-02-12 21:54:51 +00:00
|
|
|
|
2019-10-15 10:18:36 +00:00
|
|
|
this.systems.events.off(SceneEvents.START, this.start, this);
|
2018-02-12 21:54:51 +00:00
|
|
|
|
2018-04-13 16:12:17 +00:00
|
|
|
this.scene = null;
|
|
|
|
this.systems = null;
|
2017-07-04 15:44:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 17:53:11 +00:00
|
|
|
/**
|
|
|
|
* Adds a new item to the Update List.
|
2020-08-24 18:21:35 +00:00
|
|
|
*
|
2019-10-11 17:53:11 +00:00
|
|
|
* 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.
|
2020-08-24 18:21:35 +00:00
|
|
|
*
|
2019-10-11 17:53:11 +00:00
|
|
|
* 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.
|
2020-08-24 18:21:35 +00:00
|
|
|
*
|
2019-10-11 17:53:11 +00:00
|
|
|
* 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.
|
2020-08-24 18:21:35 +00:00
|
|
|
*
|
2019-10-11 17:53:11 +00:00
|
|
|
* 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.
|
2020-08-24 18:21:35 +00:00
|
|
|
*
|
2019-10-11 17:53:11 +00:00
|
|
|
* 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
|
2020-11-23 10:22:13 +00:00
|
|
|
* @type {number}
|
2019-10-11 17:53:11 +00:00
|
|
|
* @readonly
|
|
|
|
* @since 3.20.0
|
|
|
|
*/
|
2017-07-04 15:44:16 +00:00
|
|
|
});
|
|
|
|
|
2018-05-15 11:51:50 +00:00
|
|
|
PluginCache.register('UpdateList', UpdateList, 'updateList');
|
2018-02-12 21:54:51 +00:00
|
|
|
|
2017-07-04 15:44:16 +00:00
|
|
|
module.exports = UpdateList;
|