2013-08-06 02:14:48 +00:00
|
|
|
/// <reference path="../Game.ts" />
|
|
|
|
/// <reference path="../gameobjects/Sprite.ts" />
|
|
|
|
/// <reference path="../loader/AnimationLoader.ts" />
|
2013-05-28 20:38:37 +00:00
|
|
|
/// <reference path="Animation.ts" />
|
|
|
|
/// <reference path="Frame.ts" />
|
|
|
|
/// <reference path="FrameData.ts" />
|
2013-04-12 16:19:56 +00:00
|
|
|
|
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-05-29 14:45:34 +00:00
|
|
|
module Phaser.Components {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 14:48:06 +00:00
|
|
|
export class AnimationManager {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* AnimationManager constructor
|
|
|
|
* Create a new <code>AnimationManager</code>.
|
|
|
|
*
|
2013-05-04 11:53:11 +00:00
|
|
|
* @param parent {Sprite} Owner sprite of this manager.
|
2013-05-03 11:22:04 +00:00
|
|
|
*/
|
2013-06-06 01:47:08 +00:00
|
|
|
constructor(parent: Phaser.Sprite) {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this._parent = parent;
|
2013-07-12 02:28:46 +00:00
|
|
|
this.game = parent.game;
|
2013-04-18 13:16:18 +00:00
|
|
|
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-05-03 11:22:04 +00:00
|
|
|
/**
|
2013-07-12 02:28:46 +00:00
|
|
|
* Reference to Phaser.Game
|
2013-05-03 11:22:04 +00:00
|
|
|
*/
|
2013-07-12 02:28:46 +00:00
|
|
|
public game: Game;
|
2013-05-17 05:49:43 +00:00
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
2013-07-12 02:28:46 +00:00
|
|
|
* Local private reference to its parent game object.
|
2013-05-03 11:22:04 +00:00
|
|
|
*/
|
2013-06-06 01:47:08 +00:00
|
|
|
private _parent: Phaser.Sprite;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Animation key-value container.
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
private _anims: {};
|
2013-05-17 05:49:43 +00:00
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Index of current frame.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
private _frameIndex: number;
|
2013-05-17 05:49:43 +00:00
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Data contains animation frames.
|
|
|
|
* @type {FrameData}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
private _frameData: FrameData = null;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
/**
|
|
|
|
* When an animation frame changes you can choose to automatically update the physics bounds of the parent Sprite
|
|
|
|
* to the width and height of the new frame. If you've set a specific physics bounds that you don't want changed during
|
|
|
|
* animation then set this to false, otherwise leave it set to true.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
public autoUpdateBounds: boolean = true;
|
2013-06-06 01:47:08 +00:00
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Keeps track of the current animation being played.
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public currentAnim: Animation;
|
2013-05-17 05:49:43 +00:00
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Keeps track of the current frame of animation.
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public currentFrame: Frame = null;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Load animation frame data.
|
|
|
|
* @param frameData Data to be loaded.
|
|
|
|
*/
|
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-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Add a new animation.
|
2013-05-04 11:53:11 +00:00
|
|
|
* @param name {string} What this animation should be called (e.g. "run").
|
|
|
|
* @param frames {any[]} An array of numbers/strings indicating what frames to play in what order (e.g. [1, 2, 3] or ['run0', 'run1', run2]).
|
|
|
|
* @param frameRate {number} The speed in frames per second that the animation should play at (e.g. 60 fps).
|
|
|
|
* @param loop {boolean} Whether or not the animation is looped or just plays once.
|
|
|
|
* @param useNumericIndex {boolean} Use number indexes instead of string indexes?
|
2013-05-18 02:05:28 +00:00
|
|
|
* @return {Animation} The Animation that was created
|
2013-05-03 11:22:04 +00:00
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
public add(name: string, frames: any[] = null, frameRate: number = 60, loop: boolean = false, useNumericIndex: boolean = true): Animation {
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
if (this._frameData == null)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-07-12 02:28:46 +00:00
|
|
|
// Create the signals the AnimationManager will emit
|
|
|
|
if (this._parent.events.onAnimationStart == null)
|
|
|
|
{
|
|
|
|
this._parent.events.onAnimationStart = new Phaser.Signal;
|
|
|
|
this._parent.events.onAnimationComplete = new Phaser.Signal;
|
|
|
|
this._parent.events.onAnimationLoop = new Phaser.Signal;
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (frames == null)
|
|
|
|
{
|
|
|
|
frames = this._frameData.getFrameIndexes();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this.validateFrames(frames, useNumericIndex) == false)
|
|
|
|
{
|
2013-04-22 00:53:24 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-07-12 02:28:46 +00:00
|
|
|
this._anims[name] = new Animation(this.game, this._parent, this._frameData, name, frames, frameRate, loop);
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
this.currentAnim = this._anims[name];
|
2013-04-23 23:47:11 +00:00
|
|
|
this.currentFrame = this.currentAnim.currentFrame;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-18 02:05:28 +00:00
|
|
|
return this._anims[name];
|
|
|
|
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-04 11:53:11 +00:00
|
|
|
/**
|
|
|
|
* Check whether the frames is valid.
|
|
|
|
* @param frames {any[]} Frames to be validated.
|
|
|
|
* @param useNumericIndex {boolean} Does these frames use number indexes or string indexes?
|
|
|
|
* @return {boolean} True if they're valid, otherwise return false.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
private validateFrames(frames: any[], useNumericIndex: boolean): boolean {
|
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-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Play animation with specific name.
|
2013-05-04 11:53:11 +00:00
|
|
|
* @param name {string} The string name of the animation you want to play.
|
|
|
|
* @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-05-03 11:22:04 +00:00
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
public play(name: string, frameRate: number = null, loop: boolean = false): Animation {
|
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
|
|
|
{
|
2013-04-20 02:40:17 +00:00
|
|
|
if (this.currentAnim == this._anims[name])
|
|
|
|
{
|
|
|
|
if (this.currentAnim.isPlaying == false)
|
|
|
|
{
|
2013-07-12 02:28:46 +00:00
|
|
|
return this.currentAnim.play(frameRate, loop);
|
2013-04-20 02:40:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.currentAnim = this._anims[name];
|
2013-07-12 02:28:46 +00:00
|
|
|
return this.currentAnim.play(frameRate, loop);
|
2013-04-20 02:40:17 +00:00
|
|
|
}
|
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-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Stop animation by name.
|
|
|
|
* Current animation will be automatically set to the stopped one.
|
|
|
|
*/
|
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-05-03 11:22:04 +00:00
|
|
|
/**
|
|
|
|
* Update animation and parent sprite's bounds.
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
public update() {
|
|
|
|
|
|
|
|
if (this.currentAnim && this.currentAnim.update() == true)
|
|
|
|
{
|
|
|
|
this.currentFrame = this.currentAnim.currentFrame;
|
2013-06-06 01:47:08 +00:00
|
|
|
this._parent.texture.width = this.currentFrame.width;
|
|
|
|
this._parent.texture.height = this.currentFrame.height;
|
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 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 {
|
2013-05-22 23:01:58 +00:00
|
|
|
|
|
|
|
if (this._frameData)
|
|
|
|
{
|
|
|
|
return this._frameData.total;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
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-07-12 02:28:46 +00:00
|
|
|
if (this._frameData && this._frameData.getFrame(value) !== null)
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-04-22 00:53:24 +00:00
|
|
|
this.currentFrame = this._frameData.getFrame(value);
|
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
this._parent.texture.width = this.currentFrame.width;
|
|
|
|
this._parent.texture.height = this.currentFrame.height;
|
|
|
|
|
|
|
|
if (this.autoUpdateBounds && this._parent['body'])
|
|
|
|
{
|
|
|
|
this._parent.body.bounds.width = this.currentFrame.width;
|
|
|
|
this._parent.body.bounds.height = this.currentFrame.height;
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
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) {
|
2013-04-15 14:42:13 +00:00
|
|
|
|
2013-08-02 17:32:26 +00:00
|
|
|
if (this._frameData && this._frameData.getFrameByName(value))
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-04-22 00:53:24 +00:00
|
|
|
this.currentFrame = this._frameData.getFrameByName(value);
|
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
this._parent.texture.width = this.currentFrame.width;
|
|
|
|
this._parent.texture.height = this.currentFrame.height;
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this._frameIndex = this.currentFrame.index;
|
|
|
|
}
|
2013-08-02 17:32:26 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Error("Cannot set frameName: " + value);
|
|
|
|
}
|
2013-04-15 14:42:13 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-22 23:01:58 +00:00
|
|
|
/**
|
|
|
|
* Removes all related references
|
|
|
|
*/
|
|
|
|
public destroy() {
|
|
|
|
|
|
|
|
this._anims = {};
|
|
|
|
this._frameData = null;
|
|
|
|
this._frameIndex = 0;
|
|
|
|
this.currentAnim = null;
|
|
|
|
this.currentFrame = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-15 14:42:13 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|