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
|
|
|
|
|
|
|
|
// Transform Component
|
|
|
|
|
|
|
|
var Transform = {
|
|
|
|
|
2017-02-24 01:45:15 +00:00
|
|
|
// "private" properties
|
|
|
|
_scaleX: 1,
|
|
|
|
_scaleY: 1,
|
|
|
|
_rotation: 0,
|
2017-09-14 02:12:00 +00:00
|
|
|
_depth: 0,
|
2017-12-15 04:08:25 +00:00
|
|
|
_dirty: false,
|
|
|
|
_world: { a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0, sr: 0, cr: 0 },
|
2017-02-24 01:45:15 +00:00
|
|
|
|
2017-04-04 22:57:37 +00:00
|
|
|
// public properties / methods
|
|
|
|
|
2017-12-15 04:08:25 +00:00
|
|
|
// These are world coordinate values.
|
2017-04-04 22:57:37 +00:00
|
|
|
|
2017-12-15 04:08:25 +00:00
|
|
|
// If Game Object is a child of a Container, then you can modify its local position (relative to the Container)
|
|
|
|
// by setting `localX`, `localY`, etc (or changing x/y directly, but remember the values given here are world based).
|
|
|
|
// Changes to the parent Container are instantly reflected in the world coords here (x,y, etc)
|
|
|
|
|
|
|
|
_x: 0,
|
|
|
|
_y: 0,
|
|
|
|
_z: 0,
|
|
|
|
_w: 0,
|
|
|
|
|
|
|
|
x: {
|
2017-04-04 22:57:37 +00:00
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
2017-12-15 04:08:25 +00:00
|
|
|
return this._x;
|
2017-03-23 19:51:02 +00:00
|
|
|
},
|
2017-04-04 22:57:37 +00:00
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
2017-12-15 04:08:25 +00:00
|
|
|
this._x = value;
|
|
|
|
this._dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
y: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._y;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._y = value;
|
|
|
|
this._dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
z: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._z;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._z = value;
|
|
|
|
this._dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
w: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._w;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._w = value;
|
|
|
|
this._dirty = true;
|
2017-03-23 19:51:02 +00:00
|
|
|
}
|
2017-04-04 22:57:37 +00:00
|
|
|
|
2017-03-23 19:51:02 +00:00
|
|
|
},
|
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-12-15 04:08:25 +00:00
|
|
|
this._dirty = true;
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
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-12-15 04:08:25 +00:00
|
|
|
this._dirty = true;
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
this._world.sr = Math.sin(this._rotation);
|
|
|
|
this._world.cr = Math.cos(this._rotation);
|
|
|
|
|
|
|
|
this._dirty = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
depth: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._depth;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
2018-01-11 13:59:06 +00:00
|
|
|
this.scene.sys.queueDepthSort();
|
2017-12-15 04:08:25 +00:00
|
|
|
this._depth = value;
|
2017-02-24 01:45:15 +00:00
|
|
|
}
|
2017-12-15 04:08:25 +00:00
|
|
|
|
2017-02-24 01:45:15 +00:00
|
|
|
},
|
|
|
|
|
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
|
|
|
|
2017-12-15 04:08:25 +00:00
|
|
|
this._x = x;
|
|
|
|
this._y = y;
|
|
|
|
this._z = z;
|
|
|
|
this._w = w;
|
|
|
|
|
|
|
|
this._dirty = true;
|
2017-02-23 03:10:48 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
2017-07-13 01:05:44 +00:00
|
|
|
setAngle: function (degrees)
|
|
|
|
{
|
|
|
|
if (degrees === undefined) { degrees = 0; }
|
|
|
|
|
|
|
|
this.angle = degrees;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
2017-09-15 03:04:51 +00:00
|
|
|
setZ: function (value)
|
|
|
|
{
|
|
|
|
if (value === undefined) { value = 0; }
|
|
|
|
|
|
|
|
this.z = value;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setW: function (value)
|
|
|
|
{
|
|
|
|
if (value === undefined) { value = 0; }
|
|
|
|
|
|
|
|
this.w = value;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-09-14 02:12:00 +00:00
|
|
|
setDepth: function (value)
|
2017-04-04 22:57:37 +00:00
|
|
|
{
|
|
|
|
if (value === undefined) { value = 0; }
|
|
|
|
|
2017-09-14 02:12:00 +00:00
|
|
|
this.depth = 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
|
|
|
},
|
|
|
|
|
|
|
|
updateTransform: function ()
|
|
|
|
{
|
|
|
|
if (!this.parent || !this._dirty)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var tx = this._x;
|
|
|
|
var ty = this._y;
|
|
|
|
var world = this._world;
|
|
|
|
|
|
|
|
var parent = this.parent.world;
|
|
|
|
|
|
|
|
var a = world.cr * this._scaleX;
|
|
|
|
var b = world.sr * this._scaleX;
|
|
|
|
var c = -world.sr * this._scaleY;
|
|
|
|
var d = world.cr * this._scaleY;
|
|
|
|
|
|
|
|
world.a = (a * parent.a) + (b * parent.c);
|
|
|
|
world.b = (a * parent.b) + (b * parent.d);
|
|
|
|
world.c = (c * parent.a) + (d * parent.c);
|
|
|
|
world.d = (c * parent.b) + (d * parent.d);
|
|
|
|
|
|
|
|
// this._worldRotation = Math.atan2(-this.world.c, this.world.d);
|
|
|
|
|
|
|
|
world.tx = (tx * parent.a) + (ty * parent.c) + parent.tx;
|
|
|
|
world.ty = (tx * parent.b) + (ty * parent.d) + parent.ty;
|
|
|
|
|
|
|
|
// this._worldScaleX = this._scaleX * Math.sqrt((world.a * world.a) + (world.c * world.c));
|
|
|
|
// this._worldScaleY = this._scaleY * Math.sqrt((world.b * world.b) + (world.d * world.d));
|
2017-02-23 03:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Transform;
|