mirror of
https://github.com/photonstorm/phaser
synced 2025-02-17 14:38:30 +00:00
Extends List
This commit is contained in:
parent
7fbb055528
commit
13df64c01f
1 changed files with 166 additions and 55 deletions
|
@ -8,6 +8,7 @@
|
|||
var Class = require('../../utils/Class');
|
||||
var Components = require('../components');
|
||||
var GameObject = require('../GameObject');
|
||||
var List = require('../../structs/List');
|
||||
var Render = require('./ContainerRender');
|
||||
var Vector2 = require('../../math/Vector2');
|
||||
|
||||
|
@ -23,10 +24,12 @@ var Vector2 = require('../../math/Vector2');
|
|||
*
|
||||
* @extends Phaser.GameObjects.Components.BlendMode
|
||||
* @extends Phaser.GameObjects.Components.Transform
|
||||
* @extends Phaser.Structs.List
|
||||
*
|
||||
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
|
||||
* @param {number} x - The horizontal position of this Game Object in the world.
|
||||
* @param {number} y - The vertical position of this Game Object in the world.
|
||||
* @param {number} [x=0] - The horizontal position of this Game Object in the world.
|
||||
* @param {number} [y=0] - The vertical position of this Game Object in the world.
|
||||
* @param {Phaser.GameObjects.GameObject[]} [children] - An optional array of Game Objects to add to this Container.
|
||||
*/
|
||||
var Container = new Class({
|
||||
|
||||
|
@ -35,17 +38,46 @@ var Container = new Class({
|
|||
Mixins: [
|
||||
Components.BlendMode,
|
||||
Components.Transform,
|
||||
Render
|
||||
Render,
|
||||
List
|
||||
],
|
||||
|
||||
initialize:
|
||||
|
||||
function Container (scene, x, y)
|
||||
function Container (scene, x, y, children)
|
||||
{
|
||||
GameObject.call(this, scene, 'Container');
|
||||
|
||||
/**
|
||||
* [description]
|
||||
* The parent property as used by the List methods.
|
||||
*
|
||||
* @name Phaser.GameObjects.Container#parent
|
||||
* @type {Phaser.GameObjects.Container}
|
||||
* @private
|
||||
* @since 3.4.0
|
||||
*/
|
||||
this.parent = this;
|
||||
|
||||
/**
|
||||
* An array holding the children of this Container.
|
||||
*
|
||||
* @name Phaser.GameObjects.Container#list
|
||||
* @type {Phaser.GameObjects.GameObject[]}
|
||||
* @since 3.4.0
|
||||
*/
|
||||
this.list = [];
|
||||
|
||||
/**
|
||||
* The cursor position.
|
||||
*
|
||||
* @name Phaser.GameObjects.Container#position
|
||||
* @type {integer}
|
||||
* @since 3.4.0
|
||||
*/
|
||||
this.position = 0;
|
||||
|
||||
/**
|
||||
* The parent Container of this Container, if there is one.
|
||||
*
|
||||
* @name Phaser.GameObjects.Container#parentContainer
|
||||
* @type {Phaser.GameObjects.Container}
|
||||
|
@ -53,15 +85,6 @@ var Container = new Class({
|
|||
*/
|
||||
this.parentContainer = null;
|
||||
|
||||
/**
|
||||
* [description]
|
||||
*
|
||||
* @name Phaser.GameObjects.Container#children
|
||||
* @type {Phaser.GameObjects.GameObject[]}
|
||||
* @since 3.4.0
|
||||
*/
|
||||
this.children = [];
|
||||
|
||||
/**
|
||||
* [description]
|
||||
*
|
||||
|
@ -76,73 +99,77 @@ var Container = new Class({
|
|||
*
|
||||
* @name Phaser.GameObjects.Container#tempTransformMatrix
|
||||
* @type {Phaser.GameObjects.Components.TransformMatrix}
|
||||
* @private
|
||||
* @since 3.4.0
|
||||
*/
|
||||
this.tempTransformMatrix = new Components.TransformMatrix();
|
||||
|
||||
/**
|
||||
* A callback that is invoked every time a child is added to this list.
|
||||
*
|
||||
* @name Phaser.GameObjects.Container#addCallback
|
||||
* @type {function}
|
||||
* @private
|
||||
* @since 3.4.0
|
||||
*/
|
||||
this.addCallback = this.addHandler;
|
||||
|
||||
/**
|
||||
* A callback that is invoked every time a child is removed from this list.
|
||||
*
|
||||
* @name Phaser.GameObjects.Container#removeCallback
|
||||
* @type {function}
|
||||
* @private
|
||||
* @since 3.4.0
|
||||
*/
|
||||
this.removeCallback = this.removeHandler;
|
||||
|
||||
this.setPosition(x, y);
|
||||
|
||||
if (Array.isArray(children))
|
||||
{
|
||||
this.addMultiple(children);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Adds the given Game Object, or array of Game Objects, to this Container.
|
||||
* Internal add handler.
|
||||
*
|
||||
* @method Phaser.GameObjects.Container#add
|
||||
* @method Phaser.GameObjects.Container#addHandler
|
||||
* @private
|
||||
* @since 3.4.0
|
||||
*
|
||||
* @param {(Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[])} gameObject - The Game Object, or array of Game Objects, to add to this Container.
|
||||
*
|
||||
* @return {Phaser.GameObjects.Container} This Container instance.
|
||||
* @param {Phaser.GameObjects.Container} list - The List the Game Object was added to (also this Container)
|
||||
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object that was just added to this Container.
|
||||
*/
|
||||
add: function (gameObject)
|
||||
addHandler: function (list, gameObject)
|
||||
{
|
||||
if (!Array.isArray(gameObject))
|
||||
gameObject.on('destroy', this.remove, this);
|
||||
|
||||
if (gameObject.type === 'Container')
|
||||
{
|
||||
gameObject = [ gameObject ];
|
||||
gameObject.parentContainer = list;
|
||||
}
|
||||
|
||||
for (var i = 0; i < gameObject.length; i++)
|
||||
{
|
||||
var entry = gameObject[i];
|
||||
|
||||
if (entry.type === 'Container')
|
||||
{
|
||||
entry.parentContainer = this;
|
||||
}
|
||||
|
||||
if (this.children.indexOf(entry) === -1)
|
||||
{
|
||||
this.children.push(entry);
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
/**
|
||||
* Removes a Game Object from this Container.
|
||||
* Internal remove handler.
|
||||
*
|
||||
* @method Phaser.GameObjects.Container#remove
|
||||
* @method Phaser.GameObjects.Container#removeHandler
|
||||
* @private
|
||||
* @since 3.4.0
|
||||
*
|
||||
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object to remove from this Container.
|
||||
*
|
||||
* @return {Phaser.GameObjects.Container} This Container instance.
|
||||
* @param {Phaser.GameObjects.Container} list - The List the Game Object was removed from (also this Container)
|
||||
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object that was just removed from this Container.
|
||||
*/
|
||||
remove: function (gameObject)
|
||||
removeHandler: function (list, gameObject)
|
||||
{
|
||||
var index = this.children.indexOf(gameObject);
|
||||
gameObject.off('destroy', list.remove, this);
|
||||
|
||||
if (index !== -1)
|
||||
if (gameObject.type === 'Container')
|
||||
{
|
||||
if (gameObject.type === 'Container')
|
||||
{
|
||||
gameObject.parentContainer = null;
|
||||
}
|
||||
|
||||
this.children.splice(index, 1);
|
||||
gameObject.parentContainer = null;
|
||||
}
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -176,6 +203,90 @@ var Container = new Class({
|
|||
tempMatrix.transformPoint(source.x, source.y, output);
|
||||
|
||||
return output;
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @method Phaser.GameObjects.Container#localToWorld
|
||||
* @since 3.4.0
|
||||
*
|
||||
* @param {Phaser.GameObjects.GameObject} child - The child of this Container.
|
||||
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The camera to transform against.
|
||||
* @param {(object|Phaser.Geom.Point|Phaser.Math.Vector2)} [output] - A destination object to store the transformed point in. If none given a Vector2 will be created and returned.
|
||||
*
|
||||
* @return {(object|Phaser.Geom.Point|Phaser.Math.Vector2)} The transformed point.
|
||||
*/
|
||||
localToWorld: function (child, camera, output)
|
||||
{
|
||||
if (camera === undefined) { camera = this.scene.sys.cameras.main; }
|
||||
if (output === undefined) { output = new Vector2(); }
|
||||
|
||||
if (this.exists(child))
|
||||
{
|
||||
// Do matrix magic here
|
||||
// See: Camera.getWorldPoint and InputManager.hitTest
|
||||
}
|
||||
|
||||
return output;
|
||||
},
|
||||
|
||||
/**
|
||||
* Destroys this Container removing it and all children from the Display List and
|
||||
* severing all ties to parent resources.
|
||||
*
|
||||
* Use this to remove a Container from your game if you don't ever plan to use it again.
|
||||
* As long as no reference to it exists within your own code it should become free for
|
||||
* garbage collection by the browser.
|
||||
*
|
||||
* If you just want to temporarily disable an object then look at using the
|
||||
* Game Object Pool instead of destroying it, as destroyed objects cannot be resurrected.
|
||||
*
|
||||
* @method Phaser.GameObjects.Container#destroy
|
||||
* @since 3.4.0
|
||||
*/
|
||||
destroy: function ()
|
||||
{
|
||||
// This Game Object had already been destroyed
|
||||
if (!this.scene)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.preDestroy)
|
||||
{
|
||||
this.preDestroy.call(this);
|
||||
}
|
||||
|
||||
this.emit('destroy', this);
|
||||
|
||||
var sys = this.scene.sys;
|
||||
|
||||
sys.displayList.remove(this);
|
||||
|
||||
if (this.data)
|
||||
{
|
||||
this.data.destroy();
|
||||
|
||||
this.data = undefined;
|
||||
}
|
||||
|
||||
// Tell the Scene to re-sort the children
|
||||
sys.queueDepthSort();
|
||||
|
||||
this.active = false;
|
||||
this.visible = false;
|
||||
|
||||
this.scene = undefined;
|
||||
|
||||
this.removeAllListeners();
|
||||
|
||||
this.removeAll();
|
||||
|
||||
this.list = [];
|
||||
|
||||
this.parent = null;
|
||||
this.parentContainer = null;
|
||||
}
|
||||
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue