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}
|
|
|
|
*/
|
|
|
|
|
2017-07-04 00:59:31 +00:00
|
|
|
var MATH_CONST = require('../../math/const');
|
|
|
|
var WrapAngle = require('../../math/angle/Wrap');
|
|
|
|
var WrapAngleDegrees = require('../../math/angle/WrapDegrees');
|
2017-02-23 03:10:48 +00:00
|
|
|
|
2017-02-24 01:45:15 +00:00
|
|
|
// global bitmask flag for GameObject.renderMask (used by Scale)
|
2017-02-23 03:10:48 +00:00
|
|
|
var _FLAG = 4; // 0100
|
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
/**
|
|
|
|
* Provides methods used for getting and setting the position, scale and rotation of a Game Object.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Components.Transform
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-02-23 03:10:48 +00:00
|
|
|
|
|
|
|
var Transform = {
|
|
|
|
|
2017-02-24 01:45:15 +00:00
|
|
|
// "private" properties
|
|
|
|
_scaleX: 1,
|
|
|
|
_scaleY: 1,
|
|
|
|
_rotation: 0,
|
2017-02-23 03:10:48 +00:00
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
/**
|
|
|
|
* The x position of this Game Object.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Components.Transform#x
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
x: 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The y position of this Game Object.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Components.Transform#y
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
y: 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The z position of this Game Object.
|
|
|
|
* Note: Do not use this value to set the z-index, instead see the `depth` property.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Components.Transform#z
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
z: 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The w position of this Game Object.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Components.Transform#w
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
w: 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The horizontal scale of this Game Object.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Components.Transform#scaleX
|
|
|
|
* @type {number}
|
|
|
|
* @default 1
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-02-23 03:10:48 +00:00
|
|
|
scaleX: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
2017-02-24 01:45:15 +00:00
|
|
|
return this._scaleX;
|
2017-02-23 03:10:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
2017-02-24 01:45:15 +00:00
|
|
|
this._scaleX = value;
|
2017-02-23 03:10:48 +00:00
|
|
|
|
2017-02-24 01:45:15 +00:00
|
|
|
if (this._scaleX === 0)
|
2017-02-23 03:10:48 +00:00
|
|
|
{
|
|
|
|
this.renderFlags &= ~_FLAG;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.renderFlags |= _FLAG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
/**
|
|
|
|
* The vertical scale of this Game Object.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Components.Transform#scaleY
|
|
|
|
* @type {number}
|
|
|
|
* @default 1
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-02-23 03:10:48 +00:00
|
|
|
scaleY: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
2017-02-24 01:45:15 +00:00
|
|
|
return this._scaleY;
|
2017-02-23 03:10:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
2017-02-24 01:45:15 +00:00
|
|
|
this._scaleY = value;
|
2017-02-23 03:10:48 +00:00
|
|
|
|
2017-02-24 01:45:15 +00:00
|
|
|
if (this._scaleY === 0)
|
2017-02-23 03:10:48 +00:00
|
|
|
{
|
|
|
|
this.renderFlags &= ~_FLAG;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.renderFlags |= _FLAG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
/**
|
|
|
|
* The angle of this Game Object as expressed in degrees.
|
|
|
|
*
|
|
|
|
* Where 0 is to the right, 90 is down, 180 is left.
|
|
|
|
*
|
|
|
|
* If you prefer to work in radians, see the `rotation` property instead.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Components.Transform#angle
|
|
|
|
* @type {integer}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-02-24 01:45:15 +00:00
|
|
|
angle: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return WrapAngleDegrees(this._rotation * MATH_CONST.RAD_TO_DEG);
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
// value is in degrees
|
|
|
|
this.rotation = WrapAngleDegrees(value) * MATH_CONST.DEG_TO_RAD;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
/**
|
|
|
|
* The angle of this Game Object in radians.
|
|
|
|
*
|
|
|
|
* If you prefer to work in degrees, see the `angle` property instead.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Components.Transform#rotation
|
|
|
|
* @type {number}
|
|
|
|
* @default 1
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-02-24 01:45:15 +00:00
|
|
|
rotation: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._rotation;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
// value is in radians
|
|
|
|
this._rotation = WrapAngle(value);
|
2017-12-15 04:08:25 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
/**
|
|
|
|
* Sets the position of this Game Object.
|
|
|
|
*
|
2018-02-01 01:36:52 +00:00
|
|
|
* @method Phaser.GameObjects.Components.Transform#setPosition
|
2018-02-01 00:50:15 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} [x=0] - The x position of this Game Object.
|
|
|
|
* @param {number} [y] - The y position of this Game Object. If not set it will use the `x` value.
|
|
|
|
* @param {number} [z=0] - The z position of this Game Object.
|
|
|
|
* @param {number} [w=0] - The w position of this Game Object.
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
|
|
|
|
*/
|
2017-09-15 03:04:51 +00:00
|
|
|
setPosition: function (x, y, z, w)
|
2017-02-23 03:10:48 +00:00
|
|
|
{
|
2017-03-09 00:41:21 +00:00
|
|
|
if (x === undefined) { x = 0; }
|
2017-02-23 03:10:48 +00:00
|
|
|
if (y === undefined) { y = x; }
|
2017-09-15 03:04:51 +00:00
|
|
|
if (z === undefined) { z = 0; }
|
|
|
|
if (w === undefined) { w = 0; }
|
2017-02-23 03:10:48 +00:00
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.z = z;
|
|
|
|
this.w = w;
|
2017-02-23 03:10:48 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
/**
|
|
|
|
* Sets the rotation of this Game Object.
|
|
|
|
*
|
2018-02-01 01:36:52 +00:00
|
|
|
* @method Phaser.GameObjects.Components.Transform#setRotation
|
2018-02-01 00:50:15 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} [radians=0] - The rotation of this Game Object, in radians.
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
|
|
|
|
*/
|
2017-02-24 01:45:15 +00:00
|
|
|
setRotation: function (radians)
|
|
|
|
{
|
2017-03-09 00:41:21 +00:00
|
|
|
if (radians === undefined) { radians = 0; }
|
|
|
|
|
|
|
|
this.rotation = radians;
|
2017-02-24 01:45:15 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
/**
|
|
|
|
* Sets the angle of this Game Object.
|
|
|
|
*
|
2018-02-01 01:36:52 +00:00
|
|
|
* @method Phaser.GameObjects.Components.Transform#setAngle
|
2018-02-01 00:50:15 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} [degrees=0] - The rotation of this Game Object, in degrees.
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
|
|
|
|
*/
|
2017-07-13 01:05:44 +00:00
|
|
|
setAngle: function (degrees)
|
|
|
|
{
|
|
|
|
if (degrees === undefined) { degrees = 0; }
|
|
|
|
|
|
|
|
this.angle = degrees;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
/**
|
|
|
|
* Sets the scale of this Game Object.
|
|
|
|
*
|
2018-02-01 01:36:52 +00:00
|
|
|
* @method Phaser.GameObjects.Components.Transform#setScale
|
2018-02-01 00:50:15 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} x - The horizontal scale of this Game Object.
|
|
|
|
* @param {number} [y] - The vertical scale of this Game Object. If not set it will use the `x` value.
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
|
|
|
|
*/
|
2017-02-23 03:10:48 +00:00
|
|
|
setScale: function (x, y)
|
|
|
|
{
|
|
|
|
if (x === undefined) { x = 1; }
|
|
|
|
if (y === undefined) { y = x; }
|
|
|
|
|
2017-03-28 23:43:55 +00:00
|
|
|
this.scaleX = x;
|
|
|
|
this.scaleY = y;
|
2017-02-23 03:10:48 +00:00
|
|
|
|
2017-04-04 22:57:37 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
/**
|
|
|
|
* Sets the x position of this Game Object.
|
|
|
|
*
|
2018-02-01 01:36:52 +00:00
|
|
|
* @method Phaser.GameObjects.Components.Transform#setX
|
2018-02-01 00:50:15 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} [value=0] - The x position of this Game Object.
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
|
|
|
|
*/
|
|
|
|
setX: function (value)
|
2017-09-15 03:04:51 +00:00
|
|
|
{
|
|
|
|
if (value === undefined) { value = 0; }
|
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
this.x = value;
|
2017-09-15 03:04:51 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
/**
|
|
|
|
* Sets the y position of this Game Object.
|
|
|
|
*
|
2018-02-01 01:36:52 +00:00
|
|
|
* @method Phaser.GameObjects.Components.Transform#setY
|
2018-02-01 00:50:15 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} [value=0] - The y position of this Game Object.
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
|
|
|
|
*/
|
|
|
|
setY: function (value)
|
2017-09-15 03:04:51 +00:00
|
|
|
{
|
|
|
|
if (value === undefined) { value = 0; }
|
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
this.y = value;
|
2017-09-15 03:04:51 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
/**
|
|
|
|
* Sets the z position of this Game Object.
|
|
|
|
*
|
2018-02-01 01:36:52 +00:00
|
|
|
* @method Phaser.GameObjects.Components.Transform#setZ
|
2018-02-01 00:50:15 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} [value=0] - The z position of this Game Object.
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
|
|
|
|
*/
|
|
|
|
setZ: function (value)
|
2017-04-04 22:57:37 +00:00
|
|
|
{
|
|
|
|
if (value === undefined) { value = 0; }
|
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
this.z = value;
|
2017-04-04 22:57:37 +00:00
|
|
|
|
2017-02-23 03:10:48 +00:00
|
|
|
return this;
|
2017-12-15 04:08:25 +00:00
|
|
|
},
|
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
/**
|
|
|
|
* Sets the w position of this Game Object.
|
|
|
|
*
|
2018-02-01 01:36:52 +00:00
|
|
|
* @method Phaser.GameObjects.Components.Transform#setW
|
2018-02-01 00:50:15 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} [value=0] - The w position of this Game Object.
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
|
|
|
|
*/
|
|
|
|
setW: function (value)
|
2017-12-15 04:08:25 +00:00
|
|
|
{
|
2018-02-01 00:50:15 +00:00
|
|
|
if (value === undefined) { value = 0; }
|
2017-12-15 04:08:25 +00:00
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
this.w = value;
|
2017-12-15 04:08:25 +00:00
|
|
|
|
2018-02-01 00:50:15 +00:00
|
|
|
return this;
|
2017-02-23 03:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Transform;
|