Skip to content

Commit

Permalink
Add new data cache
Browse files Browse the repository at this point in the history
  • Loading branch information
samme committed May 13, 2020
1 parent 9b76df4 commit 3567a50
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Phaser.BitmapData#getImage()
- Phaser.Game#maxUpdates
- Phaser.MSPointer#pointerCancelCallback
- There is a new data cache to let you store arbitrary data throughout the game. The new methods are Phaser.Cache#addData(), Phaser.Cache#checkDataKey(), Phaser.Cache#getData(), and Phaser.Cache#removeData(). You can use them from `this.cache` in a scene or `game.cache`.

### Updates

Expand Down
59 changes: 58 additions & 1 deletion src/loader/Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ Phaser.Cache = function (game)
bitmapFont: {},
shader: {},
renderTexture: {},
compressedTexture: {}
compressedTexture: {},
data: {}
};

/**
Expand Down Expand Up @@ -113,6 +114,7 @@ Phaser.Cache = function (game)
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._cacheMap[Phaser.Cache.DATA] = this._cache.data;

/**
* @property {number}
Expand Down Expand Up @@ -221,6 +223,12 @@ Phaser.Cache.SHADER = 14;
*/
Phaser.Cache.RENDER_TEXTURE = 15;

/**
* @constant
* @type {number}
*/
Phaser.Cache.DATA = 16;

/**
* The default image used for a texture when no other is specified.
* @constant
Expand Down Expand Up @@ -848,6 +856,20 @@ Phaser.Cache.prototype = {

},

/**
* Add a data item to the cache.
*
* @method Phaser.Cache#addData
* @param {string} key - The key of the data item.
* @param {any} data - The data.
*/
addData: function (key, data)
{

this._cache.data[key] = data;

},

// //////////////////////////
// Sound Related Methods //
// //////////////////////////
Expand Down Expand Up @@ -1033,6 +1055,20 @@ Phaser.Cache.prototype = {

},

/**
* Checks if the given key exists in the Data Cache.
*
* @method Phaser.Cache#checkDataKey
* @param {string} key - The key of the data item.
* @return {boolean} True if the key exists in the cache, otherwise false.
*/
checkDataKey: function (key)
{

return this.checkKey(Phaser.Cache.DATA, key);

},

/**
* Checks if the given key exists in the Image Cache. Note that this also includes Texture Atlases, Sprite Sheets and Retro Fonts.
*
Expand Down Expand Up @@ -1645,6 +1681,20 @@ Phaser.Cache.prototype = {

},

/**
* Gets an item from the data cache.
*
* @method Phaser.Cache#getData
* @param {string} key - The key of the data item.
* @return {any} The data.
*/
getData: function (key)
{

return this.getItem(key, Phaser.Cache.DATA, 'getData');

},

// //////////////////////////
// Frame Related Methods //
// //////////////////////////
Expand Down Expand Up @@ -2146,6 +2196,13 @@ Phaser.Cache.prototype = {

},

removeData: function (key)
{

delete this._cache.data[key];

},

/**
* Empties out all of the GL Textures from Images stored in the cache.
* This is called automatically when the WebGL context is lost and then restored.
Expand Down

0 comments on commit 3567a50

Please sign in to comment.