2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2018-01-16 19:49:13 +00:00
|
|
|
var Class = require('../utils/Class');
|
2018-02-12 16:59:57 +00:00
|
|
|
var List = require('../structs/List');
|
2018-05-15 11:51:50 +00:00
|
|
|
var PluginCache = require('../plugins/PluginCache');
|
2018-01-16 19:49:13 +00:00
|
|
|
var StableSort = require('../utils/array/StableSort');
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
2018-06-01 10:31:37 +00:00
|
|
|
* 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.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @class DisplayList
|
2018-03-30 12:43:58 +00:00
|
|
|
* @extends Phaser.Structs.List.<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 this Display List belongs to.
|
2018-02-12 21:54:51 +00:00
|
|
|
*/
|
2017-07-04 00:59:31 +00:00
|
|
|
var DisplayList = new Class({
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2018-02-12 16:59:57 +00:00
|
|
|
Extends: List,
|
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
initialize:
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2018-02-12 16:59:57 +00:00
|
|
|
function DisplayList (scene)
|
2017-06-30 14:47:51 +00:00
|
|
|
{
|
2018-02-12 16:59:57 +00:00
|
|
|
List.call(this, scene);
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
2018-06-01 10:31:37 +00:00
|
|
|
* The flag the determines whether Game Objects should be sorted when `depthSort()` is called.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.DisplayList#sortChildrenFlag
|
|
|
|
* @type {boolean}
|
|
|
|
* @default false
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-11 13:59:06 +00:00
|
|
|
this.sortChildrenFlag = false;
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
2018-06-01 10:31:37 +00:00
|
|
|
* The Scene that this Display List belongs to.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.DisplayList#scene
|
|
|
|
* @type {Phaser.Scene}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-02-12 16:59:57 +00:00
|
|
|
this.scene = scene;
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
2018-06-01 10:31:37 +00:00
|
|
|
* The Scene's Systems.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.DisplayList#systems
|
|
|
|
* @type {Phaser.Scenes.Systems}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-02-12 16:59:57 +00:00
|
|
|
this.systems = scene.sys;
|
2018-01-11 13:59:06 +00:00
|
|
|
|
2018-04-17 11:25:45 +00:00
|
|
|
scene.sys.events.once('boot', this.boot, this);
|
2018-04-13 16:12:17 +00:00
|
|
|
scene.sys.events.on('start', this.start, this);
|
2018-01-11 13:59:06 +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.DisplayList#boot
|
|
|
|
* @private
|
|
|
|
* @since 3.5.1
|
|
|
|
*/
|
|
|
|
boot: function ()
|
|
|
|
{
|
|
|
|
this.systems.events.once('destroy', this.destroy, this);
|
|
|
|
},
|
|
|
|
|
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.DisplayList#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-01-11 13:59:06 +00:00
|
|
|
{
|
2018-04-17 11:25:45 +00:00
|
|
|
this.systems.events.once('shutdown', this.shutdown, this);
|
2018-01-11 13:59:06 +00:00
|
|
|
},
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
|
|
|
* Force a sort of the display list on the next call to depthSort.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.DisplayList#queueDepthSort
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-11 13:59:06 +00:00
|
|
|
queueDepthSort: function ()
|
|
|
|
{
|
|
|
|
this.sortChildrenFlag = true;
|
|
|
|
},
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
|
|
|
* Immediately sorts the display list if the flag is set.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.DisplayList#depthSort
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-11 13:59:06 +00:00
|
|
|
depthSort: function ()
|
|
|
|
{
|
|
|
|
if (this.sortChildrenFlag)
|
|
|
|
{
|
2018-02-12 16:59:57 +00:00
|
|
|
StableSort.inplace(this.list, this.sortByDepth);
|
2018-01-11 13:59:06 +00:00
|
|
|
|
|
|
|
this.sortChildrenFlag = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
2018-06-01 10:31:37 +00:00
|
|
|
* Compare the depth of two Game Objects.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.DisplayList#sortByDepth
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-01 10:31:37 +00:00
|
|
|
* @param {Phaser.GameObjects.GameObject} childA - The first Game Object.
|
|
|
|
* @param {Phaser.GameObjects.GameObject} childB - The second Game Object.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
2018-06-01 10:31:37 +00:00
|
|
|
* @return {integer} The difference between the depths of each Game Object.
|
2018-02-12 21:54:51 +00:00
|
|
|
*/
|
2018-02-12 16:59:57 +00:00
|
|
|
sortByDepth: function (childA, childB)
|
2016-12-07 01:40:56 +00:00
|
|
|
{
|
2018-02-12 16:59:57 +00:00
|
|
|
return childA._depth - childB._depth;
|
2016-12-07 01:40:56 +00:00
|
|
|
},
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
2018-06-01 10:31:37 +00:00
|
|
|
* Given an array of Game Objects, sort the array and return it, so that
|
|
|
|
* the objects are in index order with the lowest at the bottom.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.DisplayList#sortGameObjects
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-01 10:31:37 +00:00
|
|
|
* @param {Phaser.GameObjects.GameObject[]} gameObjects - The array of Game Objects to sort.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
2018-06-01 10:31:37 +00:00
|
|
|
* @return {array} The sorted array of Game Objects.
|
2018-02-12 21:54:51 +00:00
|
|
|
*/
|
2017-07-20 11:50:38 +00:00
|
|
|
sortGameObjects: function (gameObjects)
|
|
|
|
{
|
|
|
|
if (gameObjects === undefined) { gameObjects = this.list; }
|
|
|
|
|
|
|
|
this.scene.sys.depthSort();
|
|
|
|
|
|
|
|
return gameObjects.sort(this.sortIndexHandler.bind(this));
|
|
|
|
},
|
|
|
|
|
2018-02-12 21:54:51 +00:00
|
|
|
/**
|
2018-06-01 10:31:37 +00:00
|
|
|
* Get the top-most Game Object in the given array of Game Objects, after sorting it.
|
|
|
|
*
|
2018-02-12 21:54:51 +00:00
|
|
|
* Note that the given array is sorted in place, even though it isn't returned directly it will still be updated.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.DisplayList#getTopGameObject
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-01 10:31:37 +00:00
|
|
|
* @param {Phaser.GameObjects.GameObject[]} gameObjects - The array of Game Objects.
|
2018-02-12 21:54:51 +00:00
|
|
|
*
|
2018-06-01 10:31:37 +00:00
|
|
|
* @return {Phaser.GameObjects.GameObject} The top-most Game Object in the array of Game Objects.
|
2018-02-12 21:54:51 +00:00
|
|
|
*/
|
2017-07-20 11:50:38 +00:00
|
|
|
getTopGameObject: function (gameObjects)
|
|
|
|
{
|
|
|
|
this.sortGameObjects(gameObjects);
|
|
|
|
|
|
|
|
return gameObjects[gameObjects.length - 1];
|
2018-04-13 16:12:17 +00:00
|
|
|
},
|
|
|
|
|
2018-08-01 17:28:06 +00:00
|
|
|
/**
|
|
|
|
* All members of the group.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.DisplayList#getChildren
|
|
|
|
* @since 3.12.0
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.GameObject[]} The group members.
|
|
|
|
*/
|
|
|
|
getChildren: function ()
|
|
|
|
{
|
|
|
|
return this.list;
|
|
|
|
},
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.DisplayList#shutdown
|
|
|
|
* @private
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
shutdown: function ()
|
|
|
|
{
|
2018-09-13 08:29:48 +00:00
|
|
|
var i = this.list.length;
|
|
|
|
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
this.list[i].destroy(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.list.length = 0;
|
2018-04-13 16:12:17 +00:00
|
|
|
|
2018-04-17 11:25:45 +00:00
|
|
|
this.systems.events.off('shutdown', this.shutdown, this);
|
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.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.DisplayList#destroy
|
|
|
|
* @private
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.shutdown();
|
|
|
|
|
|
|
|
this.scene.sys.events.off('start', this.start, this);
|
|
|
|
|
|
|
|
this.scene = null;
|
|
|
|
this.systems = null;
|
2016-12-07 01:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-05-15 11:51:50 +00:00
|
|
|
PluginCache.register('DisplayList', DisplayList, 'displayList');
|
2018-02-12 16:59:57 +00:00
|
|
|
|
2017-07-04 00:59:31 +00:00
|
|
|
module.exports = DisplayList;
|