phaser/Phaser/Cache.ts

173 lines
3.6 KiB
TypeScript
Raw Normal View History

2013-04-18 13:16:18 +00:00
/// <reference path="Game.ts" />
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
/**
* Phaser
*/
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 Cache {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
constructor(game: Game) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._game = game;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._canvases = {};
this._images = {};
this._sounds = {};
this._text = {};
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;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
private _canvases;
private _images;
private _sounds;
private _text;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
public addCanvas(key: string, canvas: HTMLCanvasElement, context: CanvasRenderingContext2D) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._canvases[key] = { canvas: canvas, context: context };
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 addSpriteSheet(key: string, url: string, data, frameWidth: number, frameHeight: number, frameMax: number) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._images[key] = { url: url, data: data, spriteSheet: true, frameWidth: frameWidth, frameHeight: frameHeight };
this._images[key].frameData = AnimationLoader.parseSpriteSheet(this._game, key, frameWidth, frameHeight, frameMax);
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 addTextureAtlas(key: string, url: string, data, jsonData) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._images[key] = { url: url, data: data, spriteSheet: true };
this._images[key].frameData = AnimationLoader.parseJSONData(this._game, jsonData);
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 addImage(key: string, url: string, data) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._images[key] = { url: url, data: data, spriteSheet: false };
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 addSound(key: string, url: string, data) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._sounds[key] = { url: url, data: data, decoded: false };
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 decodedSound(key: string, data) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._sounds[key].data = data;
this._sounds[key].decoded = true;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
public addText(key: string, url: string, data) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this._text[key] = { url: url, data: data };
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
public getCanvas(key: string) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (this._canvases[key])
{
return this._canvases[key].canvas;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
return null;
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
public getImage(key: string) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (this._images[key])
{
return this._images[key].data;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
return null;
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
public getFrameData(key: string): FrameData {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (this._images[key] && this._images[key].spriteSheet == true)
{
return this._images[key].frameData;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
return null;
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
public getSound(key: string) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (this._sounds[key])
{
return this._sounds[key].data;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
return null;
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
public isSoundDecoded(key: string): bool {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (this._sounds[key])
{
return this._sounds[key].decoded;
}
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
public isSpriteSheet(key: string): bool {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (this._images[key])
{
return this._images[key].spriteSheet;
}
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
public getText(key: string) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if (this._text[key])
{
return this._text[key].data;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
return null;
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
public destroy() {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
for (var item in this._canvases)
{
delete this._canvases[item['key']];
}
for (var item in this._images)
{
delete this._images[item['key']];
}
for (var item in this._sounds)
{
delete this._sounds[item['key']];
}
for (var item in this._text)
{
delete this._text[item['key']];
}
2013-04-12 16:19:56 +00:00
}
}
}