Phaser.Cache = function (game){ this.game = game; this._canvases = { } ; this._images = { } ; this._textures = { } ; this._sounds = { } ; this._text = { } ; this._json = { } ; this._physics = { } ; this._tilemaps = { } ; this._binary = { } ; this._bitmapDatas = { } ; this._bitmapFont = { } ; this.addDefaultImage(); this.addMissingImage(); this.onSoundUnlock = new Phaser.Signal(); this._cacheMap = [] ; this._cacheMap[Phaser.Cache.CANVAS] = this._canvases; this._cacheMap[Phaser.Cache.IMAGE] = this._images; this._cacheMap[Phaser.Cache.TEXTURE] = this._textures; this._cacheMap[Phaser.Cache.SOUND] = this._sounds; this._cacheMap[Phaser.Cache.TEXT] = this._text; this._cacheMap[Phaser.Cache.PHYSICS] = this._physics; this._cacheMap[Phaser.Cache.TILEMAP] = this._tilemaps; this._cacheMap[Phaser.Cache.BINARY] = this._binary; this._cacheMap[Phaser.Cache.BITMAPDATA] = this._bitmapDatas; this._cacheMap[Phaser.Cache.BITMAPFONT] = this._bitmapFont; this._cacheMap[Phaser.Cache.JSON] = this._json; } ; Phaser.Cache.CANVAS = 1; Phaser.Cache.IMAGE = 2; Phaser.Cache.TEXTURE = 3; Phaser.Cache.SOUND = 4; _AN_Write_text('TEXT', Phaser.Cache, false , 5); Phaser.Cache.PHYSICS = 6; Phaser.Cache.TILEMAP = 7; Phaser.Cache.BINARY = 8; Phaser.Cache.BITMAPDATA = 9; Phaser.Cache.BITMAPFONT = 10; Phaser.Cache.JSON = 11; Phaser.Cache.prototype = { addCanvas: function (key, canvas, context){ this._canvases[key] = { canvas: canvas, context: context} ; } , addBinary: function (key, binaryData){ this._binary[key] = binaryData; } , addBitmapData: function (key, bitmapData){ this._bitmapDatas[key] = bitmapData; return bitmapData; } , addRenderTexture: function (key, texture){ var frame = new Phaser.Frame(0, 0, 0, texture.width, texture.height, '', ''); this._textures[key] = { texture: texture, frame: frame} ; } , addSpriteSheet: function (key, url, data, frameWidth, frameHeight, frameMax, margin, spacing){ this._images[key] = { url: url, data: data, spriteSheet: true , frameWidth: frameWidth, frameHeight: frameHeight, margin: margin, spacing: spacing} ; PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data); this._images[key].frameData = Phaser.AnimationParser.spriteSheet(this.game, key, frameWidth, frameHeight, frameMax, margin, spacing); } , addTilemap: function (key, url, mapData, format){ this._tilemaps[key] = { url: url, data: mapData, format: format} ; } , addTextureAtlas: function (key, url, data, atlasData, format){ this._images[key] = { url: url, data: data, spriteSheet: true } ; PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data); if (format == Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY) { this._images[key].frameData = Phaser.AnimationParser.JSONData(this.game, atlasData, key); } else if (format == Phaser.Loader.TEXTURE_ATLAS_JSON_HASH) { this._images[key].frameData = Phaser.AnimationParser.JSONDataHash(this.game, atlasData, key); } else if (format == Phaser.Loader.TEXTURE_ATLAS_XML_STARLING) { this._images[key].frameData = Phaser.AnimationParser.XMLData(this.game, atlasData, key); } } , addBitmapFont: function (key, url, data, xmlData, xSpacing, ySpacing){ this._images[key] = { url: url, data: data, spriteSheet: true } ; PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data); Phaser.LoaderParser.bitmapFont(this.game, xmlData, key, xSpacing, ySpacing); } , addPhysicsData: function (key, url, JSONData, format){ this._physics[key] = { url: url, data: JSONData, format: format} ; } , addDefaultImage: function (){ var img = new Image(); _AN_Write_src('src', img, false , "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgAQMAAABJtOi3AAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAABVJREFUeF7NwIEAAAAAgKD9qdeocAMAoAABm3DkcAAAAABJRU5ErkJggg=="); this._images.__default = { url: null , data: img, spriteSheet: false } ; this._images.__default.frame = new Phaser.Frame(0, 0, 0, 32, 32, '', ''); PIXI.BaseTextureCache.__default = new PIXI.BaseTexture(img); PIXI.TextureCache.__default = new PIXI.Texture(PIXI.BaseTextureCache.__default); } , addMissingImage: function (){ var img = new Image(); _AN_Write_src('src', img, false , "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAJ9JREFUeNq01ssOwyAMRFG46v//Mt1ESmgh+DFmE2GPOBARKb2NVjo+17PXLD8a1+pl5+A+wSgFygymWYHBb0FtsKhJDdZlncG2IzJ4ayoMDv20wTmSMzClEgbWYNTAkQ0Z+OJ+A/eWnAaR9+oxCF4Os0H8htsMUp+pwcgBBiMNnAwF8GqIgL2hAzaGFFgZauDPKABmowZ4GL369/0rwACp2yA/ttmvsQAAAABJRU5ErkJggg=="); this._images.__missing = { url: null , data: img, spriteSheet: false } ; this._images.__missing.frame = new Phaser.Frame(0, 0, 0, 32, 32, '', ''); PIXI.BaseTextureCache.__missing = new PIXI.BaseTexture(img); PIXI.TextureCache.__missing = new PIXI.Texture(PIXI.BaseTextureCache.__missing); } , addText: function (key, url, data){ this._text[key] = { url: url, data: data} ; } , addJSON: function (key, url, data){ this._json[key] = { url: url, data: data} ; } , addImage: function (key, url, data){ this._images[key] = { url: url, data: data, spriteSheet: false } ; this._images[key].frame = new Phaser.Frame(0, 0, 0, data.width, data.height, key, this.game.rnd.uuid()); PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data); } , addSound: function (key, url, data, webAudio, audioTag){ webAudio = webAudio || true ; audioTag = audioTag || false ; var decoded = false ; if (audioTag) { decoded = true ; } this._sounds[key] = { url: url, data: data, isDecoding: false , decoded: decoded, webAudio: webAudio, audioTag: audioTag, locked: this.game.sound.touchLocked} ; } , reloadSound: function (key){ var _this = this; if (this._sounds[key]) { _AN_Write_src('src', this._sounds[key].data, false , _AN_Read_url('url', this._sounds[key])); this._sounds[key].data.addEventListener('canplaythrough', function (){ return _this.reloadSoundComplete(key); } , false ); _AN_Call_load('load', this._sounds[key].data); } } , reloadSoundComplete: function (key){ if (this._sounds[key]) { this._sounds[key].locked = false ; this.onSoundUnlock.dispatch(key); } } , updateSound: function (key, property, value){ if (this._sounds[key]) { this._sounds[key][property] = value; } } , decodedSound: function (key, data){ this._sounds[key].data = data; this._sounds[key].decoded = true ; this._sounds[key].isDecoding = false ; } , getCanvas: function (key){ if (this._canvases[key]) { return this._canvases[key].canvas; } else { console.warn('Phaser.Cache.getCanvas: Invalid key: "' + key + '"'); } } , getBitmapData: function (key){ if (this._bitmapDatas[key]) { return this._bitmapDatas[key]; } else { console.warn('Phaser.Cache.getBitmapData: Invalid key: "' + key + '"'); } } , getBitmapFont: function (key){ if (this._bitmapFont[key]) { return this._bitmapFont[key]; } else { console.warn('Phaser.Cache.getBitmapFont: Invalid key: "' + key + '"'); } } , getPhysicsData: function (key, object, fixtureKey){ if (typeof object === 'undefined' || object === null ) { if (this._physics[key]) { return this._physics[key].data; } else { console.warn('Phaser.Cache.getPhysicsData: Invalid key: "' + key + '"'); } } else { if (this._physics[key] && this._physics[key].data[object]) { var fixtures = this._physics[key].data[object]; if (fixtures && fixtureKey) { for (var fixture in fixtures){ fixture = fixtures[fixture]; if (fixture.fixtureKey === fixtureKey) { return fixture; } } console.warn('Phaser.Cache.getPhysicsData: Could not find given fixtureKey: "' + fixtureKey + ' in ' + key + '"'); } else { return fixtures; } } else { console.warn('Phaser.Cache.getPhysicsData: Invalid key/object: "' + key + ' / ' + object + '"'); } } return null ; } , checkKey: function (type, key){ if (this._cacheMap[type][key]) { return true ; } return false ; } , checkCanvasKey: function (key){ return this.checkKey(Phaser.Cache.CANVAS, key); } , checkImageKey: function (key){ return this.checkKey(Phaser.Cache.IMAGE, key); } , checkTextureKey: function (key){ return this.checkKey(Phaser.Cache.TEXTURE, key); } , checkSoundKey: function (key){ return this.checkKey(Phaser.Cache.SOUND, key); } , checkTextKey: function (key){ return this.checkKey(Phaser.Cache.TEXT, key); } , checkPhysicsKey: function (key){ return this.checkKey(Phaser.Cache.PHYSICS, key); } , checkTilemapKey: function (key){ return this.checkKey(Phaser.Cache.TILEMAP, key); } , checkBinaryKey: function (key){ return this.checkKey(Phaser.Cache.BINARY, key); } , checkBitmapDataKey: function (key){ return this.checkKey(Phaser.Cache.BITMAPDATA, key); } , checkBitmapFontKey: function (key){ return this.checkKey(Phaser.Cache.BITMAPFONT, key); } , checkJSONKey: function (key){ return this.checkKey(Phaser.Cache.JSON, key); } , getImage: function (key){ if (this._images[key]) { return this._images[key].data; } else { console.warn('Phaser.Cache.getImage: Invalid key: "' + key + '"'); } } , getTilemapData: function (key){ if (this._tilemaps[key]) { return this._tilemaps[key]; } else { console.warn('Phaser.Cache.getTilemapData: Invalid key: "' + key + '"'); } } , getFrameData: function (key){ if (this._images[key] && this._images[key].frameData) { return this._images[key].frameData; } return null ; } , updateFrameData: function (key, frameData){ if (this._images[key]) { this._images[key].spriteSheet = true ; this._images[key].frameData = frameData; } } , getFrameByIndex: function (key, frame){ if (this._images[key] && this._images[key].frameData) { return this._images[key].frameData.getFrame(frame); } return null ; } , getFrameByName: function (key, frame){ if (this._images[key] && this._images[key].frameData) { return this._images[key].frameData.getFrameByName(frame); } return null ; } , getFrame: function (key){ if (this._images[key] && this._images[key].spriteSheet === false ) { return this._images[key].frame; } return null ; } , getTextureFrame: function (key){ if (this._textures[key]) { return this._textures[key].frame; } return null ; } , getTexture: function (key){ if (this._textures[key]) { return this._textures[key]; } else { console.warn('Phaser.Cache.getTexture: Invalid key: "' + key + '"'); } } , getSound: function (key){ if (this._sounds[key]) { return this._sounds[key]; } else { console.warn('Phaser.Cache.getSound: Invalid key: "' + key + '"'); } } , getSoundData: function (key){ if (this._sounds[key]) { return this._sounds[key].data; } else { console.warn('Phaser.Cache.getSoundData: Invalid key: "' + key + '"'); } } , isSoundDecoded: function (key){ if (this._sounds[key]) { return this._sounds[key].decoded; } } , isSoundReady: function (key){ return (this._sounds[key] && this._sounds[key].decoded && this.game.sound.touchLocked === false ); } , isSpriteSheet: function (key){ if (this._images[key]) { return this._images[key].spriteSheet; } return false ; } , getText: function (key){ if (this._text[key]) { return this._text[key].data; } else { console.warn('Phaser.Cache.getText: Invalid key: "' + key + '"'); } } , getJSON: function (key){ if (this._json[key]) { return this._json[key].data; } else { console.warn('Phaser.Cache.getJSON: Invalid key: "' + key + '"'); } } , getBinary: function (key){ if (this._binary[key]) { return this._binary[key]; } else { console.warn('Phaser.Cache.getBinary: Invalid key: "' + key + '"'); } } , getKeys: function (type){ var array = null ; switch (type){ case Phaser.Cache.CANVAS: array = this._canvases; break ; case Phaser.Cache.IMAGE: array = this._images; break ; case Phaser.Cache.TEXTURE: array = this._textures; break ; case Phaser.Cache.SOUND: array = this._sounds; break ; case Phaser.Cache.TEXT: array = this._text; break ; case Phaser.Cache.PHYSICS: array = this._physics; break ; case Phaser.Cache.TILEMAP: array = this._tilemaps; break ; case Phaser.Cache.BINARY: array = this._binary; break ; case Phaser.Cache.BITMAPDATA: array = this._bitmapDatas; break ; case Phaser.Cache.BITMAPFONT: array = this._bitmapFont; break ; case Phaser.Cache.JSON: array = this._json; break ; } if (!array) { return ; } var output = [] ; for (var item in array){ if (item !== '__default' && item !== '__missing') { output.push(item); } } return output; } , removeCanvas: function (key){ delete this._canvases[key]; } , removeImage: function (key){ delete this._images[key]; } , removeSound: function (key){ delete this._sounds[key]; } , removeText: function (key){ delete this._text[key]; } , removeJSON: function (key){ delete this._json[key]; } , removePhysics: function (key){ delete this._physics[key]; } , removeTilemap: function (key){ delete this._tilemaps[key]; } , removeBinary: function (key){ delete this._binary[key]; } , removeBitmapData: function (key){ delete this._bitmapDatas[key]; } , removeBitmapFont: function (key){ delete this._bitmapFont[key]; } , destroy: function (){ for (var item in this._canvases){ delete this._canvases[item]; } for (var item in this._images){ if (item !== '__default' && item !== '__missing') { delete this._images[item]; } } for (var item in this._sounds){ delete this._sounds[item]; } for (var item in this._text){ delete this._text[item]; } for (var item in this._json){ delete this._json[item]; } for (var item in this._textures){ delete this._textures[item]; } for (var item in this._physics){ delete this._physics[item]; } for (var item in this._tilemaps){ delete this._tilemaps[item]; } for (var item in this._binary){ delete this._binary[item]; } for (var item in this._bitmapDatas){ delete this._bitmapDatas[item]; } for (var item in this._bitmapFont){ delete this._bitmapFont[item]; } } } ; Phaser.Cache.prototype.constructor = Phaser.Cache;