phaser/src/cache/CacheManager.js

229 lines
6.5 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2022-02-28 14:29:51 +00:00
* @copyright 2022 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
var BaseCache = require('./BaseCache');
var Class = require('../utils/Class');
2019-01-18 13:41:43 +00:00
var GameEvents = require('../core/events');
2018-02-07 15:27:21 +00:00
/**
* @classdesc
* The Cache Manager is the global cache owned and maintained by the Game instance.
2018-03-23 15:54:12 +00:00
*
2018-02-07 15:27:21 +00:00
* 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.
*
* @class CacheManager
2018-10-10 09:49:13 +00:00
* @memberof Phaser.Cache
2018-02-07 15:27:21 +00:00
* @constructor
* @since 3.0.0
*
* @param {Phaser.Game} game - A reference to the Phaser.Game instance that owns this CacheManager.
*/
2018-01-16 16:30:11 +00:00
var CacheManager = new Class({
initialize:
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
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cache.CacheManager#game
* @type {Phaser.Game}
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.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
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cache.CacheManager#binary
* @type {Phaser.Cache.BaseCache}
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
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cache.CacheManager#bitmapFont
* @type {Phaser.Cache.BaseCache}
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
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cache.CacheManager#json
* @type {Phaser.Cache.BaseCache}
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
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cache.CacheManager#physics
* @type {Phaser.Cache.BaseCache}
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
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cache.CacheManager#shader
* @type {Phaser.Cache.BaseCache}
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
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cache.CacheManager#audio
* @type {Phaser.Cache.BaseCache}
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
2019-10-03 13:13:32 +00:00
/**
* A Cache storing all non-streaming video files, typically added via the Loader.
*
* @name Phaser.Cache.CacheManager#video
* @type {Phaser.Cache.BaseCache}
* @since 3.20.0
*/
this.video = 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
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cache.CacheManager#text
* @type {Phaser.Cache.BaseCache}
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
2018-07-19 12:19:24 +00:00
/**
* A Cache storing all html files, typically added via the Loader.
*
* @name Phaser.Cache.CacheManager#html
* @type {Phaser.Cache.BaseCache}
* @since 3.12.0
*/
this.html = new BaseCache();
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
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cache.CacheManager#obj
* @type {Phaser.Cache.BaseCache}
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
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cache.CacheManager#tilemap
* @type {Phaser.Cache.BaseCache}
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
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cache.CacheManager#xml
* @type {Phaser.Cache.BaseCache}
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
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cache.CacheManager#custom
2018-03-23 15:54:12 +00:00
* @type {Object.<Phaser.Cache.BaseCache>}
2018-01-25 03:38:23 +00:00
* @since 3.0.0
2017-10-04 23:09:12 +00:00
*/
this.custom = {};
2019-01-18 13:41:43 +00:00
this.game.events.once(GameEvents.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',
2019-10-03 13:13:32 +00:00
'video',
2018-01-25 03:38:23 +00:00
'text',
2018-07-19 12:19:24 +00:00
'html',
2018-01-25 03:38:23 +00:00
'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;