phaser/Phaser/animation/FrameData.ts

192 lines
4.6 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 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 {Frame} The frame you want to add.
* @return {Frame} The frame you just added.
2013-05-03 11:32:39 +00:00
*/
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 {number} Index of the frame you want to get.
* @return {Frame} The frame you want.
2013-05-03 11:32:39 +00:00
*/
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 {string} Name of the frame you want to get.
* @return {Frame} The frame you want.
2013-05-03 11:32:39 +00:00
*/
2013-04-18 13:16:18 +00:00
public getFrameByName(name: string): Frame {
if (this._frameNames[name] !== '')
2013-04-18 13:16:18 +00:00
{
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 {string} Name of the frame you want to check.
2013-05-03 11:32:39 +00:00
* @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
if (this._frameNames[name] == null)
2013-04-18 13:16:18 +00:00
{
return false;
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
return true;
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 {number} Start index of frames you want.
* @param end {number} End index of frames you want.
2013-05-04 16:18:45 +00:00
* @param [output] {Frame[]} result will be added into this array.
* @return {Frame[]} Ranges of specific frames in an array.
2013-05-03 11:32:39 +00:00
*/
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.
2013-05-04 16:18:45 +00:00
* @param [output] {number[]} result will be added into this array.
* @return {number[]} Indexes of specific frames in an array.
2013-05-03 11:32:39 +00:00
*/
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 the frame indexes by giving the frame names.
2013-05-04 16:18:45 +00:00
* @param [output] {number[]} result will be added into this array.
* @return {number[]} Names of specific frames in an array.
2013-05-03 11:32:39 +00:00
*/
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 {Frame[]} All the frames in an array.
2013-05-03 11:32:39 +00:00
*/
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 {number[]} Ranges in an array.
* @return {Frame[]} All frames in an array.
2013-05-03 11:32:39 +00:00
*/
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
}
}