phaser/src/gameobjects/components/Transform.js

475 lines
12 KiB
JavaScript
Raw Normal View History

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}
*/
var MATH_CONST = require('../../math/const');
2018-04-11 12:17:26 +00:00
var TransformMatrix = require('./TransformMatrix');
var WrapAngle = require('../../math/angle/Wrap');
var WrapAngleDegrees = require('../../math/angle/WrapDegrees');
// global bitmask flag for GameObject.renderMask (used by Scale)
var _FLAG = 4; // 0100
/**
* 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
*/
var Transform = {
2018-03-29 12:48:14 +00:00
/**
* Private internal value. Holds the horizontal scale value.
*
* @name Phaser.GameObjects.Components.Transform#_scaleX
* @type {number}
2018-03-29 12:48:14 +00:00
* @private
* @default 1
* @since 3.0.0
*/
_scaleX: 1,
2018-03-29 12:48:14 +00:00
/**
* Private internal value. Holds the vertical scale value.
*
* @name Phaser.GameObjects.Components.Transform#_scaleY
* @type {number}
2018-03-29 12:48:14 +00:00
* @private
* @default 1
* @since 3.0.0
*/
_scaleY: 1,
2018-03-29 12:48:14 +00:00
/**
* Private internal value. Holds the rotation value in radians.
*
* @name Phaser.GameObjects.Components.Transform#_rotation
* @type {number}
2018-03-29 12:48:14 +00:00
* @private
* @default 0
* @since 3.0.0
*/
_rotation: 0,
/**
* 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
*/
scaleX: {
get: function ()
{
return this._scaleX;
},
set: function (value)
{
this._scaleX = value;
if (this._scaleX === 0)
{
this.renderFlags &= ~_FLAG;
}
else
{
this.renderFlags |= _FLAG;
}
}
},
/**
* The vertical scale of this Game Object.
*
* @name Phaser.GameObjects.Components.Transform#scaleY
* @type {number}
* @default 1
* @since 3.0.0
*/
scaleY: {
get: function ()
{
return this._scaleY;
},
set: function (value)
{
this._scaleY = value;
if (this._scaleY === 0)
{
this.renderFlags &= ~_FLAG;
}
else
{
this.renderFlags |= _FLAG;
}
}
},
/**
* 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
*/
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;
}
},
/**
* 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
*/
rotation: {
get: function ()
{
return this._rotation;
},
set: function (value)
{
// value is in radians
this._rotation = WrapAngle(value);
}
},
/**
* Sets the position of this Game Object.
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.Transform#setPosition
* @since 3.0.0
*
* @param {number} [x=0] - The x position of this Game Object.
2018-04-05 12:51:37 +00:00
* @param {number} [y=x] - 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.
*
2018-05-22 04:46:26 +00:00
* @return {this} This Game Object instance.
*/
2017-09-15 03:04:51 +00:00
setPosition: function (x, y, z, w)
{
2017-03-09 00:41:21 +00:00
if (x === undefined) { x = 0; }
if (y === undefined) { y = x; }
2017-09-15 03:04:51 +00:00
if (z === undefined) { z = 0; }
if (w === undefined) { w = 0; }
this.x = x;
this.y = y;
this.z = z;
this.w = w;
return this;
},
/**
* Sets the position of this Game Object to be a random position within the confines of
* the given area.
*
* If no area is specified a random position between 0 x 0 and the game width x height is used instead.
*
* The position does not factor in the size of this Game Object, meaning that only the origin is
* guaranteed to be within the area.
*
* @method Phaser.GameObjects.Components.Transform#setRandomPosition
* @since 3.8.0
*
* @param {number} [x=0] - The x position of the top-left of the random area.
* @param {number} [y=0] - The y position of the top-left of the random area.
* @param {number} [width] - The width of the random area.
* @param {number} [height] - The height of the random area.
*
2018-05-22 04:46:26 +00:00
* @return {this} This Game Object instance.
*/
setRandomPosition: function (x, y, width, height)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = this.scene.sys.game.config.width; }
if (height === undefined) { height = this.scene.sys.game.config.height; }
this.x = x + (Math.random() * width);
this.y = y + (Math.random() * height);
return this;
},
/**
* Sets the rotation of this Game Object.
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.Transform#setRotation
* @since 3.0.0
*
* @param {number} [radians=0] - The rotation of this Game Object, in radians.
*
2018-05-22 04:46:26 +00:00
* @return {this} This Game Object instance.
*/
setRotation: function (radians)
{
2017-03-09 00:41:21 +00:00
if (radians === undefined) { radians = 0; }
this.rotation = radians;
return this;
},
/**
* Sets the angle of this Game Object.
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.Transform#setAngle
* @since 3.0.0
*
* @param {number} [degrees=0] - The rotation of this Game Object, in degrees.
*
2018-05-22 04:46:26 +00:00
* @return {this} 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;
},
/**
* Sets the scale of this Game Object.
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.Transform#setScale
* @since 3.0.0
*
* @param {number} x - The horizontal scale of this Game Object.
* @param {number} [y=x] - The vertical scale of this Game Object. If not set it will use the `x` value.
*
2018-05-22 04:46:26 +00:00
* @return {this} This Game Object instance.
*/
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-04-04 22:57:37 +00:00
return this;
},
/**
* Sets the x position of this Game Object.
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.Transform#setX
* @since 3.0.0
*
* @param {number} [value=0] - The x position of this Game Object.
*
2018-05-22 04:46:26 +00:00
* @return {this} This Game Object instance.
*/
setX: function (value)
2017-09-15 03:04:51 +00:00
{
if (value === undefined) { value = 0; }
this.x = value;
2017-09-15 03:04:51 +00:00
return this;
},
/**
* Sets the y position of this Game Object.
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.Transform#setY
* @since 3.0.0
*
* @param {number} [value=0] - The y position of this Game Object.
*
2018-05-22 04:46:26 +00:00
* @return {this} This Game Object instance.
*/
setY: function (value)
2017-09-15 03:04:51 +00:00
{
if (value === undefined) { value = 0; }
this.y = value;
2017-09-15 03:04:51 +00:00
return this;
},
/**
* Sets the z position of this Game Object.
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.Transform#setZ
* @since 3.0.0
*
* @param {number} [value=0] - The z position of this Game Object.
*
2018-05-22 04:46:26 +00:00
* @return {this} This Game Object instance.
*/
setZ: function (value)
2017-04-04 22:57:37 +00:00
{
if (value === undefined) { value = 0; }
this.z = value;
2017-04-04 22:57:37 +00:00
return this;
},
/**
* Sets the w position of this Game Object.
*
2018-02-01 01:36:52 +00:00
* @method Phaser.GameObjects.Components.Transform#setW
* @since 3.0.0
*
* @param {number} [value=0] - The w position of this Game Object.
*
2018-05-22 04:46:26 +00:00
* @return {this} This Game Object instance.
*/
setW: function (value)
{
if (value === undefined) { value = 0; }
this.w = value;
return this;
2018-04-05 15:50:37 +00:00
},
2018-04-06 10:15:15 +00:00
/**
* Gets the local transform matrix for this Game Object.
*
* @method Phaser.GameObjects.Components.Transform#getLocalTransformMatrix
* @since 3.4.0
*
2018-04-11 12:17:26 +00:00
* @param {Phaser.GameObjects.Components.TransformMatrix} [tempMatrix] - The matrix to populate with the values from this Game Object.
2018-04-06 10:15:15 +00:00
*
* @return {Phaser.GameObjects.Components.TransformMatrix} The populated Transform Matrix.
*/
2018-04-05 15:50:37 +00:00
getLocalTransformMatrix: function (tempMatrix)
{
2018-04-11 12:17:26 +00:00
if (tempMatrix === undefined) { tempMatrix = new TransformMatrix(); }
2018-04-05 15:50:37 +00:00
return tempMatrix.applyITRS(this.x, this.y, this._rotation, this._scaleX, this._scaleY);
},
2018-04-06 10:15:15 +00:00
/**
* Gets the world transform matrix for this Game Object, factoring in any parent Containers.
*
* @method Phaser.GameObjects.Components.Transform#getWorldTransformMatrix
* @since 3.4.0
*
2018-04-11 12:17:26 +00:00
* @param {Phaser.GameObjects.Components.TransformMatrix} [tempMatrix] - The matrix to populate with the values from this Game Object.
2018-04-06 10:15:15 +00:00
*
* @return {Phaser.GameObjects.Components.TransformMatrix} The populated Transform Matrix.
*/
2018-04-05 17:11:48 +00:00
getWorldTransformMatrix: function (tempMatrix)
2018-04-05 15:50:37 +00:00
{
2018-04-11 12:17:26 +00:00
if (tempMatrix === undefined) { tempMatrix = new TransformMatrix(); }
2018-04-05 15:50:37 +00:00
var parent = this.parentContainer;
2018-04-11 12:17:26 +00:00
if (!parent)
{
2018-04-12 13:25:46 +00:00
return this.getLocalTransformMatrix(tempMatrix);
2018-04-11 12:17:26 +00:00
}
2018-04-05 15:50:37 +00:00
var parents = [];
2018-04-05 17:11:48 +00:00
while (parent)
2018-04-05 15:50:37 +00:00
{
2018-04-05 17:11:48 +00:00
parents.unshift(parent);
2018-04-05 15:50:37 +00:00
parent = parent.parentContainer;
}
2018-04-05 17:11:48 +00:00
tempMatrix.loadIdentity();
2018-04-05 15:50:37 +00:00
2018-04-05 17:11:48 +00:00
var length = parents.length;
for (var i = 0; i < length; ++i)
{
2018-04-06 10:15:15 +00:00
parent = parents[i];
tempMatrix.translate(parent.x, parent.y);
2018-04-12 15:28:05 +00:00
tempMatrix.rotate(parent.rotation);
tempMatrix.scale(parent.scaleX, parent.scaleY);
2018-04-05 15:50:37 +00:00
}
2018-04-05 17:11:48 +00:00
tempMatrix.translate(this.x, this.y);
tempMatrix.rotate(this._rotation);
tempMatrix.scale(this._scaleX, this._scaleY);
2018-04-05 15:50:37 +00:00
return tempMatrix;
}
};
module.exports = Transform;