2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2019-01-15 16:20:22 +00:00
|
|
|
* @copyright 2019 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');
|
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
|
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({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
function UpdateList (scene)
|
2017-07-04 15:44:16 +00:00
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
/**
|
2018-06-01 10:31:37 +00:00
|
|
|
* The list of Game Objects.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.UpdateList#_list
|
|
|
|
* @type {array}
|
|
|
|
* @private
|
|
|
|
* @default []
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-07 17:14:27 +00:00
|
|
|
this._list = [];
|
2018-02-12 21:54:51 +00:00
|
|
|
|
|
|
|
/**
|
2018-06-01 10:31:37 +00:00
|
|
|
* Game Objects that are pending insertion into the list.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.UpdateList#_pendingInsertion
|
|
|
|
* @type {array}
|
|
|
|
* @private
|
|
|
|
* @default []
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 15:44:16 +00:00
|
|
|
this._pendingInsertion = [];
|
2018-02-12 21:54:51 +00:00
|
|
|
|
|
|
|
/**
|
2018-06-01 10:31:37 +00:00
|
|
|
* Game Objects that are pending removal from the list.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.UpdateList#_pendingRemoval
|
|
|
|
* @type {array}
|
|
|
|
* @private
|
|
|
|
* @default []
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 15:44:16 +00:00
|
|
|
this._pendingRemoval = [];
|
2018-04-13 16:12:17 +00:00
|
|
|
|
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-01-18 13:41:43 +00:00
|
|
|
eventEmitter.on(SceneEvents.PRE_UPDATE, this.preUpdate, this);
|
|
|
|
eventEmitter.on(SceneEvents.UPDATE, this.update, this);
|
|
|
|
eventEmitter.once(SceneEvents.SHUTDOWN, this.shutdown, this);
|
2018-02-12 21:54:51 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-06-01 10:31:37 +00:00
|
|
|
* Add a Game Object to the Update List.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.UpdateList#add
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-01 10:31:37 +00:00
|
|
|
* @param {Phaser.GameObjects.GameObject} child - The Game Object to add.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
2018-06-01 10:31:37 +00:00
|
|
|
* @return {Phaser.GameObjects.GameObject} The added Game Object.
|
2018-02-12 21:54:51 +00:00
|
|
|
*/
|
2017-07-04 15:44:16 +00:00
|
|
|
add: function (child)
|
|
|
|
{
|
2017-11-13 23:31:13 +00:00
|
|
|
// Is child already in this list?
|
|
|
|
|
|
|
|
if (this._list.indexOf(child) === -1 && this._pendingInsertion.indexOf(child) === -1)
|
|
|
|
{
|
|
|
|
this._pendingInsertion.push(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
return child;
|
2017-07-04 15:44:16 +00:00
|
|
|
},
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
2018-06-01 10:31:37 +00:00
|
|
|
* The pre-update step.
|
|
|
|
*
|
|
|
|
* Handles Game Objects that are pending insertion to and removal from the list.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.UpdateList#preUpdate
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-02-16 19:08:50 +00:00
|
|
|
preUpdate: function ()
|
2017-07-04 15:44:16 +00:00
|
|
|
{
|
|
|
|
var toRemove = this._pendingRemoval.length;
|
|
|
|
var toInsert = this._pendingInsertion.length;
|
|
|
|
|
|
|
|
if (toRemove === 0 && toInsert === 0)
|
|
|
|
{
|
|
|
|
// Quick bail
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var i;
|
2017-07-04 22:43:13 +00:00
|
|
|
var gameObject;
|
2017-07-04 15:44:16 +00:00
|
|
|
|
2017-07-04 22:43:13 +00:00
|
|
|
// Delete old gameObjects
|
2017-07-04 15:44:16 +00:00
|
|
|
for (i = 0; i < toRemove; i++)
|
|
|
|
{
|
2017-07-04 22:43:13 +00:00
|
|
|
gameObject = this._pendingRemoval[i];
|
2017-07-04 15:44:16 +00:00
|
|
|
|
2017-07-07 17:14:27 +00:00
|
|
|
var index = this._list.indexOf(gameObject);
|
2017-07-04 15:44:16 +00:00
|
|
|
|
|
|
|
if (index > -1)
|
|
|
|
{
|
2017-07-07 17:14:27 +00:00
|
|
|
this._list.splice(index, 1);
|
2017-07-04 15:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 22:43:13 +00:00
|
|
|
// Move pending to active
|
2017-07-07 17:14:27 +00:00
|
|
|
this._list = this._list.concat(this._pendingInsertion.splice(0));
|
2017-07-04 15:44:16 +00:00
|
|
|
|
|
|
|
// Clear the lists
|
|
|
|
this._pendingRemoval.length = 0;
|
|
|
|
this._pendingInsertion.length = 0;
|
|
|
|
},
|
|
|
|
|
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
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.UpdateList#update
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
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
|
|
|
*/
|
2017-07-04 15:44:16 +00:00
|
|
|
update: function (time, delta)
|
|
|
|
{
|
2017-07-07 17:14:27 +00:00
|
|
|
for (var i = 0; i < this._list.length; i++)
|
2017-07-04 15:44:16 +00:00
|
|
|
{
|
2017-07-07 17:14:27 +00:00
|
|
|
var gameObject = this._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-06-01 10:31:37 +00:00
|
|
|
* Remove a Game Object from the list.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.UpdateList#remove
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-01 10:31:37 +00:00
|
|
|
* @param {Phaser.GameObjects.GameObject} child - The Game Object to remove from the list.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
2018-06-01 10:31:37 +00:00
|
|
|
* @return {Phaser.GameObjects.GameObject} The removed Game Object.
|
2018-02-12 21:54:51 +00:00
|
|
|
*/
|
2017-08-07 16:14:13 +00:00
|
|
|
remove: function (child)
|
2017-07-04 15:44:16 +00:00
|
|
|
{
|
2019-04-24 12:58:05 +00:00
|
|
|
var index = this._pendingRemoval.indexOf(child);
|
2017-07-04 15:44:16 +00:00
|
|
|
|
2017-08-07 16:14:13 +00:00
|
|
|
if (index !== -1)
|
2017-07-04 15:44:16 +00:00
|
|
|
{
|
2019-04-24 12:58:05 +00:00
|
|
|
this._pendingRemoval.push(child);
|
2017-07-04 15:44:16 +00:00
|
|
|
}
|
2018-06-01 10:31:37 +00:00
|
|
|
|
2017-08-07 16:14:13 +00:00
|
|
|
return child;
|
|
|
|
},
|
2017-07-04 15:44:16 +00:00
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
2018-06-01 10:31:37 +00:00
|
|
|
* Remove all Game Objects from the list.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.UpdateList#removeAll
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-06 14:11:40 +00:00
|
|
|
* @return {Phaser.GameObjects.UpdateList} This UpdateList.
|
2018-02-12 21:54:51 +00:00
|
|
|
*/
|
2017-08-07 16:14:13 +00:00
|
|
|
removeAll: function ()
|
|
|
|
{
|
|
|
|
var i = this._list.length;
|
2017-07-04 15:44:16 +00:00
|
|
|
|
2017-08-07 16:14:13 +00:00
|
|
|
while (i--)
|
2017-07-04 15:44:16 +00:00
|
|
|
{
|
2017-08-07 16:14:13 +00:00
|
|
|
this.remove(this._list[i]);
|
2017-07-04 15:44:16 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 16:14:13 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
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.
|
|
|
|
* 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 ()
|
|
|
|
{
|
2018-09-13 08:29:33 +00:00
|
|
|
var i = this._list.length;
|
|
|
|
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
this._list[i].destroy(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
i = this._pendingRemoval.length;
|
|
|
|
|
|
|
|
while (i--)
|
|
|
|
{
|
2018-09-23 20:03:18 +00:00
|
|
|
this._pendingRemoval[i].destroy(true);
|
2018-09-13 08:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
i = this._pendingInsertion.length;
|
|
|
|
|
|
|
|
while (i--)
|
|
|
|
{
|
2018-09-23 20:03:18 +00:00
|
|
|
this._pendingInsertion[i].destroy(true);
|
2018-09-13 08:29:33 +00:00
|
|
|
}
|
2017-08-07 16:14:13 +00:00
|
|
|
|
2017-07-07 17:14:27 +00:00
|
|
|
this._list.length = 0;
|
2017-07-04 15:44:16 +00:00
|
|
|
this._pendingRemoval.length = 0;
|
|
|
|
this._pendingInsertion.length = 0;
|
2018-04-13 16:12:17 +00:00
|
|
|
|
|
|
|
var eventEmitter = this.systems.events;
|
|
|
|
|
2019-01-18 13:41:43 +00:00
|
|
|
eventEmitter.off(SceneEvents.PRE_UPDATE, this.preUpdate, this);
|
|
|
|
eventEmitter.off(SceneEvents.UPDATE, this.update, this);
|
|
|
|
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.
|
|
|
|
* 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-01-18 13:41:43 +00:00
|
|
|
this.scene.sys.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;
|
2018-06-03 03:59:01 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-06-06 14:11:40 +00:00
|
|
|
* The length of the list.
|
2018-06-03 03:59:01 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.UpdateList#length
|
|
|
|
* @type {integer}
|
2018-10-09 12:40:00 +00:00
|
|
|
* @readonly
|
2018-06-03 03:59:01 +00:00
|
|
|
* @since 3.10.0
|
|
|
|
*/
|
|
|
|
length: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._list.length;
|
|
|
|
}
|
|
|
|
|
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;
|