phaser/Phaser/animation/Animation.ts

274 lines
7 KiB
TypeScript
Raw Normal View History

2013-08-06 02:14:48 +00:00
/// <reference path="../Game.ts" />
2013-04-12 16:19:56 +00:00
/**
2013-04-18 15:49:08 +00:00
* Phaser - Animation
*
* An Animation is a single animation. It is created by the AnimationManager and belongs to Sprite objects.
2013-04-18 13:16:18 +00:00
*/
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
module Phaser {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
export class Animation {
2013-04-12 16:19:56 +00:00
2013-05-03 11:32:39 +00:00
/**
* Animation constructor
* Create a new <code>Animation</code>.
*
* @param parent {Sprite} Owner sprite of this animation.
* @param frameData {FrameData} The FrameData object contains animation data.
* @param name {string} Unique name of this animation.
* @param frames {number[]/string[]} An array of numbers or strings indicating what frames to play in what order.
* @param delay {number} Time between frames in ms.
* @param looped {boolean} Whether or not the animation is looped or just plays once.
2013-05-03 11:32:39 +00:00
*/
2013-04-18 13:16:18 +00:00
constructor(game: Game, parent: Sprite, frameData: FrameData, name: string, frames, delay: number, looped: bool) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._game = game;
this._parent = parent;
this._frames = frames;
this._frameData = frameData;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.name = name;
this.delay = 1000 / delay;
this.looped = looped;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.isFinished = false;
this.isPlaying = false;
2013-04-12 16:19:56 +00:00
this._frameIndex = 0;
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
/**
* Local private reference to game.
*/
2013-04-18 13:16:18 +00:00
private _game: Game;
/**
* Local private reference to its owner sprite.
* @type {Sprite}
*/
2013-04-18 13:16:18 +00:00
private _parent: Sprite;
/**
* Animation frame container.
* @type {number[]}
*/
2013-04-18 13:16:18 +00:00
private _frames: number[];
/**
* Frame data of this animation.(parsed from sprite sheet)
* @type {FrameData}
*/
2013-04-18 13:16:18 +00:00
private _frameData: FrameData;
/**
* Index of current frame.
* @type {number}
*/
2013-04-18 13:16:18 +00:00
private _frameIndex: number;
2013-04-12 16:19:56 +00:00
/**
* Time when switched to last frame (in ms).
* @type number
*/
2013-04-18 13:16:18 +00:00
private _timeLastFrame: number;
/**
* Time when this will switch to next frame (in ms).
* @type number
*/
2013-04-18 13:16:18 +00:00
private _timeNextFrame: number;
2013-04-12 16:19:56 +00:00
/**
* Name of this animation.
* @type {string}
*/
2013-04-18 13:16:18 +00:00
public name: string;
/**
* Currently played frame instance.
* @type {Frame}
*/
2013-04-18 13:16:18 +00:00
public currentFrame: Frame;
2013-04-12 16:19:56 +00:00
/**
* Whether or not this animation finished playing.
* @type {boolean}
*/
2013-04-18 13:16:18 +00:00
public isFinished: bool;
/**
* Whethor or not this animation is currently playing.
* @type {boolean}
*/
2013-04-18 13:16:18 +00:00
public isPlaying: bool;
/**
* Whether or not the animation is looped.
* @type {boolean}
*/
2013-04-18 13:16:18 +00:00
public looped: bool;
/**
* Time between frames in ms.
* @type {number}
*/
2013-04-18 13:16:18 +00:00
public delay: number;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public get frameTotal(): number {
return this._frames.length;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public get frame(): number {
if (this.currentFrame !== null)
{
return this.currentFrame.index;
}
else
{
return this._frameIndex;
}
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
public set frame(value: number) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.currentFrame = this._frameData.getFrame(value);
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (this.currentFrame !== null)
{
this._parent.texture.width = this.currentFrame.width;
this._parent.texture.height = this.currentFrame.height;
2013-04-18 13:16:18 +00:00
this._frameIndex = value;
}
2013-04-12 16:19:56 +00:00
}
/**
* Play this animation.
* @param frameRate {number} FrameRate you want to specify instead of using default.
* @param loop {boolean} Whether or not the animation is looped or just plays once.
*/
2013-04-18 13:16:18 +00:00
public play(frameRate?: number = null, loop?: bool) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (frameRate !== null)
{
this.delay = 1000 / frameRate;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (loop !== undefined)
{
this.looped = loop;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.isPlaying = true;
this.isFinished = false;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._timeLastFrame = this._game.time.now;
this._timeNextFrame = this._game.time.now + this.delay;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._frameIndex = 0;
2013-04-18 13:16:18 +00:00
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
2013-04-12 16:19:56 +00:00
this._parent.events.onAnimationStart.dispatch(this._parent, this);
return this;
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
/**
* Play this animation from the first frame.
*/
public restart() {
2013-04-12 16:19:56 +00:00
this.isPlaying = true;
this.isFinished = false;
this._timeLastFrame = this._game.time.now;
this._timeNextFrame = this._game.time.now + this.delay;
this._frameIndex = 0;
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
/**
* Stop playing animation and set it finished.
*/
2013-04-18 13:16:18 +00:00
public stop() {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.isPlaying = false;
this.isFinished = true;
}
2013-04-12 16:19:56 +00:00
/**
* Update animation frames.
*/
2013-04-18 13:16:18 +00:00
public update(): bool {
if (this.isPlaying == true && this._game.time.now >= this._timeNextFrame)
2013-04-12 16:19:56 +00:00
{
2013-04-18 13:16:18 +00:00
this._frameIndex++;
if (this._frameIndex == this._frames.length)
2013-04-12 16:19:56 +00:00
{
2013-04-18 13:16:18 +00:00
if (this.looped)
{
this._frameIndex = 0;
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
this._parent.events.onAnimationLoop.dispatch(this._parent, this);
2013-04-18 13:16:18 +00:00
}
else
{
this.onComplete();
}
2013-04-12 16:19:56 +00:00
}
else
{
2013-04-18 13:16:18 +00:00
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
this._timeLastFrame = this._game.time.now;
this._timeNextFrame = this._game.time.now + this.delay;
return true;
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
return false;
2013-04-12 16:19:56 +00:00
}
/**
* Clean up animation memory.
*/
2013-04-18 13:16:18 +00:00
public destroy() {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._game = null;
this._parent = null;
this._frames = null;
this._frameData = null;
this.currentFrame = null;
this.isPlaying = false;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
/**
* Animation complete callback method.
*/
private onComplete() {
this.isPlaying = false;
this.isFinished = true;
this._parent.events.onAnimationComplete.dispatch(this._parent, this);
}
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
}