phaser/src/gameobjects/UpdateList.js

185 lines
4.7 KiB
JavaScript
Raw Normal View History

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');
var ProcessQueue = require('../structs/ProcessQueue');
var PluginCache = require('../plugins/PluginCache');
2019-01-18 13:41:43 +00:00
var SceneEvents = require('../scene/events');
2018-02-12 21:54:51 +00:00
/**
* @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.
2018-02-12 21:54:51 +00:00
*
* @class UpdateList
* @extends Phaser.Structs.ProcessQueue
2018-10-10 09:49:13 +00:00
* @memberof Phaser.GameObjects
2018-02-12 21:54:51 +00:00
* @constructor
* @since 3.0.0
*
* @param {Phaser.Scene} scene - The Scene that the Update List belongs to.
2018-02-12 21:54:51 +00:00
*/
var UpdateList = new Class({
Extends: ProcessQueue,
initialize:
2018-02-12 21:54:51 +00:00
function UpdateList (scene)
{
ProcessQueue.call(this);
2018-02-12 21:54:51 +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;
/**
* 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-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);
},
/**
* 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-02-12 21:54:51 +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
*
* @method Phaser.GameObjects.UpdateList#start
* @private
* @since 3.5.0
2018-02-12 21:54:51 +00:00
*/
start: function ()
2018-02-12 21:54:51 +00:00
{
var eventEmitter = this.systems.events;
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
},
/**
* The update step.
*
* Pre-updates every active Game Object in the list.
2018-02-12 21:54:51 +00:00
*
* @method Phaser.GameObjects.UpdateList#sceneUpdate
* @since 3.20.0
2018-02-12 21:54:51 +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
*/
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);
}
}
},
2018-02-12 21:54:51 +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
*/
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 = [];
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);
},
2018-02-12 21:54:51 +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
*/
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
this.scene = null;
this.systems = null;
}
});
PluginCache.register('UpdateList', UpdateList, 'updateList');
2018-02-12 21:54:51 +00:00
module.exports = UpdateList;