phaser/Phaser/system/animation/AnimationLoader.ts

101 lines
2.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
/**
2013-04-18 15:49:08 +00:00
* Phaser - AnimationLoader
*
* Responsible for parsing sprite sheet and JSON data into the internal FrameData format that Phaser uses for animations.
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 AnimationLoader {
2013-04-12 16:19:56 +00:00
2013-05-03 11:32:39 +00:00
/**
* Parse a sprite sheet from asset data.
* @param key {string} Asset key for the sprite sheet data.
* @param frameWidth {number} Width of animation frame.
* @param frameHeight {number} Height of animation frame.
* @param frameMax {number} Number of animation frames.
* @return {FrameData} Generated FrameData object.
2013-05-03 11:32:39 +00:00
*/
2013-04-18 13:16:18 +00:00
public static parseSpriteSheet(game: Game, key: string, frameWidth: number, frameHeight: number, frameMax: number): FrameData {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
// How big is our image?
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
var img = game.cache.getImage(key);
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (img == null)
{
return null;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
var width = img.width;
var height = img.height;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
var row = Math.round(width / frameWidth);
var column = Math.round(height / frameHeight);
var total = row * column;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (frameMax !== -1)
{
total = frameMax;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
// Zero or smaller than frame sizes?
if (width == 0 || height == 0 || width < frameWidth || height < frameHeight || total === 0)
{
return null;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
// Let's create some frames then
var data: FrameData = new FrameData();
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
var x = 0;
var y = 0;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
for (var i = 0; i < total; i++)
2013-04-12 16:19:56 +00:00
{
2013-04-18 13:16:18 +00:00
data.addFrame(new Frame(x, y, frameWidth, frameHeight, ''));
x += frameWidth;
if (x === width)
{
x = 0;
y += frameHeight;
}
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
return data;
2013-04-12 16:19:56 +00:00
}
2013-05-03 11:32:39 +00:00
/**
* Parse frame datas from json.
* @param json {object} Json data you want to parse.
* @return {FrameData} Generated FrameData object.
2013-05-03 11:32:39 +00:00
*/
2013-04-18 13:16:18 +00:00
public static parseJSONData(game: Game, json): FrameData {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
// Let's create some frames then
var data: FrameData = new FrameData();
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
// By this stage frames is a fully parsed array
var frames = json;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
var newFrame: Frame;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
for (var i = 0; i < frames.length; i++)
{
newFrame = data.addFrame(new Frame(frames[i].frame.x, frames[i].frame.y, frames[i].frame.w, frames[i].frame.h, frames[i].filename));
newFrame.setTrim(frames[i].trimmed, frames[i].sourceSize.w, frames[i].sourceSize.h, frames[i].spriteSourceSize.x, frames[i].spriteSourceSize.y, frames[i].spriteSourceSize.w, frames[i].spriteSourceSize.h);
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
return data;
2013-04-12 16:19:56 +00:00
}
}
2013-04-18 13:16:18 +00:00
}