2013-06-06 01:47:08 +00:00
|
|
|
/// <reference path="../Game.ts" />
|
2013-06-07 15:27:33 +00:00
|
|
|
/// <reference path="../math/Mat3.ts" />
|
2013-06-06 01:47:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Phaser - Components - Transform
|
|
|
|
*/
|
|
|
|
|
|
|
|
module Phaser.Components {
|
|
|
|
|
|
|
|
export class Transform {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new Sprite Transform component
|
|
|
|
* @param parent The Sprite using this transform
|
|
|
|
*/
|
|
|
|
constructor(parent) {
|
|
|
|
|
|
|
|
this.game = parent.game;
|
|
|
|
this.parent = parent;
|
|
|
|
|
2013-06-07 15:27:33 +00:00
|
|
|
this.local = new Mat3;
|
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
this.scrollFactor = new Phaser.Vec2(1, 1);
|
|
|
|
this.origin = new Phaser.Vec2;
|
|
|
|
this.scale = new Phaser.Vec2(1, 1);
|
|
|
|
this.skew = new Phaser.Vec2;
|
|
|
|
|
2013-06-11 22:30:35 +00:00
|
|
|
this.center = new Phaser.Point;
|
|
|
|
this.upperLeft = new Phaser.Point;
|
|
|
|
this.upperRight = new Phaser.Point;
|
|
|
|
this.bottomLeft = new Phaser.Point;
|
|
|
|
this.bottomRight = new Phaser.Point;
|
|
|
|
|
|
|
|
this._pos = new Phaser.Point;
|
|
|
|
this._scale = new Phaser.Point;
|
|
|
|
this._size = new Phaser.Point;
|
|
|
|
this._halfSize = new Phaser.Point;
|
|
|
|
this._offset = new Phaser.Point;
|
|
|
|
this._origin = new Phaser.Point;
|
|
|
|
this._sc = new Phaser.Point;
|
|
|
|
this._scA = new Phaser.Point;
|
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 20:30:15 +00:00
|
|
|
private _rotation: number;
|
|
|
|
|
2013-06-11 22:30:35 +00:00
|
|
|
// Cache vars
|
|
|
|
private _pos: Phaser.Point;
|
|
|
|
private _scale: Phaser.Point;
|
|
|
|
private _size: Phaser.Point;
|
|
|
|
private _halfSize: Phaser.Point;
|
|
|
|
private _offset: Phaser.Point;
|
|
|
|
private _origin: Phaser.Point;
|
|
|
|
private _sc: Phaser.Point;
|
|
|
|
private _scA: Phaser.Point;
|
|
|
|
private _angle: number;
|
|
|
|
private _distance: number;
|
|
|
|
private _prevRotation: number;
|
|
|
|
|
|
|
|
public center: Phaser.Point;
|
|
|
|
public upperLeft: Phaser.Point;
|
|
|
|
public upperRight: Phaser.Point;
|
|
|
|
public bottomLeft: Phaser.Point;
|
|
|
|
public bottomRight: Phaser.Point;
|
2013-06-07 15:27:33 +00:00
|
|
|
|
2013-06-07 18:18:39 +00:00
|
|
|
public local: Mat3;
|
2013-06-07 15:27:33 +00:00
|
|
|
|
2013-06-11 20:30:15 +00:00
|
|
|
public setCache() {
|
|
|
|
|
2013-06-11 22:30:35 +00:00
|
|
|
this._pos.x = this.parent.x;
|
|
|
|
this._pos.y = this.parent.y;
|
|
|
|
this._halfSize.x = this.parent.width / 2;
|
|
|
|
this._halfSize.y = this.parent.height / 2;
|
|
|
|
this._offset.x = this.origin.x * this.parent.width;
|
|
|
|
this._offset.y = this.origin.y * this.parent.height;
|
|
|
|
this._angle = Math.atan2(this.halfHeight - this._offset.y, this.halfWidth - this._offset.x);
|
|
|
|
this._distance = Math.sqrt(((this._offset.x - this._halfSize.x) * (this._offset.x - this._halfSize.x)) + ((this._offset.y - this._halfSize.y) * (this._offset.y - this._halfSize.y)));
|
|
|
|
this._size.x = this.parent.width;
|
|
|
|
this._size.y = this.parent.height;
|
|
|
|
this._origin.x = this.origin.x;
|
|
|
|
this._origin.y = this.origin.y;
|
|
|
|
this._sc.x = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD);
|
|
|
|
this._sc.y = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD);
|
|
|
|
this._scA.y = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD + this._angle);
|
|
|
|
this._scA.x = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD + this._angle);
|
|
|
|
this._prevRotation = this.rotation;
|
2013-06-11 20:30:15 +00:00
|
|
|
|
|
|
|
if (this.parent.texture && this.parent.texture.renderRotation)
|
|
|
|
{
|
2013-06-11 22:30:35 +00:00
|
|
|
this._sc.x = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD);
|
|
|
|
this._sc.y = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD);
|
2013-06-11 20:30:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-11 22:30:35 +00:00
|
|
|
this._sc.x = 0;
|
|
|
|
this._sc.y = 1;
|
2013-06-11 20:30:15 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 22:30:35 +00:00
|
|
|
this.center.setTo(this.center.x, this.center.y);
|
|
|
|
this.upperLeft.setTo(this.center.x - this._halfSize.x * this._sc.y + this._halfSize.y * this._sc.x, this.center.y - this._halfSize.y * this._sc.y - this._halfSize.x * this._sc.x);
|
|
|
|
this.upperRight.setTo(this.center.x + this._halfSize.x * this._sc.y + this._halfSize.y * this._sc.x, this.center.y - this._halfSize.y * this._sc.y + this._halfSize.x * this._sc.x);
|
|
|
|
this.bottomLeft.setTo(this.center.x - this._halfSize.x * this._sc.y - this._halfSize.y * this._sc.x, this.center.y + this._halfSize.y * this._sc.y - this._halfSize.x * this._sc.x);
|
|
|
|
this.bottomRight.setTo(this.center.x + this._halfSize.x * this._sc.y - this._halfSize.y * this._sc.x, this.center.y + this._halfSize.y * this._sc.y + this._halfSize.x * this._sc.x);
|
|
|
|
|
2013-06-11 20:30:15 +00:00
|
|
|
}
|
|
|
|
|
2013-06-07 18:18:39 +00:00
|
|
|
public update() {
|
2013-06-07 15:27:33 +00:00
|
|
|
|
2013-06-11 20:30:15 +00:00
|
|
|
// Check cache
|
|
|
|
var dirty: bool = false;
|
2013-06-07 15:27:33 +00:00
|
|
|
|
2013-06-11 20:30:15 +00:00
|
|
|
// 1) Height or Width change (also triggered by a change in scale) or an Origin change
|
2013-06-11 22:30:35 +00:00
|
|
|
if (this.parent.width !== this._size.x || this.parent.height !== this._size.y || this.origin.x !== this._origin.x|| this.origin.y !== this._origin.y)
|
2013-06-11 20:30:15 +00:00
|
|
|
{
|
2013-06-11 22:30:35 +00:00
|
|
|
this._halfSize.x = this.parent.width / 2;
|
|
|
|
this._halfSize.y = this.parent.height / 2;
|
|
|
|
this._offset.x = this.origin.x * this.parent.width;
|
|
|
|
this._offset.y = this.origin.y * this.parent.height;
|
|
|
|
this._angle = Math.atan2(this.halfHeight - this._offset.y, this.halfWidth - this._offset.x);
|
|
|
|
this._distance = Math.sqrt(((this._offset.x - this._halfSize.x) * (this._offset.x - this._halfSize.x)) + ((this._offset.y - this._halfSize.y) * (this._offset.y - this._halfSize.y)));
|
2013-06-11 20:30:15 +00:00
|
|
|
// Store
|
2013-06-11 22:30:35 +00:00
|
|
|
this._size.x = this.parent.width;
|
|
|
|
this._size.y = this.parent.height;
|
|
|
|
this._origin.x = this.origin.x;
|
|
|
|
this._origin.y = this.origin.y;
|
2013-06-11 20:30:15 +00:00
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2) Rotation change
|
2013-06-11 22:30:35 +00:00
|
|
|
if (this.rotation != this._prevRotation)
|
2013-06-11 20:30:15 +00:00
|
|
|
{
|
2013-06-11 22:30:35 +00:00
|
|
|
this._sc.x = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD);
|
|
|
|
this._sc.y = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD);
|
|
|
|
this._scA.y = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD + this._angle);
|
|
|
|
this._scA.x = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD + this._angle);
|
2013-06-11 20:30:15 +00:00
|
|
|
|
|
|
|
if (this.parent.texture.renderRotation)
|
|
|
|
{
|
2013-06-11 22:30:35 +00:00
|
|
|
this._sc.x = Math.sin((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD);
|
|
|
|
this._sc.y = Math.cos((this.rotation + this.rotationOffset) * GameMath.DEG_TO_RAD);
|
2013-06-11 20:30:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-11 22:30:35 +00:00
|
|
|
this._sc.x = 0;
|
|
|
|
this._sc.y = 1;
|
2013-06-11 20:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store
|
2013-06-11 22:30:35 +00:00
|
|
|
this._prevRotation = this.rotation;
|
2013-06-11 20:30:15 +00:00
|
|
|
dirty = true;
|
|
|
|
}
|
2013-06-07 15:27:33 +00:00
|
|
|
|
2013-06-11 22:30:35 +00:00
|
|
|
// If it has moved, updated the edges and center
|
|
|
|
if (dirty || this.parent.x != this._pos.x || this.parent.y != this._pos.y)
|
2013-06-07 15:27:33 +00:00
|
|
|
{
|
2013-06-11 22:30:35 +00:00
|
|
|
this.center.x = this.parent.x + this._distance * this._scA.y;
|
|
|
|
this.center.y = this.parent.y + this._distance * this._scA.x;
|
|
|
|
|
|
|
|
this.center.setTo(this.center.x, this.center.y);
|
|
|
|
|
|
|
|
this.upperLeft.setTo(this.center.x - this._halfSize.x * this._sc.y + this._halfSize.y * this._sc.x, this.center.y - this._halfSize.y * this._sc.y - this._halfSize.x * this._sc.x);
|
|
|
|
this.upperRight.setTo(this.center.x + this._halfSize.x * this._sc.y + this._halfSize.y * this._sc.x, this.center.y - this._halfSize.y * this._sc.y + this._halfSize.x * this._sc.x);
|
|
|
|
this.bottomLeft.setTo(this.center.x - this._halfSize.x * this._sc.y - this._halfSize.y * this._sc.x, this.center.y + this._halfSize.y * this._sc.y - this._halfSize.x * this._sc.x);
|
|
|
|
this.bottomRight.setTo(this.center.x + this._halfSize.x * this._sc.y - this._halfSize.y * this._sc.x, this.center.y + this._halfSize.y * this._sc.y + this._halfSize.x * this._sc.x);
|
|
|
|
|
|
|
|
this._pos.x = this.parent.x;
|
|
|
|
this._pos.y = this.parent.y;
|
2013-06-07 15:27:33 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 20:30:15 +00:00
|
|
|
// Scale and Skew
|
2013-06-07 15:27:33 +00:00
|
|
|
if (this.parent.texture.flippedX)
|
|
|
|
{
|
2013-06-11 22:30:35 +00:00
|
|
|
this.local.data[0] = this._sc.y * -this.scale.x;
|
|
|
|
this.local.data[3] = (this._sc.x * -this.scale.x) + this.skew.x;
|
2013-06-07 15:27:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-11 22:30:35 +00:00
|
|
|
this.local.data[0] = this._sc.y * this.scale.x;
|
|
|
|
this.local.data[3] = (this._sc.x * this.scale.x) + this.skew.x;
|
2013-06-07 15:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.parent.texture.flippedY)
|
|
|
|
{
|
2013-06-11 22:30:35 +00:00
|
|
|
this.local.data[4] = this._sc.y * -this.scale.y;
|
|
|
|
this.local.data[1] = -(this._sc.x * -this.scale.y) + this.skew.y;
|
2013-06-07 15:27:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-11 22:30:35 +00:00
|
|
|
this.local.data[4] = this._sc.y * this.scale.y;
|
|
|
|
this.local.data[1] = -(this._sc.x * this.scale.y) + this.skew.y;
|
2013-06-07 15:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Translate
|
|
|
|
this.local.data[2] = this.parent.x;
|
|
|
|
this.local.data[5] = this.parent.y;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
/**
|
|
|
|
* Reference to Phaser.Game
|
|
|
|
*/
|
|
|
|
public game: Game;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to the parent object (Sprite, Group, etc)
|
|
|
|
*/
|
|
|
|
public parent: Phaser.Sprite;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scale of the object. A scale of 1.0 is the original size. 0.5 half size. 2.0 double sized.
|
|
|
|
*/
|
|
|
|
public scale: Phaser.Vec2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Skew the object along the x and y axis. A skew value of 0 is no skew.
|
|
|
|
*/
|
|
|
|
public skew: Phaser.Vec2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The influence of camera movement upon the object, if supported.
|
|
|
|
*/
|
|
|
|
public scrollFactor: Phaser.Vec2;
|
|
|
|
|
|
|
|
/**
|
2013-06-11 20:30:15 +00:00
|
|
|
* The origin is the point around which scale and rotation takes place and defaults to the top-left of the sprite.
|
2013-06-06 01:47:08 +00:00
|
|
|
*/
|
|
|
|
public origin: Phaser.Vec2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This value is added to the rotation of the object.
|
|
|
|
* For example if you had a texture drawn facing straight up then you could set
|
|
|
|
* rotationOffset to 90 and it would correspond correctly with Phasers right-handed coordinate system.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
public rotationOffset: number = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The rotation of the object in degrees. Phaser uses a right-handed coordinate system, where 0 points to the right.
|
|
|
|
*/
|
|
|
|
public rotation: number = 0;
|
|
|
|
|
2013-06-07 15:27:33 +00:00
|
|
|
/**
|
2013-06-11 20:30:15 +00:00
|
|
|
* The distance from the center of the transform to the rotation origin.
|
|
|
|
*/
|
|
|
|
public get distance(): number {
|
2013-06-11 22:30:35 +00:00
|
|
|
return this._distance;
|
2013-06-11 20:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The angle between the center of the transform to the rotation origin.
|
|
|
|
*/
|
|
|
|
public get angleToCenter(): number {
|
2013-06-11 22:30:35 +00:00
|
|
|
return this._angle;
|
2013-06-11 20:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The offset on the X axis of the origin
|
|
|
|
*/
|
|
|
|
public get offsetX(): number {
|
2013-06-11 22:30:35 +00:00
|
|
|
return this._offset.x;
|
2013-06-11 20:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The offset on the Y axis of the origin
|
|
|
|
*/
|
|
|
|
public get offsetY(): number {
|
2013-06-11 22:30:35 +00:00
|
|
|
return this._offset.y;
|
2013-06-11 20:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Half the width of the parent sprite, taking into consideration scaling
|
|
|
|
*/
|
|
|
|
public get halfWidth(): number {
|
2013-06-11 22:30:35 +00:00
|
|
|
return this._halfSize.x;
|
2013-06-11 20:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Half the height of the parent sprite, taking into consideration scaling
|
|
|
|
*/
|
|
|
|
public get halfHeight(): number {
|
2013-06-11 22:30:35 +00:00
|
|
|
return this._halfSize.y;
|
2013-06-11 20:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The center of the Sprite in world coordinates, after taking scaling and rotation into consideration
|
2013-06-07 15:27:33 +00:00
|
|
|
*/
|
2013-06-11 22:30:35 +00:00
|
|
|
//public get centerX(): number {
|
|
|
|
// return this.center.x;
|
|
|
|
//}
|
2013-06-07 15:27:33 +00:00
|
|
|
|
|
|
|
/**
|
2013-06-11 20:30:15 +00:00
|
|
|
* The center of the Sprite in world coordinates, after taking scaling and rotation into consideration
|
2013-06-07 15:27:33 +00:00
|
|
|
*/
|
2013-06-11 22:30:35 +00:00
|
|
|
//public get centerY(): number {
|
|
|
|
// return this.center.y;
|
|
|
|
//}
|
2013-06-11 20:30:15 +00:00
|
|
|
|
|
|
|
public get sin(): number {
|
2013-06-11 22:30:35 +00:00
|
|
|
return this._sc.x;
|
2013-06-11 20:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public get cos(): number {
|
2013-06-11 22:30:35 +00:00
|
|
|
return this._sc.y;
|
2013-06-07 15:27:33 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|