2013-04-18 13:16:18 +00:00
|
|
|
/// <reference path="../Game.ts" />
|
2013-05-28 20:38:37 +00:00
|
|
|
/// <reference path="../core/Rectangle.ts" />
|
|
|
|
/// <reference path="../components/animation/AnimationManager.ts" />
|
|
|
|
/// <reference path="../components/sprite/Texture.ts" />
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
/**
|
2013-04-18 15:49:08 +00:00
|
|
|
* Phaser - Sprite
|
|
|
|
*
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
module Phaser {
|
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
export class Sprite implements IGameObject {
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
|
|
|
* Create a new <code>Sprite</code>.
|
|
|
|
*
|
|
|
|
* @param game {Phaser.Game} Current game instance.
|
2013-05-04 16:18:45 +00:00
|
|
|
* @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.
|
2013-05-28 20:38:37 +00:00
|
|
|
* @param [width] {number} The width of the object.
|
|
|
|
* @param [height] {number} The height of the object.
|
2013-05-04 12:02:12 +00:00
|
|
|
*/
|
2013-05-28 20:38:37 +00:00
|
|
|
constructor(game: Game, x?: number = 0, y?: number = 0, key?: string = null, width?: number = 16, height?: number = 16) {
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
this.game = game;
|
|
|
|
this.type = Phaser.Types.SPRITE;
|
|
|
|
this.render = game.renderer.renderSprite;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
this.exists = true;
|
|
|
|
this.active = true;
|
|
|
|
this.visible = true;
|
|
|
|
this.alive = true;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
this.frameBounds = new Rectangle(x, y, width, height);
|
|
|
|
this.origin = new Phaser.Vec2(0, 0);
|
2013-05-28 20:38:37 +00:00
|
|
|
this.scrollFactor = new Phaser.Vec2(1, 1);
|
|
|
|
this.scale = new Phaser.Vec2(1, 1);
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-29 11:24:25 +00:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.z = 0;
|
|
|
|
|
2013-05-28 20:38:37 +00:00
|
|
|
this.texture = new Phaser.Components.Texture(this, key, game.stage.canvas, game.stage.context);
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
this.width = this.frameBounds.width;
|
|
|
|
this.height = this.frameBounds.height;
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
|
2013-05-29 11:24:25 +00:00
|
|
|
/**
|
|
|
|
* Rotation angle of this object.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
private _rotation: number = 0;
|
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
2013-05-28 20:38:37 +00:00
|
|
|
* Reference to the main game object
|
2013-05-04 12:02:12 +00:00
|
|
|
*/
|
2013-05-28 20:38:37 +00:00
|
|
|
public game: Game;
|
2013-05-16 01:36:58 +00:00
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
2013-05-28 20:38:37 +00:00
|
|
|
* The type of game object.
|
2013-05-04 12:02:12 +00:00
|
|
|
*/
|
2013-05-28 20:38:37 +00:00
|
|
|
public type: number;
|
2013-04-19 17:53:21 +00:00
|
|
|
|
2013-05-18 02:05:28 +00:00
|
|
|
/**
|
2013-05-28 20:38:37 +00:00
|
|
|
* Reference to the Renderer.renderSprite method. Can be overriden by custom classes.
|
2013-05-18 02:05:28 +00:00
|
|
|
*/
|
2013-05-28 20:38:37 +00:00
|
|
|
public render;
|
2013-05-18 02:05:28 +00:00
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
2013-05-28 20:38:37 +00:00
|
|
|
* Controls if both <code>update</code> and render are called by the core game loop.
|
2013-05-04 12:02:12 +00:00
|
|
|
*/
|
2013-05-28 20:38:37 +00:00
|
|
|
public exists: bool;
|
2013-05-16 01:36:58 +00:00
|
|
|
|
|
|
|
/**
|
2013-05-28 20:38:37 +00:00
|
|
|
* Controls if <code>update()</code> is automatically called by the core game loop.
|
2013-05-16 01:36:58 +00:00
|
|
|
*/
|
2013-05-28 20:38:37 +00:00
|
|
|
public active: bool;
|
2013-05-16 01:36:58 +00:00
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
2013-05-28 20:38:37 +00:00
|
|
|
* Controls if this Sprite is rendered or skipped during the core game loop.
|
2013-05-04 12:02:12 +00:00
|
|
|
*/
|
2013-05-28 20:38:37 +00:00
|
|
|
public visible: bool;
|
2013-05-16 01:36:58 +00:00
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
2013-05-28 20:38:37 +00:00
|
|
|
*
|
2013-05-04 12:02:12 +00:00
|
|
|
*/
|
2013-05-28 20:38:37 +00:00
|
|
|
public alive: bool;
|
2013-05-16 01:36:58 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
// Getters only, don't over-write these values
|
|
|
|
public width: number;
|
|
|
|
public height: number;
|
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
2013-05-28 20:38:37 +00:00
|
|
|
* The texture used to render the Sprite.
|
2013-05-04 12:02:12 +00:00
|
|
|
*/
|
2013-05-28 20:38:37 +00:00
|
|
|
public texture: Phaser.Components.Texture;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-04 12:02:12 +00:00
|
|
|
/**
|
2013-05-28 20:38:37 +00:00
|
|
|
* The frame boundary around this Sprite.
|
|
|
|
* For non-animated sprites this matches the loaded texture dimensions.
|
|
|
|
* For animated sprites it will be updated as part of the animation loop, changing to the dimensions of the current animation frame.
|
2013-05-04 12:02:12 +00:00
|
|
|
*/
|
2013-05-28 20:38:37 +00:00
|
|
|
public frameBounds: Phaser.Rectangle;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
/**
|
|
|
|
* Scale of the Sprite. A scale of 1.0 is the original size. 0.5 half size. 2.0 double sized.
|
|
|
|
*/
|
2013-05-28 20:38:37 +00:00
|
|
|
public scale: Phaser.Vec2;
|
2013-05-29 01:58:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The influence of camera movement upon the Sprite.
|
|
|
|
*/
|
2013-05-28 20:38:37 +00:00
|
|
|
public scrollFactor: Phaser.Vec2;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
/**
|
2013-05-29 11:24:25 +00:00
|
|
|
* The Sprite origin is the point around which scale and rotation takes place.
|
2013-05-29 01:58:56 +00:00
|
|
|
*/
|
|
|
|
public origin: Phaser.Vec2;
|
|
|
|
|
2013-05-29 11:24:25 +00:00
|
|
|
/**
|
|
|
|
* 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 = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This value is added to the rotation of the Sprite.
|
|
|
|
* For example if you had a sprite graphic 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 sprite in degrees. Phaser uses a right-handed coordinate system, where 0 points to the right.
|
|
|
|
*/
|
|
|
|
public get rotation(): number {
|
|
|
|
return this._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._rotation = this.game.math.wrap(value, 360, 0);
|
|
|
|
}
|
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
/**
|
|
|
|
* Pre-update is called right before update() on each object in the game loop.
|
|
|
|
*/
|
|
|
|
public preUpdate() {
|
|
|
|
|
|
|
|
//this.last.x = this.frameBounds.x;
|
|
|
|
//this.last.y = this.frameBounds.y;
|
|
|
|
|
|
|
|
//this.collisionMask.preUpdate();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override this function to update your class's position and appearance.
|
|
|
|
*/
|
|
|
|
public update() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Automatically called after update() by the game loop.
|
|
|
|
*/
|
|
|
|
public postUpdate() {
|
|
|
|
|
|
|
|
/*
|
|
|
|
this.animations.update();
|
|
|
|
|
|
|
|
if (this.moves)
|
|
|
|
{
|
|
|
|
this.updateMotion();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.worldBounds != null)
|
|
|
|
{
|
|
|
|
if (this.outOfBoundsAction == GameObject.OUT_OF_BOUNDS_KILL)
|
|
|
|
{
|
|
|
|
if (this.x < this.worldBounds.x || this.x > this.worldBounds.right || this.y < this.worldBounds.y || this.y > this.worldBounds.bottom)
|
|
|
|
{
|
|
|
|
this.kill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this.x < this.worldBounds.x)
|
|
|
|
{
|
|
|
|
this.x = this.worldBounds.x;
|
|
|
|
}
|
|
|
|
else if (this.x > this.worldBounds.right)
|
|
|
|
{
|
|
|
|
this.x = this.worldBounds.right;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.y < this.worldBounds.y)
|
|
|
|
{
|
|
|
|
this.y = this.worldBounds.y;
|
|
|
|
}
|
|
|
|
else if (this.y > this.worldBounds.bottom)
|
|
|
|
{
|
|
|
|
this.y = this.worldBounds.bottom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.collisionMask.update();
|
|
|
|
|
|
|
|
if (this.inputEnabled)
|
|
|
|
{
|
|
|
|
this.updateInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.wasTouching = this.touching;
|
|
|
|
this.touching = Collision.NONE;
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up memory.
|
|
|
|
*/
|
|
|
|
public destroy() {
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|