phaser/Phaser/AnimationManager.ts

186 lines
4.9 KiB
TypeScript
Raw Normal View History

2013-04-12 16:19:56 +00:00
/// <reference path="Game.ts" />
2013-04-18 13:16:18 +00:00
/// <reference path="gameobjects/Sprite.ts" />
2013-04-12 16:19:56 +00:00
/// <reference path="system/animation/Animation.ts" />
/// <reference path="system/animation/AnimationLoader.ts" />
/// <reference path="system/animation/Frame.ts" />
/// <reference path="system/animation/FrameData.ts" />
2013-04-18 15:49:08 +00:00
/**
* Phaser - AnimationManager
*
* Any Sprite that has animation contains an instance of the AnimationManager, which is used to add, play and update
* sprite specific animations.
*/
2013-04-18 13:16:18 +00:00
module Phaser {
2013-04-12 16:19:56 +00:00
export class AnimationManager {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
constructor(game: Game, parent: Sprite) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._game = game;
this._parent = parent;
this._anims = {};
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
private _game: Game;
private _parent: Sprite;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
private _anims: {};
private _frameIndex: number;
private _frameData: FrameData = null;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public currentAnim: Animation;
public currentFrame: Frame = null;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public loadFrameData(frameData: FrameData) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._frameData = frameData;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this.frame = 0;
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
public add(name: string, frames: any[] = null, frameRate: number = 60, loop: bool = false, useNumericIndex: bool = true) {
if (this._frameData == null)
2013-04-12 16:19:56 +00:00
{
return;
}
2013-04-18 13:16:18 +00:00
if (frames == null)
{
frames = this._frameData.getFrameIndexes();
}
else
{
if (this.validateFrames(frames, useNumericIndex) == false)
{
throw Error('Invalid frames given to Animation ' + name);
2013-04-18 13:16:18 +00:00
return;
}
}
if (useNumericIndex == false)
{
frames = this._frameData.getFrameIndexesByName(frames);
}
this._anims[name] = new Animation(this._game, this._parent, this._frameData, name, frames, frameRate, loop);
this.currentAnim = this._anims[name];
this.currentFrame = this.currentAnim.currentFrame;
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 validateFrames(frames: any[], useNumericIndex: bool): bool {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
for (var i = 0; i < frames.length; i++)
{
if (useNumericIndex == true)
{
if (frames[i] > this._frameData.total)
{
return false;
}
}
else
{
if (this._frameData.checkFrameName(frames[i]) == false)
{
return false;
}
}
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
return true;
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 play(name: string, frameRate?: number = null, loop?: bool) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (this._anims[name])
2013-04-12 16:19:56 +00:00
{
if (this.currentAnim == this._anims[name])
{
if (this.currentAnim.isPlaying == false)
{
this.currentAnim.play(frameRate, loop);
}
}
else
{
this.currentAnim = this._anims[name];
this.currentAnim.play(frameRate, loop);
}
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(name: string) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (this._anims[name])
{
this.currentAnim = this._anims[name];
this.currentAnim.stop();
}
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
public update() {
if (this.currentAnim && this.currentAnim.update() == true)
{
this.currentFrame = this.currentAnim.currentFrame;
this._parent.bounds.width = this.currentFrame.width;
this._parent.bounds.height = this.currentFrame.height;
}
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
public get frameData(): FrameData {
return this._frameData;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public get frameTotal(): number {
return this._frameData.total;
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
if (this._frameData.getFrame(value) !== null)
2013-04-18 13:16:18 +00:00
{
this.currentFrame = this._frameData.getFrame(value);
2013-04-18 13:16:18 +00:00
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
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public get frameName(): string {
return this.currentFrame.name;
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
public set frameName(value: string) {
if (this._frameData.getFrameByName(value) !== null)
2013-04-18 13:16:18 +00:00
{
this.currentFrame = this._frameData.getFrameByName(value);
2013-04-18 13:16:18 +00:00
this._parent.bounds.width = this.currentFrame.width;
this._parent.bounds.height = this.currentFrame.height;
this._frameIndex = this.currentFrame.index;
}
}
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}