phaser/Phaser/system/animation/FrameData.ts

190 lines
4.5 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
/**
2013-04-18 15:49:08 +00:00
* Phaser - FrameData
*
* FrameData is a container for Frame objects, the internal representation of animation data in Phaser.
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 FrameData {
2013-04-12 16:19:56 +00:00
2013-05-03 11:32:39 +00:00
/**
* FrameData constructor
*/
2013-04-18 13:16:18 +00:00
constructor() {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._frames = [];
this._frameNames = [];
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-05-03 11:32:39 +00:00
/**
* Local frame container.
*/
2013-04-18 13:16:18 +00:00
private _frames: Frame[];
2013-05-03 11:32:39 +00:00
/**
* Local frameName<->index container.
*/
2013-04-18 13:16:18 +00:00
private _frameNames;
public get total(): number {
return this._frames.length;
}
2013-05-03 11:32:39 +00:00
/**
* Add a new frame.
* @param frame The frame you want to add.
* @return {Frame=} The frame you just added.
*/
2013-04-18 13:16:18 +00:00
public addFrame(frame: Frame): Frame {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
frame.index = this._frames.length;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._frames.push(frame);
2013-04-18 13:16:18 +00:00
if (frame.name !== '')
{
this._frameNames[frame.name] = frame.index;
}
return frame;
2013-04-12 16:19:56 +00:00
}
2013-05-03 11:32:39 +00:00
/**
* Get a frame by its index.
* @param index Index of the frame you want to get.
* @return {Frame=} The frame you want.
*/
2013-04-18 13:16:18 +00:00
public getFrame(index: number): Frame {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (this._frames[index])
{
return this._frames[index];
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
return null;
}
2013-05-03 11:32:39 +00:00
/**
* Get a frame by its name.
* @param name Name of the frame you want to get.
* @return {Frame=} The frame you want.
*/
2013-04-18 13:16:18 +00:00
public getFrameByName(name: string): Frame {
2013-04-18 13:16:18 +00:00
if (this._frameNames[name] >= 0)
{
return this._frames[this._frameNames[name]];
}
2013-04-18 13:16:18 +00:00
return null;
2013-04-12 16:19:56 +00:00
}
2013-05-03 11:32:39 +00:00
/**
* Check whether there's a frame with given name.
* @param name Name of the frame you want to check.
* @return {boolean} True if frame with given name found, otherwise return false.
*/
2013-04-18 13:16:18 +00:00
public checkFrameName(name: string): bool {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (this._frameNames[name] >= 0)
{
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-05-03 11:32:39 +00:00
/**
* Get ranges of frames in an array.
* @param start Start index of frames you want.
* @param end End index of frames you want.
* @param output Optional, result will be added into this array.
* @return {array} Ranges of specific frames in an array.
*/
2013-04-18 13:16:18 +00:00
public getFrameRange(start: number, end: number, output?: Frame[] = []): Frame[] {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
for (var i = start; i <= end; i++)
{
output.push(this._frames[i]);
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
return output;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-05-03 11:32:39 +00:00
/**
* Get all indexes of frames by giving their name.
* @param output Optional, result will be added into this array.
* @return {array} Indexes of specific frames in an array.
*/
2013-04-18 13:16:18 +00:00
public getFrameIndexes(output?: number[] = []): number[] {
output.length = 0;
for (var i = 0; i < this._frames.length; i++)
{
output.push(i);
}
return output;
2013-04-12 16:19:56 +00:00
}
2013-05-03 11:32:39 +00:00
/**
* Get all names of frames by giving their indexes.
* @param output Optional, result will be added into this array.
* @return {array} Names of specific frames in an array.
*/
2013-04-18 13:16:18 +00:00
public getFrameIndexesByName(input: string[]): number[] {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
var output: number[] = [];
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
for (var i = 0; i < input.length; i++)
{
if (this.getFrameByName(input[i]))
{
output.push(this.getFrameByName(input[i]).index);
}
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
return output;
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:32:39 +00:00
/**
* Get all frames in this frame data.
* @return {array} All the frames in an array.
*/
2013-04-18 13:16:18 +00:00
public getAllFrames(): Frame[] {
return this._frames;
2013-04-12 16:19:56 +00:00
}
2013-05-03 11:32:39 +00:00
/**
* Get All frames with specific ranges.
* @param range Ranges in an array.
* @return All frames in an array.
*/
2013-04-18 13:16:18 +00:00
public getFrames(range: number[]) {
var output: Frame[] = [];
for (var i = 0; i < range.length; i++)
{
output.push(this._frames[i]);
}
return output;
}
2013-04-12 16:19:56 +00:00
}
}