55*/
66
77/**
8- * A game only has one instance of a Cache and it is used to store all externally loaded assets such as images, sounds
8+ * A game only has one instance of a Cache and it is used to store all externally loaded assets such as images, sounds
99* and data files as a result of Loader calls. Cached items use string based keys for look-up.
10- *
10+ *
1111* @class Phaser.Cache
1212* @constructor
1313* @param {Phaser.Game } game - A reference to the currently running game.
@@ -91,6 +91,24 @@ Phaser.Cache = function (game) {
9191 */
9292 this . _bitmapFont = { } ;
9393
94+ /**
95+ * @property {object } _urlMap - Maps URLs to resources.
96+ * @private
97+ */
98+ this . _urlMap = { } ;
99+
100+ /**
101+ * @property {Image } _urlResolver - Used to resolve URLs to the absolute path.
102+ * @private
103+ */
104+ this . _urlResolver = new Image ( ) ;
105+
106+ /**
107+ * @property {string } _urlTemp - Temporary variable to hold a resolved url.
108+ * @private
109+ */
110+ this . _urlTemp = null ;
111+
94112 this . addDefaultImage ( ) ;
95113 this . addMissingImage ( ) ;
96114
@@ -276,6 +294,8 @@ Phaser.Cache.prototype = {
276294
277295 this . _images [ key ] . frameData = Phaser . AnimationParser . spriteSheet ( this . game , key , frameWidth , frameHeight , frameMax , margin , spacing ) ;
278296
297+ this . _urlMap [ this . _resolveUrl ( url ) ] = this . _images [ key ] ;
298+
279299 } ,
280300
281301 /**
@@ -291,6 +311,8 @@ Phaser.Cache.prototype = {
291311
292312 this . _tilemaps [ key ] = { url : url , data : mapData , format : format } ;
293313
314+ this . _urlMap [ this . _resolveUrl ( url ) ] = this . _tilemaps [ key ] ;
315+
294316 } ,
295317
296318 /**
@@ -323,6 +345,8 @@ Phaser.Cache.prototype = {
323345 this . _images [ key ] . frameData = Phaser . AnimationParser . XMLData ( this . game , atlasData , key ) ;
324346 }
325347
348+ this . _urlMap [ this . _resolveUrl ( url ) ] = this . _images [ key ] ;
349+
326350 } ,
327351
328352 /**
@@ -347,6 +371,8 @@ Phaser.Cache.prototype = {
347371
348372 this . _bitmapFont [ key ] = PIXI . BitmapText . fonts [ key ] ;
349373
374+ this . _urlMap [ this . _resolveUrl ( url ) ] = this . _bitmapFont [ key ] ;
375+
350376 } ,
351377
352378 /**
@@ -362,6 +388,8 @@ Phaser.Cache.prototype = {
362388
363389 this . _physics [ key ] = { url : url , data : JSONData , format : format } ;
364390
391+ this . _urlMap [ this . _resolveUrl ( url ) ] = this . _physics [ key ] ;
392+
365393 } ,
366394
367395 /**
@@ -418,6 +446,8 @@ Phaser.Cache.prototype = {
418446
419447 this . _text [ key ] = { url : url , data : data } ;
420448
449+ this . _urlMap [ this . _resolveUrl ( url ) ] = this . _text [ key ] ;
450+
421451 } ,
422452
423453 /**
@@ -432,6 +462,8 @@ Phaser.Cache.prototype = {
432462
433463 this . _json [ key ] = { url : url , data : data } ;
434464
465+ this . _urlMap [ this . _resolveUrl ( url ) ] = this . _json [ key ] ;
466+
435467 } ,
436468
437469 /**
@@ -467,6 +499,8 @@ Phaser.Cache.prototype = {
467499 PIXI . BaseTextureCache [ key ] = new PIXI . BaseTexture ( data ) ;
468500 PIXI . TextureCache [ key ] = new PIXI . Texture ( PIXI . BaseTextureCache [ key ] ) ;
469501
502+ this . _urlMap [ this . _resolveUrl ( url ) ] = this . _images [ key ] ;
503+
470504 } ,
471505
472506 /**
@@ -493,6 +527,8 @@ Phaser.Cache.prototype = {
493527
494528 this . _sounds [ key ] = { url : url , data : data , isDecoding : false , decoded : decoded , webAudio : webAudio , audioTag : audioTag , locked : this . game . sound . touchLocked } ;
495529
530+ this . _urlMap [ this . _resolveUrl ( url ) ] = this . _sounds [ key ] ;
531+
496532 } ,
497533
498534 /**
@@ -667,7 +703,7 @@ Phaser.Cache.prototype = {
667703 }
668704
669705 }
670-
706+
671707 // We did not find the requested fixture
672708 console . warn ( 'Phaser.Cache.getPhysicsData: Could not find given fixtureKey: "' + fixtureKey + ' in ' + key + '"' ) ;
673709 }
@@ -861,6 +897,24 @@ Phaser.Cache.prototype = {
861897
862898 } ,
863899
900+ /**
901+ * Checks if the given URL has been loaded into the Cache.
902+ *
903+ * @method Phaser.Cache#checkUrl
904+ * @param {string } url - The url to check for in the cache.
905+ * @return {boolean } True if the url exists, otherwise false.
906+ */
907+ checkUrl : function ( url ) {
908+
909+ if ( this . _urlMap [ this . _resolveUrl ( url ) ] )
910+ {
911+ return true ;
912+ }
913+
914+ return false ;
915+
916+ } ,
917+
864918 /**
865919 * Get image data by key.
866920 *
@@ -1193,6 +1247,26 @@ Phaser.Cache.prototype = {
11931247
11941248 } ,
11951249
1250+ /**
1251+ * Get a cached object by the URL.
1252+ *
1253+ * @method Phaser.Cache#getUrl
1254+ * @param {string } url - The url for the object loaded to get from the cache.
1255+ * @return {object } The cached object.
1256+ */
1257+ getUrl : function ( url ) {
1258+
1259+ if ( this . _urlMap [ this . _resolveUrl ( url ) ] )
1260+ {
1261+ return this . _urlMap [ this . _resolveUrl ( url ) ] ;
1262+ }
1263+ else
1264+ {
1265+ console . warn ( 'Phaser.Cache.getUrl: Invalid url: "' + url + '"' ) ;
1266+ }
1267+
1268+ } ,
1269+
11961270 /**
11971271 * Gets all keys used by the Cache for the given data type.
11981272 *
@@ -1394,6 +1468,24 @@ Phaser.Cache.prototype = {
13941468 delete this . _bitmapFont [ key ] ;
13951469 } ,
13961470
1471+ /**
1472+ * Resolves a url its absolute form.
1473+ *
1474+ * @method Phaser.Cache#_resolveUrl
1475+ * @param {string } url - The url to resolve.
1476+ * @private
1477+ */
1478+ _resolveUrl : function ( url ) {
1479+ this . _urlResolver . src = this . game . load . baseUrl + url ;
1480+
1481+ this . _urlTemp = this . _urlResolver . src ;
1482+
1483+ // ensure no request is actually made
1484+ this . _urlResolver . src = '' ;
1485+
1486+ return this . _urlTemp ;
1487+ } ,
1488+
13971489 /**
13981490 * Clears the cache. Removes every local cache object reference.
13991491 *
@@ -1464,6 +1556,10 @@ Phaser.Cache.prototype = {
14641556 delete this . _bitmapFont [ item ] ;
14651557 }
14661558
1559+ this . _urlMap = null ;
1560+ this . _urlResolver = null ;
1561+ this . _urlTemp = null ;
1562+
14671563 }
14681564
14691565} ;
0 commit comments