phaser/Phaser/system/animation/Animation.ts

174 lines
4.4 KiB
TypeScript
Raw Normal View History

2013-04-12 16:19:56 +00:00
/// <reference path="../../Game.ts" />
/**
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 width Width of the world bound.
* @param height Height of the world bound.
*/
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
2013-04-18 13:16:18 +00:00
private _game: Game;
private _parent: Sprite;
private _frames: number[];
private _frameData: FrameData;
private _frameIndex: number;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
private _timeLastFrame: number;
private _timeNextFrame: number;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public name: string;
public currentFrame: Frame;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public isFinished: bool;
public isPlaying: bool;
public looped: bool;
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 {
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.bounds.width = this.currentFrame.width;
this._parent.bounds.height = this.currentFrame.height;
this._frameIndex = value;
}
2013-04-12 16:19:56 +00:00
}
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;
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
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
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
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]);
}
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
}
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
private onComplete() {
this.isPlaying = false;
this.isFinished = true;
// callback
}
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
}