forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheManager.js
More file actions
218 lines (193 loc) · 6.11 KB
/
CacheManager.js
File metadata and controls
218 lines (193 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var BaseCache = require('./BaseCache');
var Class = require('../utils/Class');
/**
* @classdesc
* 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.
*
* @class CacheManager
* @memberOf Phaser.Cache
* @constructor
* @since 3.0.0
*
* @param {Phaser.Game} game - A reference to the Phaser.Game instance that owns this CacheManager.
*/
var CacheManager = new Class({
initialize:
function CacheManager (game)
{
/**
* A reference to the Phaser.Game instance that owns this CacheManager.
*
* @name Phaser.Cache.CacheManager#game
* @type {Phaser.Game}
* @protected
* @since 3.0.0
*/
this.game = game;
/**
* A Cache storing all binary files, typically added via the Loader.
*
* @name Phaser.Cache.CacheManager#binary
* @type {Phaser.Cache.BaseCache}
* @protected
* @since 3.0.0
*/
this.binary = new BaseCache();
/**
* 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.
*
* @name Phaser.Cache.CacheManager#bitmapFont
* @type {Phaser.Cache.BaseCache}
* @protected
* @since 3.0.0
*/
this.bitmapFont = new BaseCache();
/**
* A Cache storing all JSON data files, typically added via the Loader.
*
* @name Phaser.Cache.CacheManager#json
* @type {Phaser.Cache.BaseCache}
* @protected
* @since 3.0.0
*/
this.json = new BaseCache();
/**
* A Cache storing all physics data files, typically added via the Loader.
*
* @name Phaser.Cache.CacheManager#physics
* @type {Phaser.Cache.BaseCache}
* @protected
* @since 3.0.0
*/
this.physics = new BaseCache();
/**
* A Cache storing all shader source files, typically added via the Loader.
*
* @name Phaser.Cache.CacheManager#shader
* @type {Phaser.Cache.BaseCache}
* @protected
* @since 3.0.0
*/
this.shader = new BaseCache();
/**
* A Cache storing all non-streaming audio files, typically added via the Loader.
*
* @name Phaser.Cache.CacheManager#audio
* @type {Phaser.Cache.BaseCache}
* @protected
* @since 3.0.0
*/
this.audio = new BaseCache();
/**
* A Cache storing all text files, typically added via the Loader.
*
* @name Phaser.Cache.CacheManager#text
* @type {Phaser.Cache.BaseCache}
* @protected
* @since 3.0.0
*/
this.text = new BaseCache();
/**
* A Cache storing all WaveFront OBJ files, typically added via the Loader.
*
* @name Phaser.Cache.CacheManager#obj
* @type {Phaser.Cache.BaseCache}
* @protected
* @since 3.0.0
*/
this.obj = new BaseCache();
/**
* 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.
*
* @name Phaser.Cache.CacheManager#tilemap
* @type {Phaser.Cache.BaseCache}
* @protected
* @since 3.0.0
*/
this.tilemap = new BaseCache();
/**
* A Cache storing all xml data files, typically added via the Loader.
*
* @name Phaser.Cache.CacheManager#xml
* @type {Phaser.Cache.BaseCache}
* @protected
* @since 3.0.0
*/
this.xml = new BaseCache();
/**
* An object that contains your own custom BaseCache entries.
* Add to this via the `addCustom` method.
*
* @name Phaser.Cache.CacheManager#custom
* @type {object.<Phaser.Cache.BaseCache>}
* @protected
* @since 3.0.0
*/
this.custom = {};
this.game.events.once('destroy', this.destroy, this);
},
/**
* 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.
*
* @method Phaser.Cache.CacheManager#addCustom
* @since 3.0.0
*
* @param {string} key - The unique key of your custom cache.
*
* @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.
*/
addCustom: function (key)
{
if (!this.custom.hasOwnProperty(key))
{
this.custom[key] = new BaseCache();
}
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;
}
for (var key in this.custom)
{
this.custom[key].destroy();
}
this.custom = null;
this.game = null;
}
});
module.exports = CacheManager;