2018-04-05 08:23:29 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @author Felipe Alfonso <@bitnenfer>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2018-03-23 17:15:52 +00:00
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var Components = require('../components');
|
|
|
|
var GameObject = require('../GameObject');
|
|
|
|
var Render = require('./ContainerRender');
|
2018-04-05 08:23:29 +00:00
|
|
|
var Vector2 = require('../../math/Vector2');
|
2018-03-23 17:15:52 +00:00
|
|
|
|
2018-04-05 08:23:29 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* A Container Game Object.
|
|
|
|
*
|
|
|
|
* @class Container
|
|
|
|
* @extends Phaser.GameObjects.GameObject
|
|
|
|
* @memberOf Phaser.GameObjects
|
|
|
|
* @constructor
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @extends Phaser.GameObjects.Components.BlendMode
|
|
|
|
* @extends Phaser.GameObjects.Components.Transform
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
2018-03-23 17:15:52 +00:00
|
|
|
var Container = new Class({
|
|
|
|
|
|
|
|
Extends: GameObject,
|
|
|
|
|
|
|
|
Mixins: [
|
|
|
|
Components.BlendMode,
|
|
|
|
Components.Transform,
|
|
|
|
Render
|
|
|
|
],
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2018-04-05 08:02:36 +00:00
|
|
|
function Container (scene, x, y)
|
2018-03-23 17:15:52 +00:00
|
|
|
{
|
|
|
|
GameObject.call(this, scene, 'Container');
|
2018-04-05 08:23:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Container#parentContainer
|
|
|
|
* @type {Phaser.GameObjects.Container}
|
|
|
|
* @since 3.4.0
|
|
|
|
*/
|
2018-03-23 17:15:52 +00:00
|
|
|
this.parentContainer = null;
|
2018-04-05 08:23:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Container#children
|
|
|
|
* @type {Phaser.GameObjects.GameObject[]}
|
|
|
|
* @since 3.4.0
|
|
|
|
*/
|
2018-03-23 17:15:52 +00:00
|
|
|
this.children = [];
|
2018-04-05 08:23:29 +00:00
|
|
|
|
2018-03-23 17:15:52 +00:00
|
|
|
this.setPosition(x, y);
|
2018-04-05 08:23:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Container#localTransform
|
|
|
|
* @type {Phaser.GameObjects.Components.TransformMatrix}
|
|
|
|
* @since 3.4.0
|
|
|
|
*/
|
2018-03-23 17:15:52 +00:00
|
|
|
this.localTransform = new Components.TransformMatrix();
|
2018-04-05 08:23:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Container#tempTransformMatrix
|
|
|
|
* @type {Phaser.GameObjects.Components.TransformMatrix}
|
|
|
|
* @since 3.4.0
|
|
|
|
*/
|
2018-03-28 23:47:57 +00:00
|
|
|
this.tempTransformMatrix = new Components.TransformMatrix();
|
2018-03-23 17:15:52 +00:00
|
|
|
},
|
|
|
|
|
2018-04-05 08:23:29 +00:00
|
|
|
/**
|
|
|
|
* Adds the given Game Object, or array of Game Objects, to this Container.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Container#add
|
|
|
|
* @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.
|
|
|
|
*/
|
2018-03-23 17:15:52 +00:00
|
|
|
add: function (gameObject)
|
|
|
|
{
|
2018-04-05 08:23:29 +00:00
|
|
|
if (!Array.isArray(gameObject))
|
2018-03-29 15:34:23 +00:00
|
|
|
{
|
2018-04-05 08:23:29 +00:00
|
|
|
gameObject = [ gameObject ];
|
2018-03-29 15:34:23 +00:00
|
|
|
}
|
2018-04-05 08:23:29 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < gameObject.length; i++)
|
2018-03-23 17:15:52 +00:00
|
|
|
{
|
2018-04-05 08:23:29 +00:00
|
|
|
var entry = gameObject[i];
|
|
|
|
|
|
|
|
if (entry.type === 'Container')
|
|
|
|
{
|
|
|
|
entry.parentContainer = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.children.indexOf(entry) === -1)
|
|
|
|
{
|
|
|
|
this.children.push(entry);
|
|
|
|
}
|
2018-03-23 17:15:52 +00:00
|
|
|
}
|
2018-04-05 08:23:29 +00:00
|
|
|
|
2018-03-26 20:23:18 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-04-05 08:23:29 +00:00
|
|
|
/**
|
|
|
|
* Removes a Game Object from this Container.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Container#remove
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object to remove from this Container.
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.Container} This Container instance.
|
|
|
|
*/
|
2018-03-26 20:23:18 +00:00
|
|
|
remove: function (gameObject)
|
|
|
|
{
|
|
|
|
var index = this.children.indexOf(gameObject);
|
2018-04-05 08:23:29 +00:00
|
|
|
|
|
|
|
if (index !== -1)
|
2018-03-26 20:23:18 +00:00
|
|
|
{
|
2018-03-29 15:34:23 +00:00
|
|
|
if (gameObject.type === 'Container')
|
|
|
|
{
|
|
|
|
gameObject.parentContainer = null;
|
|
|
|
}
|
2018-04-05 08:23:29 +00:00
|
|
|
|
2018-03-26 20:23:18 +00:00
|
|
|
this.children.splice(index, 1);
|
|
|
|
}
|
2018-04-05 08:23:29 +00:00
|
|
|
|
2018-03-26 20:23:18 +00:00
|
|
|
return this;
|
2018-03-28 23:47:57 +00:00
|
|
|
},
|
|
|
|
|
2018-04-05 08:23:29 +00:00
|
|
|
/**
|
|
|
|
* Takes a Point-like object, such as a Vector2, Geom.Point or object with public x and y properties,
|
|
|
|
* and transforms it into the space of this Container, then returns it in the output object.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Container#pointToContainer
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param {(object|Phaser.Geom.Point|Phaser.Math.Vector2)} source - The Source Point to be transformed.
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
pointToContainer: function (source, output)
|
2018-03-28 23:47:57 +00:00
|
|
|
{
|
2018-04-05 08:23:29 +00:00
|
|
|
if (output === undefined) { output = new Vector2(); }
|
2018-03-28 23:47:57 +00:00
|
|
|
|
2018-04-05 08:23:29 +00:00
|
|
|
if (this.parentContainer)
|
2018-03-28 23:47:57 +00:00
|
|
|
{
|
2018-04-05 08:23:29 +00:00
|
|
|
this.parentContainer.pointToContainer(source, output);
|
2018-03-28 23:47:57 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 08:23:29 +00:00
|
|
|
var tempMatrix = this.tempTransformMatrix;
|
|
|
|
|
2018-03-28 23:47:57 +00:00
|
|
|
tempMatrix.loadIdentity();
|
|
|
|
tempMatrix.applyITRS(this.x, this.y, this.rotation, this.scaleX, this.scaleY);
|
|
|
|
tempMatrix.invert();
|
2018-04-05 08:23:29 +00:00
|
|
|
tempMatrix.transformPoint(source.x, source.y, output);
|
2018-03-28 23:47:57 +00:00
|
|
|
|
2018-04-05 08:23:29 +00:00
|
|
|
return output;
|
2018-03-23 17:15:52 +00:00
|
|
|
}
|
2018-04-05 08:23:29 +00:00
|
|
|
|
2018-03-23 17:15:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Container;
|