Farewell TypeScript, see you on the other side.

This commit is contained in:
Richard Davey
2013-08-28 07:02:57 +01:00
parent 0e55e644a9
commit 09def364c3
989 changed files with 67634 additions and 14984 deletions
+29
View File
@@ -0,0 +1,29 @@
var Phaser;
(function (Phaser) {
/// <reference path="../_definitions.ts" />
/**
* Phaser - Components - Events
*
* Signals that are dispatched by the Sprite and its various components
*/
(function (Components) {
var Events = (function () {
/**
* The Events component is a collection of events fired by the parent game object and its components.
* @param parent The game object using this Input component
*/
function Events(parent) {
this.game = parent.game;
this._parent = parent;
this.onAddedToGroup = new Phaser.Signal();
this.onRemovedFromGroup = new Phaser.Signal();
this.onKilled = new Phaser.Signal();
this.onRevived = new Phaser.Signal();
this.onOutOfBounds = new Phaser.Signal();
}
return Events;
})();
Components.Events = Events;
})(Phaser.Components || (Phaser.Components = {}));
var Components = Phaser.Components;
})(Phaser || (Phaser = {}));
+112
View File
@@ -0,0 +1,112 @@
/// <reference path="../_definitions.ts" />
/**
* Phaser - Components - Events
*
* Signals that are dispatched by the Sprite and its various components
*/
module Phaser.Components {
export class Events {
/**
* The Events component is a collection of events fired by the parent game object and its components.
* @param parent The game object using this Input component
*/
constructor(parent: Phaser.Sprite) {
this.game = parent.game;
this._parent = parent;
this.onAddedToGroup = new Phaser.Signal;
this.onRemovedFromGroup = new Phaser.Signal;
this.onKilled = new Phaser.Signal;
this.onRevived = new Phaser.Signal;
this.onOutOfBounds = new Phaser.Signal;
}
/**
* Reference to Phaser.Game
*/
public game: Phaser.Game;
/**
* Local private reference to its parent game object.
*/
private _parent: Phaser.Sprite;
/**
* Dispatched by the Group this Sprite is added to.
*/
public onAddedToGroup: Phaser.Signal;
/**
* Dispatched by the Group this Sprite is removed from.
*/
public onRemovedFromGroup: Phaser.Signal;
/**
* Dispatched when this Sprite is killed.
*/
public onKilled: Phaser.Signal;
/**
* Dispatched when this Sprite is revived.
*/
public onRevived: Phaser.Signal;
/**
* Dispatched by the Input component when a pointer moves over an Input enabled sprite.
*/
public onInputOver: Phaser.Signal;
/**
* Dispatched by the Input component when a pointer moves out of an Input enabled sprite.
*/
public onInputOut: Phaser.Signal;
/**
* Dispatched by the Input component when a pointer is pressed down on an Input enabled sprite.
*/
public onInputDown: Phaser.Signal;
/**
* Dispatched by the Input component when a pointer is released over an Input enabled sprite
*/
public onInputUp: Phaser.Signal;
/**
* Dispatched by the Input component when the Sprite starts being dragged
*/
public onDragStart: Phaser.Signal;
/**
* Dispatched by the Input component when the Sprite stops being dragged
*/
public onDragStop: Phaser.Signal;
/**
* Dispatched by the Animation component when the Sprite starts being animated
*/
public onAnimationStart: Phaser.Signal;
/**
* Dispatched by the Animation component when the Sprite animation completes
*/
public onAnimationComplete: Phaser.Signal;
/**
* Dispatched by the Animation component when the Sprite animation loops
*/
public onAnimationLoop: Phaser.Signal;
/**
* Dispatched by the Sprite when it first leaves the world bounds
*/
public onOutOfBounds: Phaser.Signal;
}
}
+260
View File
@@ -0,0 +1,260 @@
/// <reference path="../_definitions.ts" />
/**
* Phaser - GameObjectFactory
*
* A quick way to create new world objects and add existing objects to the current world.
*/
var Phaser;
(function (Phaser) {
var GameObjectFactory = (function () {
/**
* GameObjectFactory constructor
* @param game {Game} A reference to the current Game.
*/
function GameObjectFactory(game) {
this.game = game;
this._world = this.game.world;
}
GameObjectFactory.prototype.camera = /**
* Create a new camera with specific position and size.
*
* @param x {number} X position of the new camera.
* @param y {number} Y position of the new camera.
* @param width {number} Width of the new camera.
* @param height {number} Height of the new camera.
* @returns {Camera} The newly created camera object.
*/
function (x, y, width, height) {
return this._world.cameras.addCamera(x, y, width, height);
};
GameObjectFactory.prototype.button = /**
* Create a new GeomSprite with specific position.
*
* @param x {number} X position of the new geom sprite.
* @param y {number} Y position of the new geom sprite.
* @returns {GeomSprite} The newly created geom sprite object.
*/
//public geomSprite(x: number, y: number): GeomSprite {
// return <GeomSprite> this._world.group.add(new GeomSprite(this.game, x, y));
//}
/**
* Create a new Button game object.
*
* @param [x] {number} X position of the button.
* @param [y] {number} Y position of the button.
* @param [key] {string} The image key as defined in the Game.Cache to use as the texture for this button.
* @param [callback] {function} The function to call when this button is pressed
* @param [callbackContext] {object} The context in which the callback will be called (usually 'this')
* @param [overFrame] {string|number} This is the frame or frameName that will be set when this button is in an over state. Give either a number to use a frame ID or a string for a frame name.
* @param [outFrame] {string|number} This is the frame or frameName that will be set when this button is in an out state. Give either a number to use a frame ID or a string for a frame name.
* @param [downFrame] {string|number} This is the frame or frameName that will be set when this button is in a down state. Give either a number to use a frame ID or a string for a frame name.
* @returns {Button} The newly created button object.
*/
function (x, y, key, callback, callbackContext, overFrame, outFrame, downFrame) {
if (typeof x === "undefined") { x = 0; }
if (typeof y === "undefined") { y = 0; }
if (typeof key === "undefined") { key = null; }
if (typeof callback === "undefined") { callback = null; }
if (typeof callbackContext === "undefined") { callbackContext = null; }
if (typeof overFrame === "undefined") { overFrame = null; }
if (typeof outFrame === "undefined") { outFrame = null; }
if (typeof downFrame === "undefined") { downFrame = null; }
return this._world.group.add(new Phaser.UI.Button(this.game, x, y, key, callback, callbackContext, overFrame, outFrame, downFrame));
};
GameObjectFactory.prototype.sprite = /**
* Create a new Sprite with specific position and sprite sheet key.
*
* @param x {number} X position of the new sprite.
* @param y {number} Y position of the new sprite.
* @param [key] {string} The image key as defined in the Game.Cache to use as the texture for this sprite
* @param [frame] {string|number} If the sprite uses an image from a texture atlas or sprite sheet you can pass the frame here. Either a number for a frame ID or a string for a frame name.
* @returns {Sprite} The newly created sprite object.
*/
function (x, y, key, frame) {
if (typeof key === "undefined") { key = ''; }
if (typeof frame === "undefined") { frame = null; }
return this._world.group.add(new Phaser.Sprite(this.game, x, y, key, frame));
};
GameObjectFactory.prototype.audio = function (key, volume, loop) {
if (typeof volume === "undefined") { volume = 1; }
if (typeof loop === "undefined") { loop = false; }
return this.game.sound.add(key, volume, loop);
};
GameObjectFactory.prototype.circle = function (x, y, radius) {
return new Phaser.Physics.Circle(this.game, x, y, radius);
};
GameObjectFactory.prototype.aabb = function (x, y, width, height) {
return new Phaser.Physics.AABB(this.game, x, y, Math.floor(width / 2), Math.floor(height / 2));
};
GameObjectFactory.prototype.cell = function (x, y, width, height, state) {
if (typeof state === "undefined") { state = Phaser.Physics.TileMapCell.TID_FULL; }
return new Phaser.Physics.TileMapCell(this.game, x, y, width, height).SetState(state);
};
GameObjectFactory.prototype.dynamicTexture = /**
* Create a new DynamicTexture with specific size.
*
* @param width {number} Width of the texture.
* @param height {number} Height of the texture.
* @returns {DynamicTexture} The newly created dynamic texture object.
*/
function (width, height) {
return new Phaser.Display.DynamicTexture(this.game, width, height);
};
GameObjectFactory.prototype.group = /**
* Create a new object container.
*
* @param maxSize {number} Optional, capacity of this group.
* @returns {Group} The newly created group.
*/
function (maxSize) {
if (typeof maxSize === "undefined") { maxSize = 0; }
return this._world.group.add(new Phaser.Group(this.game, maxSize));
};
GameObjectFactory.prototype.scrollZone = /**
* Create a new Particle.
*
* @return {Particle} The newly created particle object.
*/
//public particle(): Phaser.ArcadeParticle {
// return new Phaser.ArcadeParticle(this.game);
//}
/**
* Create a new Emitter.
*
* @param x {number} Optional, x position of the emitter.
* @param y {number} Optional, y position of the emitter.
* @param size {number} Optional, size of this emitter.
* @return {Emitter} The newly created emitter object.
*/
//public emitter(x: number = 0, y: number = 0, size: number = 0): Phaser.ArcadeEmitter {
// return <Phaser.ArcadeEmitter> this._world.group.add(new Phaser.ArcadeEmitter(this.game, x, y, size));
//}
/**
* Create a new ScrollZone object with image key, position and size.
*
* @param key {string} Key to a image you wish this object to use.
* @param x {number} X position of this object.
* @param y {number} Y position of this object.
* @param width number} Width of this object.
* @param height {number} Height of this object.
* @returns {ScrollZone} The newly created scroll zone object.
*/
function (key, x, y, width, height) {
if (typeof x === "undefined") { x = 0; }
if (typeof y === "undefined") { y = 0; }
if (typeof width === "undefined") { width = 0; }
if (typeof height === "undefined") { height = 0; }
return this._world.group.add(new Phaser.ScrollZone(this.game, key, x, y, width, height));
};
GameObjectFactory.prototype.tilemap = /**
* Create a new Tilemap.
*
* @param key {string} Key for tileset image.
* @param mapData {string} Data of this tilemap.
* @param format {number} Format of map data. (Tilemap.FORMAT_CSV or Tilemap.FORMAT_TILED_JSON)
* @param [resizeWorld] {bool} resize the world to make same as tilemap?
* @param [tileWidth] {number} width of each tile.
* @param [tileHeight] {number} height of each tile.
* @return {Tilemap} The newly created tilemap object.
*/
function (key, mapData, format, resizeWorld, tileWidth, tileHeight) {
if (typeof resizeWorld === "undefined") { resizeWorld = true; }
if (typeof tileWidth === "undefined") { tileWidth = 0; }
if (typeof tileHeight === "undefined") { tileHeight = 0; }
return this._world.group.add(new Phaser.Tilemap(this.game, key, mapData, format, resizeWorld, tileWidth, tileHeight));
};
GameObjectFactory.prototype.tween = /**
* Create a tween object for a specific object. The object can be any JavaScript object or Phaser object such as Sprite.
*
* @param obj {object} Object the tween will be run on.
* @param [localReference] {bool} If true the tween will be stored in the object.tween property so long as it exists. If already set it'll be over-written.
* @return {Phaser.Tween} The newly created tween object.
*/
function (obj, localReference) {
if (typeof localReference === "undefined") { localReference = false; }
return this.game.tweens.create(obj, localReference);
};
GameObjectFactory.prototype.existingSprite = /**
* Add an existing Sprite to the current world.
* Note: This doesn't check or update the objects reference to Game. If that is wrong, all kinds of things will break.
*
* @param sprite The Sprite to add to the Game World
* @return {Phaser.Sprite} The Sprite object
*/
function (sprite) {
return this._world.group.add(sprite);
};
GameObjectFactory.prototype.existingGroup = /**
* Add an existing Group to the current world.
* Note: This doesn't check or update the objects reference to Game. If that is wrong, all kinds of things will break.
*
* @param group The Group to add to the Game World
* @return {Phaser.Group} The Group object
*/
function (group) {
return this._world.group.add(group);
};
GameObjectFactory.prototype.existingButton = /**
* Add an existing Button to the current world.
* Note: This doesn't check or update the objects reference to Game. If that is wrong, all kinds of things will break.
*
* @param button The Button to add to the Game World
* @return {Phaser.Button} The Button object
*/
function (button) {
return this._world.group.add(button);
};
GameObjectFactory.prototype.existingScrollZone = /**
* Add an existing GeomSprite to the current world.
* Note: This doesn't check or update the objects reference to Game. If that is wrong, all kinds of things will break.
*
* @param sprite The GeomSprite to add to the Game World
* @return {Phaser.GeomSprite} The GeomSprite object
*/
//public existingGeomSprite(sprite: GeomSprite): GeomSprite {
// return this._world.group.add(sprite);
//}
/**
* Add an existing Emitter to the current world.
* Note: This doesn't check or update the objects reference to Game. If that is wrong, all kinds of things will break.
*
* @param emitter The Emitter to add to the Game World
* @return {Phaser.Emitter} The Emitter object
*/
//public existingEmitter(emitter: Phaser.ArcadeEmitter): Phaser.ArcadeEmitter {
// return this._world.group.add(emitter);
//}
/**
* Add an existing ScrollZone to the current world.
* Note: This doesn't check or update the objects reference to Game. If that is wrong, all kinds of things will break.
*
* @param scrollZone The ScrollZone to add to the Game World
* @return {Phaser.ScrollZone} The ScrollZone object
*/
function (scrollZone) {
return this._world.group.add(scrollZone);
};
GameObjectFactory.prototype.existingTilemap = /**
* Add an existing Tilemap to the current world.
* Note: This doesn't check or update the objects reference to Game. If that is wrong, all kinds of things will break.
*
* @param tilemap The Tilemap to add to the Game World
* @return {Phaser.Tilemap} The Tilemap object
*/
function (tilemap) {
return this._world.group.add(tilemap);
};
GameObjectFactory.prototype.existingTween = /**
* Add an existing Tween to the current world.
* Note: This doesn't check or update the objects reference to Game. If that is wrong, all kinds of things will break.
*
* @param tween The Tween to add to the Game World
* @return {Phaser.Tween} The Tween object
*/
function (tween) {
return this.game.tweens.add(tween);
};
return GameObjectFactory;
})();
Phaser.GameObjectFactory = GameObjectFactory;
})(Phaser || (Phaser = {}));
+276
View File
@@ -0,0 +1,276 @@
/// <reference path="../_definitions.ts" />
/**
* Phaser - GameObjectFactory
*
* A quick way to create new world objects and add existing objects to the current world.
*/
module Phaser {
export class GameObjectFactory {
/**
* GameObjectFactory constructor
* @param game {Game} A reference to the current Game.
*/
constructor(game: Phaser.Game) {
this.game = game;
this._world = this.game.world;
}
/**
* Local reference to Game
*/
public game: Phaser.Game;
/**
* Local private reference to World
*/
private _world: Phaser.World;
/**
* Create a new camera with specific position and size.
*
* @param x {number} X position of the new camera.
* @param y {number} Y position of the new camera.
* @param width {number} Width of the new camera.
* @param height {number} Height of the new camera.
* @returns {Camera} The newly created camera object.
*/
public camera(x: number, y: number, width: number, height: number): Phaser.Camera {
return this._world.cameras.addCamera(x, y, width, height);
}
/**
* Create a new GeomSprite with specific position.
*
* @param x {number} X position of the new geom sprite.
* @param y {number} Y position of the new geom sprite.
* @returns {GeomSprite} The newly created geom sprite object.
*/
//public geomSprite(x: number, y: number): GeomSprite {
// return <GeomSprite> this._world.group.add(new GeomSprite(this.game, x, y));
//}
/**
* Create a new Button game object.
*
* @param [x] {number} X position of the button.
* @param [y] {number} Y position of the button.
* @param [key] {string} The image key as defined in the Game.Cache to use as the texture for this button.
* @param [callback] {function} The function to call when this button is pressed
* @param [callbackContext] {object} The context in which the callback will be called (usually 'this')
* @param [overFrame] {string|number} This is the frame or frameName that will be set when this button is in an over state. Give either a number to use a frame ID or a string for a frame name.
* @param [outFrame] {string|number} This is the frame or frameName that will be set when this button is in an out state. Give either a number to use a frame ID or a string for a frame name.
* @param [downFrame] {string|number} This is the frame or frameName that will be set when this button is in a down state. Give either a number to use a frame ID or a string for a frame name.
* @returns {Button} The newly created button object.
*/
public button(x: number = 0, y: number = 0, key: string = null, callback = null, callbackContext = null, overFrame = null, outFrame = null, downFrame = null): Phaser.UI.Button {
return <Phaser.UI.Button> this._world.group.add(new Phaser.UI.Button(this.game, x, y, key, callback, callbackContext, overFrame, outFrame, downFrame));
}
/**
* Create a new Sprite with specific position and sprite sheet key.
*
* @param x {number} X position of the new sprite.
* @param y {number} Y position of the new sprite.
* @param [key] {string} The image key as defined in the Game.Cache to use as the texture for this sprite
* @param [frame] {string|number} If the sprite uses an image from a texture atlas or sprite sheet you can pass the frame here. Either a number for a frame ID or a string for a frame name.
* @returns {Sprite} The newly created sprite object.
*/
public sprite(x: number, y: number, key: string = '', frame = null): Phaser.Sprite {
return <Phaser.Sprite> this._world.group.add(new Phaser.Sprite(this.game, x, y, key, frame));
}
public audio(key: string, volume: number = 1, loop: bool = false): Phaser.Sound {
return <Phaser.Sound> this.game.sound.add(key, volume, loop);
}
public circle(x: number, y: number, radius: number): Phaser.Physics.Circle {
return new Phaser.Physics.Circle(this.game, x, y, radius);
}
public aabb(x: number, y: number, width: number, height:number): Phaser.Physics.AABB {
return new Phaser.Physics.AABB(this.game, x, y, Math.floor(width / 2), Math.floor(height / 2));
}
public cell(x: number, y: number, width: number, height: number, state: number = Phaser.Physics.TileMapCell.TID_FULL): Phaser.Physics.TileMapCell {
return new Phaser.Physics.TileMapCell(this.game, x, y, width, height).SetState(state);
}
/**
* Create a new DynamicTexture with specific size.
*
* @param width {number} Width of the texture.
* @param height {number} Height of the texture.
* @returns {DynamicTexture} The newly created dynamic texture object.
*/
public dynamicTexture(width: number, height: number): Phaser.Display.DynamicTexture {
return new Phaser.Display.DynamicTexture(this.game, width, height);
}
/**
* Create a new object container.
*
* @param maxSize {number} Optional, capacity of this group.
* @returns {Group} The newly created group.
*/
public group(maxSize: number = 0): Phaser.Group {
return <Phaser.Group> this._world.group.add(new Phaser.Group(this.game, maxSize));
}
/**
* Create a new Particle.
*
* @return {Particle} The newly created particle object.
*/
//public particle(): Phaser.ArcadeParticle {
// return new Phaser.ArcadeParticle(this.game);
//}
/**
* Create a new Emitter.
*
* @param x {number} Optional, x position of the emitter.
* @param y {number} Optional, y position of the emitter.
* @param size {number} Optional, size of this emitter.
* @return {Emitter} The newly created emitter object.
*/
//public emitter(x: number = 0, y: number = 0, size: number = 0): Phaser.ArcadeEmitter {
// return <Phaser.ArcadeEmitter> this._world.group.add(new Phaser.ArcadeEmitter(this.game, x, y, size));
//}
/**
* Create a new ScrollZone object with image key, position and size.
*
* @param key {string} Key to a image you wish this object to use.
* @param x {number} X position of this object.
* @param y {number} Y position of this object.
* @param width number} Width of this object.
* @param height {number} Height of this object.
* @returns {ScrollZone} The newly created scroll zone object.
*/
public scrollZone(key: string, x: number = 0, y: number = 0, width: number = 0, height: number = 0): Phaser.ScrollZone {
return <Phaser.ScrollZone> this._world.group.add(new Phaser.ScrollZone(this.game, key, x, y, width, height));
}
/**
* Create a new Tilemap.
*
* @param key {string} Key for tileset image.
* @param mapData {string} Data of this tilemap.
* @param format {number} Format of map data. (Tilemap.FORMAT_CSV or Tilemap.FORMAT_TILED_JSON)
* @param [resizeWorld] {bool} resize the world to make same as tilemap?
* @param [tileWidth] {number} width of each tile.
* @param [tileHeight] {number} height of each tile.
* @return {Tilemap} The newly created tilemap object.
*/
public tilemap(key: string, mapData: string, format: number, resizeWorld: bool = true, tileWidth: number = 0, tileHeight: number = 0): Phaser.Tilemap {
return <Phaser.Tilemap> this._world.group.add(new Phaser.Tilemap(this.game, key, mapData, format, resizeWorld, tileWidth, tileHeight));
}
/**
* Create a tween object for a specific object. The object can be any JavaScript object or Phaser object such as Sprite.
*
* @param obj {object} Object the tween will be run on.
* @param [localReference] {bool} If true the tween will be stored in the object.tween property so long as it exists. If already set it'll be over-written.
* @return {Phaser.Tween} The newly created tween object.
*/
public tween(obj, localReference: bool = false): Phaser.Tween {
return this.game.tweens.create(obj, localReference);
}
/**
* Add an existing Sprite to the current world.
* Note: This doesn't check or update the objects reference to Game. If that is wrong, all kinds of things will break.
*
* @param sprite The Sprite to add to the Game World
* @return {Phaser.Sprite} The Sprite object
*/
public existingSprite(sprite: Phaser.Sprite): Phaser.Sprite {
return this._world.group.add(sprite);
}
/**
* Add an existing Group to the current world.
* Note: This doesn't check or update the objects reference to Game. If that is wrong, all kinds of things will break.
*
* @param group The Group to add to the Game World
* @return {Phaser.Group} The Group object
*/
public existingGroup(group: Phaser.Group): Phaser.Group {
return this._world.group.add(group);
}
/**
* Add an existing Button to the current world.
* Note: This doesn't check or update the objects reference to Game. If that is wrong, all kinds of things will break.
*
* @param button The Button to add to the Game World
* @return {Phaser.Button} The Button object
*/
public existingButton(button: Phaser.UI.Button): Phaser.UI.Button {
return this._world.group.add(button);
}
/**
* Add an existing GeomSprite to the current world.
* Note: This doesn't check or update the objects reference to Game. If that is wrong, all kinds of things will break.
*
* @param sprite The GeomSprite to add to the Game World
* @return {Phaser.GeomSprite} The GeomSprite object
*/
//public existingGeomSprite(sprite: GeomSprite): GeomSprite {
// return this._world.group.add(sprite);
//}
/**
* Add an existing Emitter to the current world.
* Note: This doesn't check or update the objects reference to Game. If that is wrong, all kinds of things will break.
*
* @param emitter The Emitter to add to the Game World
* @return {Phaser.Emitter} The Emitter object
*/
//public existingEmitter(emitter: Phaser.ArcadeEmitter): Phaser.ArcadeEmitter {
// return this._world.group.add(emitter);
//}
/**
* Add an existing ScrollZone to the current world.
* Note: This doesn't check or update the objects reference to Game. If that is wrong, all kinds of things will break.
*
* @param scrollZone The ScrollZone to add to the Game World
* @return {Phaser.ScrollZone} The ScrollZone object
*/
public existingScrollZone(scrollZone: Phaser.ScrollZone): Phaser.ScrollZone {
return this._world.group.add(scrollZone);
}
/**
* Add an existing Tilemap to the current world.
* Note: This doesn't check or update the objects reference to Game. If that is wrong, all kinds of things will break.
*
* @param tilemap The Tilemap to add to the Game World
* @return {Phaser.Tilemap} The Tilemap object
*/
public existingTilemap(tilemap: Phaser.Tilemap): Phaser.Tilemap {
return this._world.group.add(tilemap);
}
/**
* Add an existing Tween to the current world.
* Note: This doesn't check or update the objects reference to Game. If that is wrong, all kinds of things will break.
*
* @param tween The Tween to add to the Game World
* @return {Phaser.Tween} The Tween object
*/
public existingTween(tween: Phaser.Tween): Phaser.Tween {
return this.game.tweens.add(tween);
}
}
}
+85
View File
@@ -0,0 +1,85 @@
/// <reference path="../_definitions.ts" />
module Phaser {
export interface IGameObject {
/**
* Reference to the main game object
*/
game: Phaser.Game;
/**
* The type of game object.
*/
type: number;
/**
* The ID of the Group this Sprite belongs to.
*/
group: Phaser.Group;
/**
* The name of the Game Object. Typically not set by Phaser, but extremely useful for debugging / logic.
*/
name: string;
/**
* x value of the object.
*/
x: number;
/**
* y value of the object.
*/
y: number;
/**
* z-index value of the object.
*/
z: number;
/**
* Controls if both <code>update</code> and render are called by the core game loop.
*/
exists: bool;
/**
* Controls if <code>update()</code> is automatically called by the core game loop.
*/
active: bool;
/**
* Controls if this is rendered or skipped during the core game loop.
*/
visible: bool;
/**
* The animation manager component
*/
animations: Phaser.Components.AnimationManager;
/**
* Associated events
*/
events: Phaser.Components.Events;
/**
* The input component
*/
input: Phaser.Components.InputHandler;
/**
* The texture used to render.
*/
texture: Phaser.Display.Texture;
/**
* The transform component.
*/
transform: Phaser.Components.TransformManager;
//transform;
}
}
+148
View File
@@ -0,0 +1,148 @@
/// <reference path="../_definitions.ts" />
/**
* Phaser - ScrollRegion
*
* Creates a scrolling region within a ScrollZone.
* It is scrolled via the scrollSpeed.x/y properties.
*/
var Phaser;
(function (Phaser) {
var ScrollRegion = (function () {
/**
* ScrollRegion constructor
* Create a new <code>ScrollRegion</code>.
*
* @param x {number} X position in world coordinate.
* @param y {number} Y position in world coordinate.
* @param width {number} Width of this object.
* @param height {number} Height of this object.
* @param speedX {number} X-axis scrolling speed.
* @param speedY {number} Y-axis scrolling speed.
*/
function ScrollRegion(x, y, width, height, speedX, speedY) {
this._anchorWidth = 0;
this._anchorHeight = 0;
this._inverseWidth = 0;
this._inverseHeight = 0;
/**
* Will this region be rendered? (default to true)
* @type {bool}
*/
this.visible = true;
// Our seamless scrolling quads
this._A = new Phaser.Rectangle(x, y, width, height);
this._B = new Phaser.Rectangle(x, y, width, height);
this._C = new Phaser.Rectangle(x, y, width, height);
this._D = new Phaser.Rectangle(x, y, width, height);
this._scroll = new Phaser.Vec2();
this._bounds = new Phaser.Rectangle(x, y, width, height);
this.scrollSpeed = new Phaser.Vec2(speedX, speedY);
}
ScrollRegion.prototype.update = /**
* Update region scrolling with tick time.
* @param delta {number} Elapsed time since last update.
*/
function (delta) {
this._scroll.x += this.scrollSpeed.x;
this._scroll.y += this.scrollSpeed.y;
if(this._scroll.x > this._bounds.right) {
this._scroll.x = this._bounds.x;
}
if(this._scroll.x < this._bounds.x) {
this._scroll.x = this._bounds.right;
}
if(this._scroll.y > this._bounds.bottom) {
this._scroll.y = this._bounds.y;
}
if(this._scroll.y < this._bounds.y) {
this._scroll.y = this._bounds.bottom;
}
// Anchor Dimensions
this._anchorWidth = (this._bounds.width - this._scroll.x) + this._bounds.x;
this._anchorHeight = (this._bounds.height - this._scroll.y) + this._bounds.y;
if(this._anchorWidth > this._bounds.width) {
this._anchorWidth = this._bounds.width;
}
if(this._anchorHeight > this._bounds.height) {
this._anchorHeight = this._bounds.height;
}
this._inverseWidth = this._bounds.width - this._anchorWidth;
this._inverseHeight = this._bounds.height - this._anchorHeight;
// Rectangle A
this._A.setTo(this._scroll.x, this._scroll.y, this._anchorWidth, this._anchorHeight);
// Rectangle B
this._B.y = this._scroll.y;
this._B.width = this._inverseWidth;
this._B.height = this._anchorHeight;
// Rectangle C
this._C.x = this._scroll.x;
this._C.width = this._anchorWidth;
this._C.height = this._inverseHeight;
// Rectangle D
this._D.width = this._inverseWidth;
this._D.height = this._inverseHeight;
};
ScrollRegion.prototype.render = /**
* Render this region to specific context.
* @param context {CanvasRenderingContext2D} Canvas context this region will be rendered to.
* @param texture {object} The texture to be rendered.
* @param dx {number} X position in world coordinate.
* @param dy {number} Y position in world coordinate.
* @param width {number} Width of this region to be rendered.
* @param height {number} Height of this region to be rendered.
*/
function (context, texture, dx, dy, dw, dh) {
if(this.visible == false) {
return;
}
// dx/dy are the world coordinates to render the FULL ScrollZone into.
// This ScrollRegion may be smaller than that and offset from the dx/dy coordinates.
this.crop(context, texture, this._A.x, this._A.y, this._A.width, this._A.height, dx, dy, dw, dh, 0, 0);
this.crop(context, texture, this._B.x, this._B.y, this._B.width, this._B.height, dx, dy, dw, dh, this._A.width, 0);
this.crop(context, texture, this._C.x, this._C.y, this._C.width, this._C.height, dx, dy, dw, dh, 0, this._A.height);
this.crop(context, texture, this._D.x, this._D.y, this._D.width, this._D.height, dx, dy, dw, dh, this._C.width, this._A.height);
//context.fillStyle = 'rgb(255,255,255)';
//context.font = '18px Arial';
//context.fillText('RectangleA: ' + this._A.toString(), 32, 450);
//context.fillText('RectangleB: ' + this._B.toString(), 32, 480);
//context.fillText('RectangleC: ' + this._C.toString(), 32, 510);
//context.fillText('RectangleD: ' + this._D.toString(), 32, 540);
};
ScrollRegion.prototype.crop = /**
* Crop part of the texture and render it to the given context.
* @param context {CanvasRenderingContext2D} Canvas context the texture will be rendered to.
* @param texture {object} Texture to be rendered.
* @param srcX {number} Target region top-left x coordinate in the texture.
* @param srcX {number} Target region top-left y coordinate in the texture.
* @param srcW {number} Target region width in the texture.
* @param srcH {number} Target region height in the texture.
* @param destX {number} Render region top-left x coordinate in the context.
* @param destX {number} Render region top-left y coordinate in the context.
* @param destW {number} Target region width in the context.
* @param destH {number} Target region height in the context.
* @param offsetX {number} X offset to the context.
* @param offsetY {number} Y offset to the context.
*/
function (context, texture, srcX, srcY, srcW, srcH, destX, destY, destW, destH, offsetX, offsetY) {
offsetX += destX;
offsetY += destY;
if(srcW > (destX + destW) - offsetX) {
srcW = (destX + destW) - offsetX;
}
if(srcH > (destY + destH) - offsetY) {
srcH = (destY + destH) - offsetY;
}
srcX = Math.floor(srcX);
srcY = Math.floor(srcY);
srcW = Math.floor(srcW);
srcH = Math.floor(srcH);
offsetX = Math.floor(offsetX + this._bounds.x);
offsetY = Math.floor(offsetY + this._bounds.y);
if(srcW > 0 && srcH > 0) {
context.drawImage(texture, srcX, srcY, srcW, srcH, offsetX, offsetY, srcW, srcH);
}
};
return ScrollRegion;
})();
Phaser.ScrollRegion = ScrollRegion;
})(Phaser || (Phaser = {}));
+206
View File
@@ -0,0 +1,206 @@
/// <reference path="../_definitions.ts" />
/**
* Phaser - ScrollRegion
*
* Creates a scrolling region within a ScrollZone.
* It is scrolled via the scrollSpeed.x/y properties.
*/
module Phaser {
export class ScrollRegion {
/**
* ScrollRegion constructor
* Create a new <code>ScrollRegion</code>.
*
* @param x {number} X position in world coordinate.
* @param y {number} Y position in world coordinate.
* @param width {number} Width of this object.
* @param height {number} Height of this object.
* @param speedX {number} X-axis scrolling speed.
* @param speedY {number} Y-axis scrolling speed.
*/
constructor(x: number, y: number, width: number, height: number, speedX:number, speedY:number) {
// Our seamless scrolling quads
this._A = new Rectangle(x, y, width, height);
this._B = new Rectangle(x, y, width, height);
this._C = new Rectangle(x, y, width, height);
this._D = new Rectangle(x, y, width, height);
this._scroll = new Vec2();
this._bounds = new Rectangle(x, y, width, height);
this.scrollSpeed = new Vec2(speedX, speedY);
}
private _A: Rectangle;
private _B: Rectangle;
private _C: Rectangle;
private _D: Rectangle;
private _bounds: Rectangle;
private _scroll: Vec2;
private _anchorWidth: number = 0;
private _anchorHeight: number = 0;
private _inverseWidth: number = 0;
private _inverseHeight: number = 0;
/**
* Will this region be rendered? (default to true)
* @type {bool}
*/
public visible: bool = true;
/**
* Region scrolling speed.
* @type {Vec2}
*/
public scrollSpeed: Vec2;
/**
* Update region scrolling with tick time.
* @param delta {number} Elapsed time since last update.
*/
public update(delta: number) {
this._scroll.x += this.scrollSpeed.x;
this._scroll.y += this.scrollSpeed.y;
if (this._scroll.x > this._bounds.right)
{
this._scroll.x = this._bounds.x;
}
if (this._scroll.x < this._bounds.x)
{
this._scroll.x = this._bounds.right;
}
if (this._scroll.y > this._bounds.bottom)
{
this._scroll.y = this._bounds.y;
}
if (this._scroll.y < this._bounds.y)
{
this._scroll.y = this._bounds.bottom;
}
// Anchor Dimensions
this._anchorWidth = (this._bounds.width - this._scroll.x) + this._bounds.x;
this._anchorHeight = (this._bounds.height - this._scroll.y) + this._bounds.y;
if (this._anchorWidth > this._bounds.width)
{
this._anchorWidth = this._bounds.width;
}
if (this._anchorHeight > this._bounds.height)
{
this._anchorHeight = this._bounds.height;
}
this._inverseWidth = this._bounds.width - this._anchorWidth;
this._inverseHeight = this._bounds.height - this._anchorHeight;
// Rectangle A
this._A.setTo(this._scroll.x, this._scroll.y, this._anchorWidth, this._anchorHeight);
// Rectangle B
this._B.y = this._scroll.y;
this._B.width = this._inverseWidth;
this._B.height = this._anchorHeight;
// Rectangle C
this._C.x = this._scroll.x;
this._C.width = this._anchorWidth;
this._C.height = this._inverseHeight;
// Rectangle D
this._D.width = this._inverseWidth;
this._D.height = this._inverseHeight;
}
/**
* Render this region to specific context.
* @param context {CanvasRenderingContext2D} Canvas context this region will be rendered to.
* @param texture {object} The texture to be rendered.
* @param dx {number} X position in world coordinate.
* @param dy {number} Y position in world coordinate.
* @param width {number} Width of this region to be rendered.
* @param height {number} Height of this region to be rendered.
*/
public render(context:CanvasRenderingContext2D, texture, dx: number, dy: number, dw: number, dh: number) {
if (this.visible == false)
{
return;
}
// dx/dy are the world coordinates to render the FULL ScrollZone into.
// This ScrollRegion may be smaller than that and offset from the dx/dy coordinates.
this.crop(context, texture, this._A.x, this._A.y, this._A.width, this._A.height, dx, dy, dw, dh, 0, 0);
this.crop(context, texture, this._B.x, this._B.y, this._B.width, this._B.height, dx, dy, dw, dh, this._A.width, 0);
this.crop(context, texture, this._C.x, this._C.y, this._C.width, this._C.height, dx, dy, dw, dh, 0, this._A.height);
this.crop(context, texture, this._D.x, this._D.y, this._D.width, this._D.height, dx, dy, dw, dh, this._C.width, this._A.height);
//context.fillStyle = 'rgb(255,255,255)';
//context.font = '18px Arial';
//context.fillText('RectangleA: ' + this._A.toString(), 32, 450);
//context.fillText('RectangleB: ' + this._B.toString(), 32, 480);
//context.fillText('RectangleC: ' + this._C.toString(), 32, 510);
//context.fillText('RectangleD: ' + this._D.toString(), 32, 540);
}
/**
* Crop part of the texture and render it to the given context.
* @param context {CanvasRenderingContext2D} Canvas context the texture will be rendered to.
* @param texture {object} Texture to be rendered.
* @param srcX {number} Target region top-left x coordinate in the texture.
* @param srcX {number} Target region top-left y coordinate in the texture.
* @param srcW {number} Target region width in the texture.
* @param srcH {number} Target region height in the texture.
* @param destX {number} Render region top-left x coordinate in the context.
* @param destX {number} Render region top-left y coordinate in the context.
* @param destW {number} Target region width in the context.
* @param destH {number} Target region height in the context.
* @param offsetX {number} X offset to the context.
* @param offsetY {number} Y offset to the context.
*/
private crop(context, texture, srcX, srcY, srcW, srcH, destX, destY, destW, destH, offsetX, offsetY) {
offsetX += destX;
offsetY += destY;
if (srcW > (destX + destW) - offsetX)
{
srcW = (destX + destW) - offsetX;
}
if (srcH > (destY + destH) - offsetY)
{
srcH = (destY + destH) - offsetY;
}
srcX = Math.floor(srcX);
srcY = Math.floor(srcY);
srcW = Math.floor(srcW);
srcH = Math.floor(srcH);
offsetX = Math.floor(offsetX + this._bounds.x);
offsetY = Math.floor(offsetY + this._bounds.y);
if (srcW > 0 && srcH > 0)
{
context.drawImage(texture, srcX, srcY, srcW, srcH, offsetX, offsetY, srcW, srcH);
}
}
}
}
+111
View File
@@ -0,0 +1,111 @@
var __extends = this.__extends || function (d, b) {
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
/// <reference path="../_definitions.ts" />
/**
* Phaser - ScrollZone
*
* Creates a scrolling region of the given width and height from an image in the cache.
* The ScrollZone can be positioned anywhere in-world like a normal game object, re-act to physics, collision, etc.
* The image within it is scrolled via ScrollRegions and their scrollSpeed.x/y properties.
* If you create a scroll zone larger than the given source image it will create a DynamicTexture and fill it with a pattern of the source image.
*/
var Phaser;
(function (Phaser) {
var ScrollZone = (function (_super) {
__extends(ScrollZone, _super);
/**
* ScrollZone constructor
* Create a new <code>ScrollZone</code>.
*
* @param game {Phaser.Game} Current game instance.
* @param key {string} Asset key for image texture of this object.
* @param x {number} X position in world coordinate.
* @param y {number} Y position in world coordinate.
* @param [width] {number} width of this object.
* @param [height] {number} height of this object.
*/
function ScrollZone(game, key, x, y, width, height) {
if (typeof x === "undefined") { x = 0; }
if (typeof y === "undefined") { y = 0; }
if (typeof width === "undefined") { width = 0; }
if (typeof height === "undefined") { height = 0; }
_super.call(this, game, x, y, key);
this.type = Phaser.Types.SCROLLZONE;
this.regions = [];
if(this.texture.loaded) {
if(width > this.width || height > this.height) {
// Create our repeating texture (as the source image wasn't large enough for the requested size)
this.createRepeatingTexture(width, height);
this.width = width;
this.height = height;
}
// Create a default ScrollRegion at the requested size
this.addRegion(0, 0, this.width, this.height);
// If the zone is smaller than the image itself then shrink the bounds
if((width < this.width || height < this.height) && width !== 0 && height !== 0) {
this.width = width;
this.height = height;
}
}
}
ScrollZone.prototype.addRegion = /**
* Add a new region to this zone.
* @param x {number} X position of the new region.
* @param y {number} Y position of the new region.
* @param width {number} Width of the new region.
* @param height {number} Height of the new region.
* @param [speedX] {number} x-axis scrolling speed.
* @param [speedY] {number} y-axis scrolling speed.
* @return {ScrollRegion} The newly added region.
*/
function (x, y, width, height, speedX, speedY) {
if (typeof speedX === "undefined") { speedX = 0; }
if (typeof speedY === "undefined") { speedY = 0; }
if(x > this.width || y > this.height || x < 0 || y < 0 || (x + width) > this.width || (y + height) > this.height) {
throw Error('Invalid ScrollRegion defined. Cannot be larger than parent ScrollZone');
return null;
}
this.currentRegion = new Phaser.ScrollRegion(x, y, width, height, speedX, speedY);
this.regions.push(this.currentRegion);
return this.currentRegion;
};
ScrollZone.prototype.setSpeed = /**
* Set scrolling speed of current region.
* @param x {number} X speed of current region.
* @param y {number} Y speed of current region.
*/
function (x, y) {
if(this.currentRegion) {
this.currentRegion.scrollSpeed.setTo(x, y);
}
return this;
};
ScrollZone.prototype.update = /**
* Update regions.
*/
function () {
for(var i = 0; i < this.regions.length; i++) {
this.regions[i].update(this.game.time.delta);
}
};
ScrollZone.prototype.createRepeatingTexture = /**
* Create repeating texture with _texture, and store it into the _dynamicTexture.
* Used to create texture when texture image is small than size of the zone.
*/
function (regionWidth, regionHeight) {
// Work out how many we'll need of the source image to make it tile properly
var tileWidth = Math.ceil(this.width / regionWidth) * regionWidth;
var tileHeight = Math.ceil(this.height / regionHeight) * regionHeight;
var dt = new Phaser.Display.DynamicTexture(this.game, tileWidth, tileHeight);
dt.context.rect(0, 0, tileWidth, tileHeight);
dt.context.fillStyle = dt.context.createPattern(this.texture.imageTexture, "repeat");
dt.context.fill();
this.texture.loadDynamicTexture(dt);
};
return ScrollZone;
})(Phaser.Sprite);
Phaser.ScrollZone = ScrollZone;
})(Phaser || (Phaser = {}));
+147
View File
@@ -0,0 +1,147 @@
/// <reference path="../_definitions.ts" />
/**
* Phaser - ScrollZone
*
* Creates a scrolling region of the given width and height from an image in the cache.
* The ScrollZone can be positioned anywhere in-world like a normal game object, re-act to physics, collision, etc.
* The image within it is scrolled via ScrollRegions and their scrollSpeed.x/y properties.
* If you create a scroll zone larger than the given source image it will create a DynamicTexture and fill it with a pattern of the source image.
*/
module Phaser {
export class ScrollZone extends Sprite {
/**
* ScrollZone constructor
* Create a new <code>ScrollZone</code>.
*
* @param game {Phaser.Game} Current game instance.
* @param key {string} Asset key for image texture of this object.
* @param x {number} X position in world coordinate.
* @param y {number} Y position in world coordinate.
* @param [width] {number} width of this object.
* @param [height] {number} height of this object.
*/
constructor(game: Game, key:string, x: number = 0, y: number = 0, width: number = 0, height: number = 0) {
super(game, x, y, key);
this.type = Phaser.Types.SCROLLZONE;
this.regions = [];
if (this.texture.loaded)
{
if (width > this.width || height > this.height)
{
// Create our repeating texture (as the source image wasn't large enough for the requested size)
this.createRepeatingTexture(width, height);
this.width = width;
this.height = height;
}
// Create a default ScrollRegion at the requested size
this.addRegion(0, 0, this.width, this.height);
// If the zone is smaller than the image itself then shrink the bounds
if ((width < this.width || height < this.height) && width !== 0 && height !== 0)
{
this.width = width;
this.height = height;
}
}
}
/**
* Current region this zone is scrolling.
* @type {ScrollRegion}
*/
public currentRegion: Phaser.ScrollRegion;
/**
* Array contains all added regions.
* @type {ScrollRegion[]}
*/
public regions: Phaser.ScrollRegion[];
/**
* Add a new region to this zone.
* @param x {number} X position of the new region.
* @param y {number} Y position of the new region.
* @param width {number} Width of the new region.
* @param height {number} Height of the new region.
* @param [speedX] {number} x-axis scrolling speed.
* @param [speedY] {number} y-axis scrolling speed.
* @return {ScrollRegion} The newly added region.
*/
public addRegion(x: number, y: number, width: number, height: number, speedX: number = 0, speedY: number = 0): Phaser.ScrollRegion {
if (x > this.width || y > this.height || x < 0 || y < 0 || (x + width) > this.width || (y + height) > this.height)
{
throw Error('Invalid ScrollRegion defined. Cannot be larger than parent ScrollZone');
return null;
}
this.currentRegion = new Phaser.ScrollRegion(x, y, width, height, speedX, speedY);
this.regions.push(this.currentRegion);
return this.currentRegion;
}
/**
* Set scrolling speed of current region.
* @param x {number} X speed of current region.
* @param y {number} Y speed of current region.
*/
public setSpeed(x: number, y: number) {
if (this.currentRegion)
{
this.currentRegion.scrollSpeed.setTo(x, y);
}
return this;
}
/**
* Update regions.
*/
public update() {
for (var i = 0; i < this.regions.length; i++)
{
this.regions[i].update(this.game.time.delta);
}
}
/**
* Create repeating texture with _texture, and store it into the _dynamicTexture.
* Used to create texture when texture image is small than size of the zone.
*/
private createRepeatingTexture(regionWidth: number, regionHeight: number) {
// Work out how many we'll need of the source image to make it tile properly
var tileWidth = Math.ceil(this.width / regionWidth) * regionWidth;
var tileHeight = Math.ceil(this.height / regionHeight) * regionHeight;
var dt: Phaser.Display.DynamicTexture = new Phaser.Display.DynamicTexture(this.game, tileWidth, tileHeight);
dt.context.rect(0, 0, tileWidth, tileHeight);
dt.context.fillStyle = dt.context.createPattern(this.texture.imageTexture, "repeat");
dt.context.fill();
this.texture.loadDynamicTexture(dt);
}
}
}
+263
View File
@@ -0,0 +1,263 @@
/// <reference path="../_definitions.ts" />
/**
* Phaser - Sprite
*/
var Phaser;
(function (Phaser) {
var Sprite = (function () {
/**
* Create a new <code>Sprite</code>.
*
* @param game {Phaser.Game} Current game instance.
* @param [x] {number} the initial x position of the sprite.
* @param [y] {number} the initial y position of the sprite.
* @param [key] {string} Key of the graphic you want to load for this sprite.
*/
function Sprite(game, x, y, key, frame) {
if (typeof x === "undefined") { x = 0; }
if (typeof y === "undefined") { y = 0; }
if (typeof key === "undefined") { key = null; }
if (typeof frame === "undefined") { frame = null; }
/**
* A bool representing if the Sprite has been modified in any way via a scale, rotate, flip or skew.
*/
this.modified = false;
/**
* x value of the object.
*/
this.x = 0;
/**
* y value of the object.
*/
this.y = 0;
/**
* z order value of the object.
*/
this.z = -1;
/**
* Render iteration counter
*/
this.renderOrderID = 0;
this.game = game;
this.type = Phaser.Types.SPRITE;
this.exists = true;
this.active = true;
this.visible = true;
this.alive = true;
this.x = x;
this.y = y;
this.z = -1;
this.group = null;
this.name = '';
this.events = new Phaser.Components.Events(this);
this.animations = new Phaser.Components.AnimationManager(this);
this.input = new Phaser.Components.InputHandler(this);
this.texture = new Phaser.Display.Texture(this);
this.transform = new Phaser.Components.TransformManager(this);
if(key !== null) {
this.texture.loadImage(key, false);
} else {
this.texture.opaque = true;
}
if(frame !== null) {
if(typeof frame == 'string') {
this.frameName = frame;
} else {
this.frame = frame;
}
}
this.worldView = new Phaser.Rectangle(x, y, this.width, this.height);
this.cameraView = new Phaser.Rectangle(x, y, this.width, this.height);
this.transform.setCache();
this.body = new Phaser.Physics.Body(this, 0);
this.outOfBounds = false;
this.outOfBoundsAction = Phaser.Types.OUT_OF_BOUNDS_PERSIST;
// Handy proxies
this.scale = this.transform.scale;
this.alpha = this.texture.alpha;
this.origin = this.transform.origin;
this.crop = this.texture.crop;
}
Object.defineProperty(Sprite.prototype, "rotation", {
get: /**
* The rotation of the sprite in degrees. Phaser uses a right-handed coordinate system, where 0 points to the right.
*/
function () {
return this.transform.rotation;
},
set: /**
* Set the rotation of the sprite in degrees. Phaser uses a right-handed coordinate system, where 0 points to the right.
* The value is automatically wrapped to be between 0 and 360.
*/
function (value) {
this.transform.rotation = this.game.math.wrap(value, 360, 0);
if(this.body) {
//this.body.angle = this.game.math.degreesToRadians(this.game.math.wrap(value, 360, 0));
}
},
enumerable: true,
configurable: true
});
Sprite.prototype.bringToTop = /**
* Brings this Sprite to the top of its current Group, if set.
*/
function () {
if(this.group) {
this.group.bringToTop(this);
}
};
Object.defineProperty(Sprite.prototype, "alpha", {
get: /**
* The alpha of the Sprite between 0 and 1, a value of 1 being fully opaque.
*/
function () {
return this.texture.alpha;
},
set: /**
* The alpha of the Sprite between 0 and 1, a value of 1 being fully opaque.
*/
function (value) {
this.texture.alpha = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Sprite.prototype, "frame", {
get: /**
* Get the animation frame number.
*/
function () {
return this.animations.frame;
},
set: /**
* Set the animation frame by frame number.
*/
function (value) {
this.animations.frame = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Sprite.prototype, "frameName", {
get: /**
* Get the animation frame name.
*/
function () {
return this.animations.frameName;
},
set: /**
* Set the animation frame by frame name.
*/
function (value) {
this.animations.frameName = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Sprite.prototype, "width", {
get: function () {
return this.texture.width * this.transform.scale.x;
},
set: function (value) {
this.transform.scale.x = value / this.texture.width;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Sprite.prototype, "height", {
get: function () {
return this.texture.height * this.transform.scale.y;
},
set: function (value) {
this.transform.scale.y = value / this.texture.height;
},
enumerable: true,
configurable: true
});
Sprite.prototype.preUpdate = /**
* Pre-update is called right before update() on each object in the game loop.
*/
function () {
this.transform.update();
if(this.transform.scrollFactor.x != 1 && this.transform.scrollFactor.x != 0) {
this.worldView.x = (this.x * this.transform.scrollFactor.x) - (this.width * this.transform.origin.x);
} else {
this.worldView.x = this.x - (this.width * this.transform.origin.x);
}
if(this.transform.scrollFactor.y != 1 && this.transform.scrollFactor.y != 0) {
this.worldView.y = (this.y * this.transform.scrollFactor.y) - (this.height * this.transform.origin.y);
} else {
this.worldView.y = this.y - (this.height * this.transform.origin.y);
}
this.worldView.width = this.width;
this.worldView.height = this.height;
if(this.modified == false && (!this.transform.scale.equals(1) || !this.transform.skew.equals(0) || this.transform.rotation != 0 || this.transform.rotationOffset != 0 || this.texture.flippedX || this.texture.flippedY)) {
this.modified = true;
}
};
Sprite.prototype.update = /**
* Override this function to update your sprites position and appearance.
*/
function () {
};
Sprite.prototype.postUpdate = /**
* Automatically called after update() by the game loop for all 'alive' objects.
*/
function () {
this.animations.update();
this.checkBounds();
//this.transform.centerOn(this.body.aabb.pos.x, this.body.aabb.pos.y);
if(this.modified == true && this.transform.scale.equals(1) && this.transform.skew.equals(0) && this.transform.rotation == 0 && this.transform.rotationOffset == 0 && this.texture.flippedX == false && this.texture.flippedY == false) {
this.modified = false;
}
};
Sprite.prototype.checkBounds = function () {
if(Phaser.RectangleUtils.intersects(this.worldView, this.game.world.bounds)) {
this.outOfBounds = false;
} else {
if(this.outOfBounds == false) {
this.events.onOutOfBounds.dispatch(this);
}
this.outOfBounds = true;
if(this.outOfBoundsAction == Phaser.Types.OUT_OF_BOUNDS_KILL) {
this.kill();
} else if(this.outOfBoundsAction == Phaser.Types.OUT_OF_BOUNDS_DESTROY) {
this.destroy();
}
}
};
Sprite.prototype.destroy = /**
* Clean up memory.
*/
function () {
this.input.destroy();
};
Sprite.prototype.kill = /**
* Handy for "killing" game objects.
* Default behavior is to flag them as nonexistent AND dead.
* However, if you want the "corpse" to remain in the game,
* like to animate an effect or whatever, you should override this,
* setting only alive to false, and leaving exists true.
*/
function (removeFromGroup) {
if (typeof removeFromGroup === "undefined") { removeFromGroup = false; }
this.alive = false;
this.exists = false;
if(removeFromGroup && this.group) {
//this.group.remove(this);
}
this.events.onKilled.dispatch(this);
};
Sprite.prototype.revive = /**
* Handy for bringing game objects "back to life". Just sets alive and exists back to true.
* In practice, this is most often called by <code>Object.reset()</code>.
*/
function () {
this.alive = true;
this.exists = true;
this.events.onRevived.dispatch(this);
};
return Sprite;
})();
Phaser.Sprite = Sprite;
})(Phaser || (Phaser = {}));
+447
View File
@@ -0,0 +1,447 @@
/// <reference path="../_definitions.ts" />
/**
* Phaser - Sprite
*/
module Phaser {
export class Sprite implements IGameObject {
/**
* Create a new <code>Sprite</code>.
*
* @param game {Phaser.Game} Current game instance.
* @param [x] {number} the initial x position of the sprite.
* @param [y] {number} the initial y position of the sprite.
* @param [key] {string} Key of the graphic you want to load for this sprite.
*/
constructor(game: Game, x: number = 0, y: number = 0, key: string = null, frame = null) {
this.game = game;
this.type = Phaser.Types.SPRITE;
this.exists = true;
this.active = true;
this.visible = true;
this.alive = true;
this.x = x;
this.y = y;
this.z = -1;
this.group = null;
this.name = '';
this.events = new Phaser.Components.Events(this);
this.animations = new Phaser.Components.AnimationManager(this);
this.input = new Phaser.Components.InputHandler(this);
this.texture = new Phaser.Display.Texture(this);
this.transform = new Phaser.Components.TransformManager(this);
if (key !== null)
{
this.texture.loadImage(key, false);
}
else
{
this.texture.opaque = true;
}
if (frame !== null)
{
if (typeof frame == 'string')
{
this.frameName = frame;
}
else
{
this.frame = frame;
}
}
this.worldView = new Rectangle(x, y, this.width, this.height);
this.cameraView = new Rectangle(x, y, this.width, this.height);
this.transform.setCache();
//this.body = new Phaser.Physics.Body(this, 0);
this.outOfBounds = false;
this.outOfBoundsAction = Phaser.Types.OUT_OF_BOUNDS_PERSIST;
// Handy proxies
this.scale = this.transform.scale;
this.alpha = this.texture.alpha;
this.origin = this.transform.origin;
this.crop = this.texture.crop;
}
/**
* Reference to the main game object
*/
public game: Phaser.Game;
/**
* The type of game object.
*/
public type: number;
/**
* The name of game object.
*/
public name: string;
/**
* The Group this Sprite belongs to.
*/
public group: Phaser.Group;
/**
* Controls if both <code>update</code> and render are called by the core game loop.
*/
public exists: bool;
/**
* Controls if <code>update()</code> is automatically called by the core game loop.
*/
public active: bool;
/**
* Controls if this Sprite is rendered or skipped during the core game loop.
*/
public visible: bool;
/**
* A useful state for many game objects. Kill and revive both flip this switch.
*/
public alive: bool;
/**
* Is the Sprite out of the world bounds or not?
*/
public outOfBounds: bool;
/**
* The action to be taken when the sprite is fully out of the world bounds
* Defaults to Phaser.Types.OUT_OF_BOUNDS_PERSIST
*/
public outOfBoundsAction: number;
/**
* The texture used to render the Sprite.
*/
public texture: Phaser.Display.Texture;
/**
* The Sprite transform component.
*/
public transform: Phaser.Components.TransformManager;
/**
* The Input component
*/
public input: Phaser.Components.InputHandler;
/**
* The Events component
*/
public events: Phaser.Components.Events;
/**
* The Physics Body
*/
public body: Phaser.Physics.Body;
/**
* This manages animations of the sprite. You can modify animations through it. (see AnimationManager)
* @type AnimationManager
*/
public animations: Phaser.Components.AnimationManager;
/**
* A Rectangle that defines the size and placement of the Sprite in the game world,
* after taking into consideration both scrollFactor and scaling.
*/
public worldView: Phaser.Rectangle;
/**
* A Rectangle that maps to the placement of this sprite with respect to a specific Camera.
* This value is constantly updated and modified during the internal render pass, it is not meant to be accessed directly.
*/
public cameraView: Phaser.Rectangle;
/**
* A local tween variable. Used by the TweenManager when setting a tween on this sprite or a property of it.
*/
public tween: Phaser.Tween;
/**
* A bool representing if the Sprite has been modified in any way via a scale, rotate, flip or skew.
*/
public modified: bool = false;
/**
* x value of the object.
*/
public x: number = 0;
/**
* y value of the object.
*/
public y: number = 0;
/**
* z order value of the object.
*/
public z: number = -1;
/**
* Render iteration counter
*/
public renderOrderID: number = 0;
/**
* The rotation of the sprite in degrees. Phaser uses a right-handed coordinate system, where 0 points to the right.
*/
public get rotation(): number {
return this.transform.rotation;
}
/**
* Set the rotation of the sprite in degrees. Phaser uses a right-handed coordinate system, where 0 points to the right.
* The value is automatically wrapped to be between 0 and 360.
*/
public set rotation(value: number) {
this.transform.rotation = this.game.math.wrap(value, 360, 0);
if (this.body)
{
//this.body.angle = this.game.math.degreesToRadians(this.game.math.wrap(value, 360, 0));
}
}
/**
* Brings this Sprite to the top of its current Group, if set.
*/
public bringToTop() {
if (this.group)
{
this.group.bringToTop(this);
}
}
/**
* The scale of the Sprite. A value of 1 is original scale. 0.5 is half size. 2 is double the size.
* This is a reference to Sprite.transform.scale
*/
public scale: Phaser.Vec2;
/**
* The crop rectangle allows you to control which part of the sprite texture is rendered without distorting it.
* Set to null to disable, set to a Phaser.Rectangle object to control the region that will be rendered, anything outside the rectangle is ignored.
* This is a reference to Sprite.texture.crop
* @type {Phaser.Rectangle}
*/
public crop: Phaser.Rectangle;
/**
* The origin of the Sprite around which rotation and positioning takes place.
* This is a reference to Sprite.transform.origin
*/
public origin: Phaser.Vec2;
/**
* The alpha of the Sprite between 0 and 1, a value of 1 being fully opaque.
*/
public set alpha(value: number) {
this.texture.alpha = value;
}
/**
* The alpha of the Sprite between 0 and 1, a value of 1 being fully opaque.
*/
public get alpha(): number {
return this.texture.alpha;
}
/**
* Set the animation frame by frame number.
*/
public set frame(value: number) {
this.animations.frame = value;
}
/**
* Get the animation frame number.
*/
public get frame(): number {
return this.animations.frame;
}
/**
* Set the animation frame by frame name.
*/
public set frameName(value: string) {
this.animations.frameName = value;
}
/**
* Get the animation frame name.
*/
public get frameName(): string {
return this.animations.frameName;
}
public set width(value: number) {
this.transform.scale.x = value / this.texture.width;
}
public get width(): number {
return this.texture.width * this.transform.scale.x;
}
public set height(value: number) {
this.transform.scale.y = value / this.texture.height;
}
public get height(): number {
return this.texture.height * this.transform.scale.y;
}
/**
* Pre-update is called right before update() on each object in the game loop.
*/
public preUpdate() {
this.transform.update();
if (this.transform.scrollFactor.x != 1 && this.transform.scrollFactor.x != 0)
{
this.worldView.x = (this.x * this.transform.scrollFactor.x) - (this.width * this.transform.origin.x);
}
else
{
this.worldView.x = this.x - (this.width * this.transform.origin.x);
}
if (this.transform.scrollFactor.y != 1 && this.transform.scrollFactor.y != 0)
{
this.worldView.y = (this.y * this.transform.scrollFactor.y) - (this.height * this.transform.origin.y);
}
else
{
this.worldView.y = this.y - (this.height * this.transform.origin.y);
}
this.worldView.width = this.width;
this.worldView.height = this.height;
if (this.modified == false && (!this.transform.scale.equals(1) || !this.transform.skew.equals(0) || this.transform.rotation != 0 || this.transform.rotationOffset != 0 || this.texture.flippedX || this.texture.flippedY))
{
this.modified = true;
}
}
/**
* Override this function to update your sprites position and appearance.
*/
public update() {
}
/**
* Automatically called after update() by the game loop for all 'alive' objects.
*/
public postUpdate() {
this.animations.update();
this.checkBounds();
//this.transform.centerOn(this.body.aabb.pos.x, this.body.aabb.pos.y);
if (this.modified == true && this.transform.scale.equals(1) && this.transform.skew.equals(0) && this.transform.rotation == 0 && this.transform.rotationOffset == 0 && this.texture.flippedX == false && this.texture.flippedY == false)
{
this.modified = false;
}
}
private checkBounds() {
if (Phaser.RectangleUtils.intersects(this.worldView, this.game.world.bounds))
{
this.outOfBounds = false;
}
else
{
if (this.outOfBounds == false)
{
this.events.onOutOfBounds.dispatch(this);
}
this.outOfBounds = true;
if (this.outOfBoundsAction == Phaser.Types.OUT_OF_BOUNDS_KILL)
{
this.kill();
}
else if (this.outOfBoundsAction == Phaser.Types.OUT_OF_BOUNDS_DESTROY)
{
this.destroy();
}
}
}
/**
* Clean up memory.
*/
public destroy() {
this.input.destroy();
}
/**
* Handy for "killing" game objects.
* Default behavior is to flag them as nonexistent AND dead.
* However, if you want the "corpse" to remain in the game,
* like to animate an effect or whatever, you should override this,
* setting only alive to false, and leaving exists true.
*/
public kill(removeFromGroup:bool = false) {
this.alive = false;
this.exists = false;
if (removeFromGroup && this.group)
{
//this.group.remove(this);
}
this.events.onKilled.dispatch(this);
}
/**
* Handy for bringing game objects "back to life". Just sets alive and exists back to true.
* In practice, this is most often called by <code>Object.reset()</code>.
*/
public revive() {
this.alive = true;
this.exists = true;
this.events.onRevived.dispatch(this);
}
}
}
+251
View File
@@ -0,0 +1,251 @@
var Phaser;
(function (Phaser) {
/// <reference path="../_definitions.ts" />
/**
* Phaser - Components - TransformManager
*/
(function (Components) {
var TransformManager = (function () {
/**
* Creates a new TransformManager component
* @param parent The game object using this transform
*/
function TransformManager(parent) {
this._dirty = false;
/**
* 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}
*/
this.rotationOffset = 0;
/**
* The rotation of the object in degrees. Phaser uses a right-handed coordinate system, where 0 points to the right.
*/
this.rotation = 0;
this.game = parent.game;
this.parent = parent;
this.local = new Phaser.Mat3();
this.scrollFactor = new Phaser.Vec2(1, 1);
this.origin = new Phaser.Vec2();
this.scale = new Phaser.Vec2(1, 1);
this.skew = new Phaser.Vec2();
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();
}
Object.defineProperty(TransformManager.prototype, "distance", {
get: /**
* The distance from the center of the transform to the rotation origin.
*/
function () {
return this._distance;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TransformManager.prototype, "angleToCenter", {
get: /**
* The angle between the center of the transform to the rotation origin.
*/
function () {
return this._angle;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TransformManager.prototype, "offsetX", {
get: /**
* The offset on the X axis of the origin That is the difference between the top left of the Sprite and the origin.x.
* So if the origin.x is 0 the offsetX will be 0. If the origin.x is 0.5 then offsetX will be sprite width / 2, and so on.
*/
function () {
return this._offset.x;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TransformManager.prototype, "offsetY", {
get: /**
* The offset on the Y axis of the origin
*/
function () {
return this._offset.y;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TransformManager.prototype, "halfWidth", {
get: /**
* Half the width of the parent sprite, taking into consideration scaling
*/
function () {
return this._halfSize.x;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TransformManager.prototype, "halfHeight", {
get: /**
* Half the height of the parent sprite, taking into consideration scaling
*/
function () {
return this._halfSize.y;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TransformManager.prototype, "sin", {
get: /**
* The equivalent of Math.sin(rotation + rotationOffset)
*/
function () {
return this._sc.x;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TransformManager.prototype, "cos", {
get: /**
* The equivalent of Math.cos(rotation + rotationOffset)
*/
function () {
return this._sc.y;
},
enumerable: true,
configurable: true
});
TransformManager.prototype.centerOn = /**
* Moves the sprite so its center is located on the given x and y coordinates.
* Doesn't change the origin of the sprite.
*/
function (x, y) {
this.parent.x = x + (this.parent.x - this.center.x);
this.parent.y = y + (this.parent.y - this.center.y);
//this.setCache();
};
TransformManager.prototype.setCache = /**
* Populates the transform cache. Called by the parent object on creation.
*/
function () {
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.x, this.halfWidth - this._offset.y);
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._scA.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._angle);
this._scA.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._angle);
this._prevRotation = this.rotation;
if(this.parent.texture && this.parent.texture.renderRotation) {
this._sc.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD);
this._sc.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD);
} else {
this._sc.x = 0;
this._sc.y = 1;
}
this.center.x = this.parent.x + this._distance * this._scA.y;
this.center.y = this.parent.y + this._distance * this._scA.x;
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;
this._flippedX = this.parent.texture.flippedX;
this._flippedY = this.parent.texture.flippedY;
};
TransformManager.prototype.update = /**
* Updates the local transform matrix and the cache values if anything has changed in the parent.
*/
function () {
// Check cache
this._dirty = false;
// 1) Height or Width change (also triggered by a change in scale) or an Origin change
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) {
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)));
// Store
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._dirty = true;
}
// 2) Rotation change
if(this.rotation != this._prevRotation || this._dirty) {
this._scA.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._angle);
this._scA.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._angle);
if(this.parent.texture.renderRotation) {
this._sc.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD);
this._sc.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD);
} else {
this._sc.x = 0;
this._sc.y = 1;
}
// Store
this._prevRotation = this.rotation;
this._dirty = true;
}
// 3) If it has moved (or any of the above) then update the edges and center
if(this._dirty || this.parent.x != this._pos.x || this.parent.y != this._pos.y) {
this.center.x = this.parent.x + this._distance * this._scA.y;
this.center.y = this.parent.y + this._distance * this._scA.x;
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;
// Translate
this.local.data[2] = this.parent.x;
this.local.data[5] = this.parent.y;
}
// Scale and Skew
if(this._dirty || this._flippedX != this.parent.texture.flippedX) {
this._flippedX = this.parent.texture.flippedX;
if(this._flippedX) {
this.local.data[0] = this._sc.y * -this.scale.x;
this.local.data[3] = (this._sc.x * -this.scale.x) + this.skew.x;
} else {
this.local.data[0] = this._sc.y * this.scale.x;
this.local.data[3] = (this._sc.x * this.scale.x) + this.skew.x;
}
}
if(this._dirty || this._flippedY != this.parent.texture.flippedY) {
this._flippedY = this.parent.texture.flippedY;
if(this._flippedY) {
this.local.data[4] = this._sc.y * -this.scale.y;
this.local.data[1] = -(this._sc.x * -this.scale.y) + this.skew.y;
} else {
this.local.data[4] = this._sc.y * this.scale.y;
this.local.data[1] = -(this._sc.x * this.scale.y) + this.skew.y;
}
}
};
return TransformManager;
})();
Components.TransformManager = TransformManager;
})(Phaser.Components || (Phaser.Components = {}));
var Components = Phaser.Components;
})(Phaser || (Phaser = {}));
+357
View File
@@ -0,0 +1,357 @@
/// <reference path="../_definitions.ts" />
/**
* Phaser - Components - TransformManager
*/
module Phaser.Components {
export class TransformManager {
/**
* Creates a new TransformManager component
* @param parent The game object using this transform
*/
constructor(parent) {
this.game = parent.game;
this.parent = parent;
this.local = new Phaser.Mat3;
this.scrollFactor = new Phaser.Vec2(1, 1);
this.origin = new Phaser.Vec2;
this.scale = new Phaser.Vec2(1, 1);
this.skew = new Phaser.Vec2;
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;
}
private _rotation: number;
private _dirty: bool = false;
// 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;
private _flippedX: bool;
private _flippedY: bool;
/**
* Reference to Phaser.Game
*/
public game: Phaser.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;
/**
* The origin is the point around which scale and rotation takes place and defaults to the top-left of the sprite.
*/
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;
/**
* The center of the Sprite in world coordinates, after taking scaling and rotation into consideration
*/
public center: Phaser.Point;
/**
* The upper-left corner of the Sprite in world coordinates, after taking scaling and rotation into consideration
*/
public upperLeft: Phaser.Point;
/**
* The upper-right corner of the Sprite in world coordinates, after taking scaling and rotation into consideration
*/
public upperRight: Phaser.Point;
/**
* The bottom-left corner of the Sprite in world coordinates, after taking scaling and rotation into consideration
*/
public bottomLeft: Phaser.Point;
/**
* The bottom-right corner of the Sprite in world coordinates, after taking scaling and rotation into consideration
*/
public bottomRight: Phaser.Point;
/**
* The local transform matrix
*/
public local: Phaser.Mat3;
/**
* The distance from the center of the transform to the rotation origin.
*/
public get distance(): number {
return this._distance;
}
/**
* The angle between the center of the transform to the rotation origin.
*/
public get angleToCenter(): number {
return this._angle;
}
/**
* The offset on the X axis of the origin That is the difference between the top left of the Sprite and the origin.x.
* So if the origin.x is 0 the offsetX will be 0. If the origin.x is 0.5 then offsetX will be sprite width / 2, and so on.
*/
public get offsetX(): number {
return this._offset.x;
}
/**
* The offset on the Y axis of the origin
*/
public get offsetY(): number {
return this._offset.y;
}
/**
* Half the width of the parent sprite, taking into consideration scaling
*/
public get halfWidth(): number {
return this._halfSize.x;
}
/**
* Half the height of the parent sprite, taking into consideration scaling
*/
public get halfHeight(): number {
return this._halfSize.y;
}
/**
* The equivalent of Math.sin(rotation + rotationOffset)
*/
public get sin(): number {
return this._sc.x;
}
/**
* The equivalent of Math.cos(rotation + rotationOffset)
*/
public get cos(): number {
return this._sc.y;
}
/**
* Moves the sprite so its center is located on the given x and y coordinates.
* Doesn't change the origin of the sprite.
*/
public centerOn(x: number, y: number) {
this.parent.x = x + (this.parent.x - this.center.x);
this.parent.y = y + (this.parent.y - this.center.y);
//this.setCache();
}
/**
* Populates the transform cache. Called by the parent object on creation.
*/
public setCache() {
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.x, this.halfWidth - this._offset.y);
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._scA.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._angle);
this._scA.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._angle);
this._prevRotation = this.rotation;
if (this.parent.texture && this.parent.texture.renderRotation)
{
this._sc.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD);
this._sc.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD);
}
else
{
this._sc.x = 0;
this._sc.y = 1;
}
this.center.x = this.parent.x + this._distance * this._scA.y;
this.center.y = this.parent.y + this._distance * this._scA.x;
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;
this._flippedX = this.parent.texture.flippedX;
this._flippedY = this.parent.texture.flippedY;
}
/**
* Updates the local transform matrix and the cache values if anything has changed in the parent.
*/
public update() {
// Check cache
this._dirty = false;
// 1) Height or Width change (also triggered by a change in scale) or an Origin change
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)
{
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)));
// Store
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._dirty = true;
}
// 2) Rotation change
if (this.rotation != this._prevRotation || this._dirty)
{
this._scA.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._angle);
this._scA.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD + this._angle);
if (this.parent.texture.renderRotation)
{
this._sc.x = Math.sin((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD);
this._sc.y = Math.cos((this.rotation + this.rotationOffset) * Phaser.GameMath.DEG_TO_RAD);
}
else
{
this._sc.x = 0;
this._sc.y = 1;
}
// Store
this._prevRotation = this.rotation;
this._dirty = true;
}
// 3) If it has moved (or any of the above) then update the edges and center
if (this._dirty || this.parent.x != this._pos.x || this.parent.y != this._pos.y)
{
this.center.x = this.parent.x + this._distance * this._scA.y;
this.center.y = this.parent.y + this._distance * this._scA.x;
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;
// Translate
this.local.data[2] = this.parent.x;
this.local.data[5] = this.parent.y;
}
// Scale and Skew
if (this._dirty || this._flippedX != this.parent.texture.flippedX)
{
this._flippedX = this.parent.texture.flippedX;
if (this._flippedX)
{
this.local.data[0] = this._sc.y * -this.scale.x;
this.local.data[3] = (this._sc.x * -this.scale.x) + this.skew.x;
}
else
{
this.local.data[0] = this._sc.y * this.scale.x;
this.local.data[3] = (this._sc.x * this.scale.x) + this.skew.x;
}
}
if (this._dirty || this._flippedY != this.parent.texture.flippedY)
{
this._flippedY = this.parent.texture.flippedY;
if (this._flippedY)
{
this.local.data[4] = this._sc.y * -this.scale.y;
this.local.data[1] = -(this._sc.x * -this.scale.y) + this.skew.y;
}
else
{
this.local.data[4] = this._sc.y * this.scale.y;
this.local.data[1] = -(this._sc.x * this.scale.y) + this.skew.y;
}
}
}
}
}