phaser/src/cache/CacheManager.js

200 lines
5.5 KiB
JavaScript
Raw Normal View History

var BaseCache = require('./BaseCache');
var Class = require('../utils/Class');
2018-01-16 16:30:11 +00:00
var CacheManager = new Class({
initialize:
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* The Cache Manager is the global cache owned and maintained by the Game instance.
*
* Various systems, such as the file Loader, rely on this cache in order to store the files
* it has loaded. The manager itself doesn't store any files, but instead owns multiple BaseCache
* instances, one per type of file. You can also add your own custom caches.
2017-10-04 23:09:12 +00:00
*
2018-01-16 16:30:11 +00:00
* @class CacheManager
2017-10-04 23:09:12 +00:00
* @memberOf Phaser.Cache
* @constructor
* @since 3.0.0
*
2018-01-25 03:38:23 +00:00
* @param {Phaser.Game} game - A reference to the Phaser.Game instance that owns this CacheManager.
2017-10-04 23:09:12 +00:00
*/
2018-01-16 16:30:11 +00:00
function CacheManager (game)
{
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* A reference to the Phaser.Game instance that owns this CacheManager.
2017-10-04 23:09:12 +00:00
*
* @property {Phaser.Game} game
* @protected
2018-01-25 03:38:23 +00:00
* @since 3.0.0
2017-10-04 23:09:12 +00:00
*/
this.game = game;
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* A Cache storing all binary files, typically added via the Loader.
2017-10-04 23:09:12 +00:00
*
* @property {Phaser.Cache.BaseCache} binary
* @protected
2018-01-25 03:38:23 +00:00
* @since 3.0.0
2017-10-04 23:09:12 +00:00
*/
this.binary = new BaseCache();
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* A Cache storing all bitmap font data files, typically added via the Loader.
* Only the font data is stored in this cache, the textures are part of the Texture Manager.
2017-10-04 23:09:12 +00:00
*
* @property {Phaser.Cache.BaseCache} bitmapFont
* @protected
2018-01-25 03:38:23 +00:00
* @since 3.0.0
2017-10-04 23:09:12 +00:00
*/
this.bitmapFont = new BaseCache();
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* A Cache storing all JSON data files, typically added via the Loader.
2017-10-04 23:09:12 +00:00
*
* @property {Phaser.Cache.BaseCache} json
* @protected
2018-01-25 03:38:23 +00:00
* @since 3.0.0
2017-10-04 23:09:12 +00:00
*/
2017-08-10 04:17:13 +00:00
this.json = new BaseCache();
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* A Cache storing all physics data files, typically added via the Loader.
2017-10-04 23:09:12 +00:00
*
* @property {Phaser.Cache.BaseCache} physics
* @protected
2018-01-25 03:38:23 +00:00
* @since 3.0.0
2017-10-04 23:09:12 +00:00
*/
2017-08-10 04:17:13 +00:00
this.physics = new BaseCache();
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* A Cache storing all shader source files, typically added via the Loader.
2017-10-04 23:09:12 +00:00
*
* @property {Phaser.Cache.BaseCache} shader
* @protected
2018-01-25 03:38:23 +00:00
* @since 3.0.0
2017-10-04 23:09:12 +00:00
*/
this.shader = new BaseCache();
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* A Cache storing all non-streaming audio files, typically added via the Loader.
2017-10-04 23:09:12 +00:00
*
* @property {Phaser.Cache.BaseCache} audio
2017-10-04 23:09:12 +00:00
* @protected
2018-01-25 03:38:23 +00:00
* @since 3.0.0
2017-10-04 23:09:12 +00:00
*/
this.audio = new BaseCache();
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* A Cache storing all text files, typically added via the Loader.
2017-10-04 23:09:12 +00:00
*
* @property {Phaser.Cache.BaseCache} text
* @protected
2018-01-25 03:38:23 +00:00
* @since 3.0.0
2017-10-04 23:09:12 +00:00
*/
2017-08-10 04:17:13 +00:00
this.text = new BaseCache();
2017-10-04 23:09:12 +00:00
2017-12-07 02:18:40 +00:00
/**
2018-01-25 03:38:23 +00:00
* A Cache storing all WaveFront OBJ files, typically added via the Loader.
2017-12-07 02:18:40 +00:00
*
* @property {Phaser.Cache.BaseCache} obj
* @protected
2018-01-25 03:38:23 +00:00
* @since 3.0.0
2017-12-07 02:18:40 +00:00
*/
this.obj = new BaseCache();
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* A Cache storing all tilemap data files, typically added via the Loader.
* Only the data is stored in this cache, the textures are part of the Texture Manager.
2017-10-04 23:09:12 +00:00
*
* @property {Phaser.Cache.BaseCache} tilemap
* @protected
2018-01-25 03:38:23 +00:00
* @since 3.0.0
2017-10-04 23:09:12 +00:00
*/
2017-08-10 04:17:13 +00:00
this.tilemap = new BaseCache();
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* A Cache storing all xml data files, typically added via the Loader.
2017-10-04 23:09:12 +00:00
*
* @property {Phaser.Cache.BaseCache} xml
* @protected
2018-01-25 03:38:23 +00:00
* @since 3.0.0
2017-10-04 23:09:12 +00:00
*/
2017-08-10 04:17:13 +00:00
this.xml = new BaseCache();
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* An object that contains your own custom BaseCache entries.
* Add to this via the `addCustom` method.
2017-10-04 23:09:12 +00:00
*
* @property {object.<Phaser.Cache.BaseCache>} custom
* @protected
2018-01-25 03:38:23 +00:00
* @since 3.0.0
2017-10-04 23:09:12 +00:00
*/
this.custom = {};
this.game.events.once('destroy', this.destroy, this);
},
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* Add your own custom Cache for storing your own files.
* The cache will be available under `Cache.custom.key`.
* The cache will only be created if the key is not already in use.
2017-10-04 23:09:12 +00:00
*
2018-01-16 16:30:11 +00:00
* @method Phaser.Cache.CacheManager#addCustom
2017-10-04 23:09:12 +00:00
* @since 3.0.0
*
2018-01-25 03:38:23 +00:00
* @param {string} key - The unique key of your custom cache.
*
2018-01-25 03:38:23 +00:00
* @return {Phaser.Cache.BaseCache} A reference to the BaseCache that was created. If the key was already in use, a reference to the existing cache is returned instead.
2017-10-04 23:09:12 +00:00
*/
addCustom: function (key)
{
if (!this.custom.hasOwnProperty(key))
{
this.custom[key] = new BaseCache();
2018-01-25 03:38:23 +00:00
}
return this.custom[key];
},
/**
* Removes all entries from all BaseCaches and destroys all custom caches.
*
* @method Phaser.Cache.CacheManager#destroy
* @since 3.0.0
*/
destroy: function ()
{
var keys = [
'binary',
'bitmapFont',
'json',
'physics',
'shader',
'audio',
'text',
'obj',
'tilemap',
'xml'
];
for (var i = 0; i < keys.length; i++)
{
this[keys[i]].destroy();
this[keys[i]] = null;
}
2018-01-25 03:38:23 +00:00
for (var key in this.custom)
{
this.custom[key].destroy();
}
2018-01-25 03:38:23 +00:00
this.custom = null;
this.game = null;
}
});
2018-01-16 16:30:11 +00:00
module.exports = CacheManager;