Phaser.Cache = function (game){ this.game = game; this.autoResolveURL = false ; this._cache = { canvas: { } , image: { } , texture: { } , sound: { } , video: { } , text: { } , json: { } , xml: { } , physics: { } , tilemap: { } , binary: { } , bitmapData: { } , bitmapFont: { } , shader: { } , renderTexture: { } } ; this._urlMap = { } ; this._urlResolver = new Image(); this._urlTemp = null ; this.onSoundUnlock = new Phaser.Signal(); this._cacheMap = [] ; this._cacheMap[Phaser.Cache.CANVAS] = this._cache.canvas; this._cacheMap[Phaser.Cache.IMAGE] = this._cache.image; this._cacheMap[Phaser.Cache.TEXTURE] = this._cache.texture; this._cacheMap[Phaser.Cache.SOUND] = this._cache.sound; this._cacheMap[Phaser.Cache.TEXT] = this._cache.text; this._cacheMap[Phaser.Cache.PHYSICS] = this._cache.physics; this._cacheMap[Phaser.Cache.TILEMAP] = this._cache.tilemap; this._cacheMap[Phaser.Cache.BINARY] = this._cache.binary; this._cacheMap[Phaser.Cache.BITMAPDATA] = this._cache.bitmapData; this._cacheMap[Phaser.Cache.BITMAPFONT] = this._cache.bitmapFont; this._cacheMap[Phaser.Cache.JSON] = this._cache.json; this._cacheMap[Phaser.Cache.XML] = this._cache.xml; this._cacheMap[Phaser.Cache.VIDEO] = this._cache.video; this._cacheMap[Phaser.Cache.SHADER] = this._cache.shader; this._cacheMap[Phaser.Cache.RENDER_TEXTURE] = this._cache.renderTexture; this.addDefaultImage(); this.addMissingImage(); } ; 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.XML = 12; Phaser.Cache.VIDEO = 13; Phaser.Cache.SHADER = 14; Phaser.Cache.RENDER_TEXTURE = 15; Phaser.Cache.prototype = { addCanvas: function (key, canvas, context){ if (context === undefined) { context = canvas.getContext('2d'); } this._cache.canvas[key] = { canvas: canvas, context: context} ; } , addImage: function (key, url, data){ if (this.checkImageKey(key)) { this.removeImage(key); } var img = { key: key, url: url, data: data, base: new PIXI.BaseTexture(data), frame: new Phaser.Frame(0, 0, 0, data.width, data.height, key), frameData: new Phaser.FrameData()} ; img.frameData.addFrame(new Phaser.Frame(0, 0, 0, data.width, data.height, url)); this._cache.image[key] = img; this._resolveURL(url, img); return img; } , addDefaultImage: function (){ var img = new Image(); _AN_Write_src('src', img, false , "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgAQMAAABJtOi3AAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAABVJREFUeF7NwIEAAAAAgKD9qdeocAMAoAABm3DkcAAAAABJRU5ErkJggg=="); var obj = this.addImage('__default', null , img); obj.base.skipRender = true ; PIXI.TextureCache.__default = new PIXI.Texture(obj.base); } , 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=="); var obj = this.addImage('__missing', null , img); PIXI.TextureCache.__missing = new PIXI.Texture(obj.base); } , addSound: function (key, url, data, webAudio, audioTag){ if (webAudio === undefined) { webAudio = true ; audioTag = false ; } if (audioTag === undefined) { webAudio = false ; audioTag = true ; } var decoded = false ; if (audioTag) { decoded = true ; } this._cache.sound[key] = { url: url, data: data, isDecoding: false , decoded: decoded, webAudio: webAudio, audioTag: audioTag, locked: this.game.sound.touchLocked} ; this._resolveURL(url, this._cache.sound[key]); } , addText: function (key, url, data){ this._cache.text[key] = { url: url, data: data} ; this._resolveURL(url, this._cache.text[key]); } , addPhysicsData: function (key, url, JSONData, format){ this._cache.physics[key] = { url: url, data: JSONData, format: format} ; this._resolveURL(url, this._cache.physics[key]); } , addTilemap: function (key, url, mapData, format){ this._cache.tilemap[key] = { url: url, data: mapData, format: format} ; this._resolveURL(url, this._cache.tilemap[key]); } , addBinary: function (key, binaryData){ this._cache.binary[key] = binaryData; } , addBitmapData: function (key, bitmapData, frameData){ bitmapData.key = key; if (frameData === undefined) { frameData = new Phaser.FrameData(); frameData.addFrame(bitmapData.textureFrame); } this._cache.bitmapData[key] = { data: bitmapData, frameData: frameData} ; return bitmapData; } , addBitmapFont: function (key, url, data, atlasData, atlasType, xSpacing, ySpacing){ var obj = { url: url, data: data, font: null , base: new PIXI.BaseTexture(data)} ; if (xSpacing === undefined) { xSpacing = 0; } if (ySpacing === undefined) { ySpacing = 0; } if (atlasType === 'json') { obj.font = Phaser.LoaderParser.jsonBitmapFont(atlasData, obj.base, xSpacing, ySpacing); } else { obj.font = Phaser.LoaderParser.xmlBitmapFont(atlasData, obj.base, xSpacing, ySpacing); } this._cache.bitmapFont[key] = obj; this._resolveURL(url, obj); } , addJSON: function (key, url, data){ this._cache.json[key] = { url: url, data: data} ; this._resolveURL(url, this._cache.json[key]); } , addXML: function (key, url, data){ this._cache.xml[key] = { url: url, data: data} ; this._resolveURL(url, this._cache.xml[key]); } , addVideo: function (key, url, data, isBlob){ this._cache.video[key] = { url: url, data: data, isBlob: isBlob, locked: true } ; this._resolveURL(url, this._cache.video[key]); } , addShader: function (key, url, data){ this._cache.shader[key] = { url: url, data: data} ; this._resolveURL(url, this._cache.shader[key]); } , addRenderTexture: function (key, texture){ this._cache.renderTexture[key] = { texture: texture, frame: new Phaser.Frame(0, 0, 0, texture.width, texture.height, '', '')} ; } , addSpriteSheet: function (key, url, data, frameWidth, frameHeight, frameMax, margin, spacing){ if (frameMax === undefined) { frameMax = -1; } if (margin === undefined) { margin = 0; } if (spacing === undefined) { spacing = 0; } var obj = { key: key, url: url, data: data, frameWidth: frameWidth, frameHeight: frameHeight, margin: margin, spacing: spacing, base: new PIXI.BaseTexture(data), frameData: Phaser.AnimationParser.spriteSheet(this.game, data, frameWidth, frameHeight, frameMax, margin, spacing)} ; this._cache.image[key] = obj; this._resolveURL(url, obj); } , addTextureAtlas: function (key, url, data, atlasData, format){ var obj = { key: key, url: url, data: data, base: new PIXI.BaseTexture(data)} ; if (format === Phaser.Loader.TEXTURE_ATLAS_XML_STARLING) { obj.frameData = Phaser.AnimationParser.XMLData(this.game, atlasData, key); } else if (format === Phaser.Loader.TEXTURE_ATLAS_JSON_PYXEL) { obj.frameData = Phaser.AnimationParser.JSONDataPyxel(this.game, atlasData, key); } else { if (Array.isArray(atlasData.frames)) { obj.frameData = Phaser.AnimationParser.JSONData(this.game, atlasData, key); } else { obj.frameData = Phaser.AnimationParser.JSONDataHash(this.game, atlasData, key); } } this._cache.image[key] = obj; this._resolveURL(url, obj); } , reloadSound: function (key){ var _this = this; var sound = this.getSound(key); if (sound) { _AN_Write_src('src', sound.data, false , _AN_Read_url('url', sound)); sound.data.addEventListener('canplaythrough', function (){ return _this.reloadSoundComplete(key); } , false ); _AN_Call_load('load', sound.data); } } , reloadSoundComplete: function (key){ var sound = this.getSound(key); if (sound) { sound.locked = false ; this.onSoundUnlock.dispatch(key); } } , updateSound: function (key, property, value){ var sound = this.getSound(key); if (sound) { sound[property] = value; } } , decodedSound: function (key, data){ var sound = this.getSound(key); sound.data = data; sound.decoded = true ; sound.isDecoding = false ; } , isSoundDecoded: function (key){ var sound = _AN_Call_getitem('getItem', this, key, Phaser.Cache.SOUND, 'isSoundDecoded'); if (sound) { return sound.decoded; } } , isSoundReady: function (key){ var sound = _AN_Call_getitem('getItem', this, key, Phaser.Cache.SOUND, 'isSoundDecoded'); if (sound) { return (sound.decoded && !this.game.sound.touchLocked); } } , checkKey: function (cache, key){ if (this._cacheMap[cache][key]) { return true ; } return false ; } , checkURL: function (url){ if (this._urlMap[this._resolveURL(url)]) { 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); } , checkXMLKey: function (key){ return this.checkKey(Phaser.Cache.XML, key); } , checkVideoKey: function (key){ return this.checkKey(Phaser.Cache.VIDEO, key); } , checkShaderKey: function (key){ return this.checkKey(Phaser.Cache.SHADER, key); } , checkRenderTextureKey: function (key){ return this.checkKey(Phaser.Cache.RENDER_TEXTURE, key); } , getItem: function (key, cache, method, property){ if (!this.checkKey(cache, key)) { if (method) { console.warn('Phaser.Cache.' + method + ': Key "' + key + '" not found in Cache.'); } } else { if (property === undefined) { return this._cacheMap[cache][key]; } else { return this._cacheMap[cache][key][property]; } } return null ; } , getCanvas: function (key){ return _AN_Call_getitem('getItem', this, key, Phaser.Cache.CANVAS, 'getCanvas', 'canvas'); } , getImage: function (key, full){ if (key === undefined || key === null ) { key = '__default'; } if (full === undefined) { full = false ; } var img = _AN_Call_getitem('getItem', this, key, Phaser.Cache.IMAGE, 'getImage'); if (img === null ) { img = _AN_Call_getitem('getItem', this, '__missing', Phaser.Cache.IMAGE, 'getImage'); } if (full) { return img; } else { return img.data; } } , getTextureFrame: function (key){ return _AN_Call_getitem('getItem', this, key, Phaser.Cache.TEXTURE, 'getTextureFrame', 'frame'); } , getSound: function (key){ return _AN_Call_getitem('getItem', this, key, Phaser.Cache.SOUND, 'getSound'); } , getSoundData: function (key){ return _AN_Call_getitem('getItem', this, key, Phaser.Cache.SOUND, 'getSoundData', 'data'); } , getText: function (key){ return _AN_Call_getitem('getItem', this, key, Phaser.Cache.TEXT, 'getText', 'data'); } , getPhysicsData: function (key, object, fixtureKey){ var data = _AN_Call_getitem('getItem', this, key, Phaser.Cache.PHYSICS, 'getPhysicsData', 'data'); if (data === null || object === undefined || object === null ) { return data; } else { if (data[object]) { var fixtures = 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 ; } , getTilemapData: function (key){ return _AN_Call_getitem('getItem', this, key, Phaser.Cache.TILEMAP, 'getTilemapData'); } , getBinary: function (key){ return _AN_Call_getitem('getItem', this, key, Phaser.Cache.BINARY, 'getBinary'); } , getBitmapData: function (key){ return _AN_Call_getitem('getItem', this, key, Phaser.Cache.BITMAPDATA, 'getBitmapData', 'data'); } , getBitmapFont: function (key){ return _AN_Call_getitem('getItem', this, key, Phaser.Cache.BITMAPFONT, 'getBitmapFont'); } , getJSON: function (key, clone){ var data = _AN_Call_getitem('getItem', this, key, Phaser.Cache.JSON, 'getJSON', 'data'); if (data) { if (clone) { return Phaser.Utils.extend(true , data); } else { return data; } } else { return null ; } } , getXML: function (key){ return _AN_Call_getitem('getItem', this, key, Phaser.Cache.XML, 'getXML', 'data'); } , getVideo: function (key){ return _AN_Call_getitem('getItem', this, key, Phaser.Cache.VIDEO, 'getVideo'); } , getShader: function (key){ return _AN_Call_getitem('getItem', this, key, Phaser.Cache.SHADER, 'getShader', 'data'); } , getRenderTexture: function (key){ return _AN_Call_getitem('getItem', this, key, Phaser.Cache.RENDER_TEXTURE, 'getRenderTexture'); } , getBaseTexture: function (key, cache){ if (cache === undefined) { cache = Phaser.Cache.IMAGE; } return _AN_Call_getitem('getItem', this, key, cache, 'getBaseTexture', 'base'); } , getFrame: function (key, cache){ if (cache === undefined) { cache = Phaser.Cache.IMAGE; } return _AN_Call_getitem('getItem', this, key, cache, 'getFrame', 'frame'); } , getFrameCount: function (key, cache){ var data = this.getFrameData(key, cache); if (data) { return data.total; } else { return 0; } } , getFrameData: function (key, cache){ if (cache === undefined) { cache = Phaser.Cache.IMAGE; } return _AN_Call_getitem('getItem', this, key, cache, 'getFrameData', 'frameData'); } , hasFrameData: function (key, cache){ if (cache === undefined) { cache = Phaser.Cache.IMAGE; } return (_AN_Call_getitem('getItem', this, key, cache, '', 'frameData') !== null ); } , updateFrameData: function (key, frameData, cache){ if (cache === undefined) { cache = Phaser.Cache.IMAGE; } if (this._cacheMap[cache][key]) { this._cacheMap[cache][key].frameData = frameData; } } , getFrameByIndex: function (key, index, cache){ var data = this.getFrameData(key, cache); if (data) { return data.getFrame(index); } else { return null ; } } , getFrameByName: function (key, name, cache){ var data = this.getFrameData(key, cache); if (data) { return data.getFrameByName(name); } else { return null ; } } , getPixiTexture: function (key){ if (PIXI.TextureCache[key]) { return PIXI.TextureCache[key]; } else { var base = this.getPixiBaseTexture(key); if (base) { return new PIXI.Texture(base); } else { return null ; } } } , getPixiBaseTexture: function (key){ if (PIXI.BaseTextureCache[key]) { return PIXI.BaseTextureCache[key]; } else { var img = _AN_Call_getitem('getItem', this, key, Phaser.Cache.IMAGE, 'getPixiBaseTexture'); if (img !== null ) { return img.base; } else { return null ; } } } , getURL: function (url){ var url = this._resolveURL(url); if (url) { return this._urlMap[url]; } else { console.warn('Phaser.Cache.getUrl: Invalid url: "' + url + '" or Cache.autoResolveURL was false'); return null ; } } , getKeys: function (cache){ if (cache === undefined) { cache = Phaser.Cache.IMAGE; } var out = [] ; if (this._cacheMap[cache]) { for (var key in this._cacheMap[cache]){ if (key !== '__default' && key !== '__missing') { out.push(key); } } } return out; } , removeCanvas: function (key){ delete this._cache.canvas[key]; } , removeImage: function (key, removeFromPixi){ if (removeFromPixi === undefined) { removeFromPixi = true ; } var img = this.getImage(key, true ); if (removeFromPixi && img.base) { img.base.destroy(); } delete this._cache.image[key]; } , removeSound: function (key){ delete this._cache.sound[key]; } , removeText: function (key){ delete this._cache.text[key]; } , removePhysics: function (key){ delete this._cache.physics[key]; } , removeTilemap: function (key){ delete this._cache.tilemap[key]; } , removeBinary: function (key){ delete this._cache.binary[key]; } , removeBitmapData: function (key){ delete this._cache.bitmapData[key]; } , removeBitmapFont: function (key){ delete this._cache.bitmapFont[key]; } , removeJSON: function (key){ delete this._cache.json[key]; } , removeXML: function (key){ delete this._cache.xml[key]; } , removeVideo: function (key){ delete this._cache.video[key]; } , removeShader: function (key){ delete this._cache.shader[key]; } , removeRenderTexture: function (key){ delete this._cache.renderTexture[key]; } , removeSpriteSheet: function (key){ delete this._cache.spriteSheet[key]; } , removeTextureAtlas: function (key){ delete this._cache.atlas[key]; } , clearGLTextures: function (){ for (var key in this._cache.image){ this._cache.image[key].base._glTextures = [] ; } } , _resolveURL: function (url, data){ if (!this.autoResolveURL) { return null ; } _AN_Write_src('src', this._urlResolver, false , _AN_Read_baseurl('baseURL', this.game.load) + url); this._urlTemp = _AN_Read_src('src', this._urlResolver); _AN_Write_src('src', this._urlResolver, false , ''); if (data) { this._urlMap[this._urlTemp] = data; } return this._urlTemp; } , destroy: function (){ for (var i = 0; i < _AN_Read_length('length', this._cacheMap); i++ ){ var cache = this._cacheMap[i]; for (var key in cache){ if (key !== '__default' && key !== '__missing') { if (cache[key].destroy) { cache[key].destroy(); } delete cache[key]; } } } this._urlMap = null ; this._urlResolver = null ; this._urlTemp = null ; } } ; Phaser.Cache.prototype.constructor = Phaser.Cache;