2013-08-08 18:16:47 +00:00
|
|
|
/// <reference path="../_definitions.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 - Cache
|
|
|
|
*
|
|
|
|
* A game only has one instance of a Cache and it is used to store all externally loaded assets such
|
|
|
|
* as images, sounds and data files as a result of Loader calls. Cache items use string based keys for look-up.
|
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 Cache {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Cache constructor
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
constructor(game: Game) {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-08-08 03:35:13 +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-05-03 11:12:12 +00:00
|
|
|
/**
|
2013-08-08 03:35:13 +00:00
|
|
|
* Local reference to Game.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-08-08 18:16:47 +00:00
|
|
|
public game: Phaser.Game;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Canvas key-value container.
|
|
|
|
* @type {object}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
private _canvases;
|
2013-05-25 03:21:24 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Image key-value container.
|
|
|
|
* @type {object}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
private _images;
|
2013-05-25 03:21:24 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Sound key-value container.
|
|
|
|
* @type {object}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
private _sounds;
|
2013-05-25 03:21:24 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Text key-value container.
|
|
|
|
* @type {object}
|
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
private _text;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Add a new canvas.
|
2013-05-04 11:46:19 +00:00
|
|
|
* @param key {string} Asset key for this canvas.
|
|
|
|
* @param canvas {HTMLCanvasElement} Canvas DOM element.
|
|
|
|
* @param context {CanvasRenderingContext2D} Render context of this canvas.
|
2013-05-03 11:12:12 +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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Add a new sprite sheet.
|
2013-05-04 11:46:19 +00:00
|
|
|
* @param key {string} Asset key for the sprite sheet.
|
|
|
|
* @param url {string} URL of this sprite sheet file.
|
|
|
|
* @param data {object} Extra sprite sheet data.
|
|
|
|
* @param frameWidth {number} Width of the sprite sheet.
|
|
|
|
* @param frameHeight {number} Height of the sprite sheet.
|
|
|
|
* @param frameMax {number} How many frames stored in the sprite sheet.
|
2013-05-03 11:12:12 +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 };
|
2013-08-08 03:35:13 +00:00
|
|
|
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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Add a new texture atlas.
|
2013-05-04 11:46:19 +00:00
|
|
|
* @param key {string} Asset key for the texture atlas.
|
|
|
|
* @param url {string} URL of this texture atlas file.
|
|
|
|
* @param data {object} Extra texture atlas data.
|
2013-05-23 02:08:57 +00:00
|
|
|
* @param atlasData {object} Texture atlas frames data.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-06-03 11:03:34 +00:00
|
|
|
public addTextureAtlas(key: string, url: string, data, atlasData, format: 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 };
|
2013-05-23 02:08:57 +00:00
|
|
|
|
|
|
|
if (format == Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY)
|
|
|
|
{
|
2013-08-08 03:35:13 +00:00
|
|
|
this._images[key].frameData = AnimationLoader.parseJSONData(this.game, atlasData);
|
2013-05-23 02:08:57 +00:00
|
|
|
}
|
|
|
|
else if (format == Phaser.Loader.TEXTURE_ATLAS_XML_STARLING)
|
|
|
|
{
|
2013-08-08 03:35:13 +00:00
|
|
|
this._images[key].frameData = AnimationLoader.parseXMLData(this.game, atlasData, format);
|
2013-05-23 02:08:57 +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:12:12 +00:00
|
|
|
/**
|
|
|
|
* Add a new image.
|
2013-05-04 11:46:19 +00:00
|
|
|
* @param key {string} Asset key for the image.
|
|
|
|
* @param url {string} URL of this image file.
|
|
|
|
* @param data {object} Extra image data.
|
2013-05-03 11:12:12 +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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Add a new sound.
|
2013-05-04 11:46:19 +00:00
|
|
|
* @param key {string} Asset key for the sound.
|
|
|
|
* @param url {string} URL of this sound file.
|
|
|
|
* @param data {object} Extra sound data.
|
2013-05-03 11:12:12 +00:00
|
|
|
*/
|
2013-08-13 03:22:24 +00:00
|
|
|
public addSound(key: string, url: string, data, webAudio: bool = true, audioTag: bool = false) {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-08-13 03:22:24 +00:00
|
|
|
var locked: bool = this.game.sound.touchLocked;
|
|
|
|
var decoded: bool = false;
|
2013-07-16 13:45:08 +00:00
|
|
|
|
|
|
|
if (audioTag) {
|
|
|
|
decoded = true;
|
|
|
|
}
|
|
|
|
|
2013-07-16 22:32:47 +00:00
|
|
|
this._sounds[key] = { url: url, data: data, locked: locked, isDecoding: false, decoded: decoded, webAudio: webAudio, audioTag: audioTag };
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public reloadSound(key: string) {
|
|
|
|
|
|
|
|
if (this._sounds[key])
|
|
|
|
{
|
|
|
|
this._sounds[key].data.src = this._sounds[key].url;
|
|
|
|
this._sounds[key].data.addEventListener('canplaythrough', () => this.reloadSoundComplete(key), false);
|
|
|
|
this._sounds[key].data.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public onSoundUnlock: Phaser.Signal = new Phaser.Signal;
|
|
|
|
|
|
|
|
public reloadSoundComplete(key: string) {
|
|
|
|
|
|
|
|
if (this._sounds[key])
|
|
|
|
{
|
|
|
|
this._sounds[key].locked = false;
|
|
|
|
this.onSoundUnlock.dispatch(key);
|
|
|
|
}
|
2013-07-16 13:45:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public updateSound(key: string, property: string, value) {
|
|
|
|
|
|
|
|
if (this._sounds[key])
|
|
|
|
{
|
|
|
|
this._sounds[key][property] = 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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Add a new decoded sound.
|
2013-05-04 11:46:19 +00:00
|
|
|
* @param key {string} Asset key for the sound.
|
|
|
|
* @param data {object} Extra sound data.
|
2013-05-03 11:12:12 +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-07-16 13:45:08 +00:00
|
|
|
this._sounds[key].isDecoding = false;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Add a new text data.
|
2013-05-04 11:46:19 +00:00
|
|
|
* @param key {string} Asset key for the text data.
|
|
|
|
* @param url {string} URL of this text data file.
|
|
|
|
* @param data {object} Extra text data.
|
2013-05-03 11:12:12 +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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Get canvas by key.
|
|
|
|
* @param key Asset key of the canvas you want.
|
|
|
|
* @return {object} The canvas you want.
|
|
|
|
*/
|
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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Get image data by key.
|
|
|
|
* @param key Asset key of the image you want.
|
|
|
|
* @return {object} The image data you want.
|
|
|
|
*/
|
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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Get frame data by key.
|
|
|
|
* @param key Asset key of the frame data you want.
|
|
|
|
* @return {object} The frame data you want.
|
|
|
|
*/
|
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-07-16 13:45:08 +00:00
|
|
|
/**
|
|
|
|
* Get sound by key.
|
|
|
|
* @param key Asset key of the sound you want.
|
|
|
|
* @return {object} The sound you want.
|
|
|
|
*/
|
|
|
|
public getSound(key: string) {
|
|
|
|
|
|
|
|
if (this._sounds[key])
|
|
|
|
{
|
|
|
|
return this._sounds[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Get sound data by key.
|
|
|
|
* @param key Asset key of the sound you want.
|
|
|
|
* @return {object} The sound data you want.
|
|
|
|
*/
|
2013-07-16 13:45:08 +00:00
|
|
|
public getSoundData(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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Check whether an asset is decoded sound.
|
|
|
|
* @param key Asset key of the sound you want.
|
|
|
|
* @return {object} The sound data you want.
|
|
|
|
*/
|
2013-08-13 03:22:24 +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-07-16 22:32:47 +00:00
|
|
|
/**
|
|
|
|
* Check whether an asset is decoded sound.
|
|
|
|
* @param key Asset key of the sound you want.
|
|
|
|
* @return {object} The sound data you want.
|
|
|
|
*/
|
2013-08-13 03:22:24 +00:00
|
|
|
public isSoundReady(key: string): bool {
|
2013-07-16 22:32:47 +00:00
|
|
|
|
|
|
|
if (this._sounds[key] && this._sounds[key].decoded == true && this._sounds[key].locked == false)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Check whether an asset is sprite sheet.
|
|
|
|
* @param key Asset key of the sprite sheet you want.
|
|
|
|
* @return {object} The sprite sheet data you want.
|
|
|
|
*/
|
2013-08-13 03:22:24 +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-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Get text data by key.
|
|
|
|
* @param key Asset key of the text data you want.
|
|
|
|
* @return {object} The text data you want.
|
|
|
|
*/
|
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-06-13 00:55:32 +00:00
|
|
|
/**
|
|
|
|
* Returns an array containing all of the keys of Images in the Cache.
|
|
|
|
* @return {Array} The string based keys in the Cache.
|
|
|
|
*/
|
|
|
|
public getImageKeys() {
|
|
|
|
|
|
|
|
var output = [];
|
|
|
|
|
|
|
|
for (var item in this._images)
|
|
|
|
{
|
|
|
|
output.push(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array containing all of the keys of Sounds in the Cache.
|
|
|
|
* @return {Array} The string based keys in the Cache.
|
|
|
|
*/
|
|
|
|
public getSoundKeys() {
|
|
|
|
|
|
|
|
var output = [];
|
|
|
|
|
|
|
|
for (var item in this._sounds)
|
|
|
|
{
|
|
|
|
output.push(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array containing all of the keys of Text Files in the Cache.
|
|
|
|
* @return {Array} The string based keys in the Cache.
|
|
|
|
*/
|
|
|
|
public getTextKeys() {
|
|
|
|
|
|
|
|
var output = [];
|
|
|
|
|
|
|
|
for (var item in this._text)
|
|
|
|
{
|
|
|
|
output.push(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-12 02:28:46 +00:00
|
|
|
public removeCanvas(key: string) {
|
|
|
|
delete this._canvases[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
public removeImage(key: string) {
|
|
|
|
delete this._images[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
public removeSound(key: string) {
|
|
|
|
delete this._sounds[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
public removeText(key: string) {
|
|
|
|
delete this._text[key];
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:12:12 +00:00
|
|
|
/**
|
|
|
|
* Clean up cache memory.
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|