forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.js
More file actions
617 lines (476 loc) · 15.2 KB
/
Copy pathCache.js
File metadata and controls
617 lines (476 loc) · 15.2 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/**
* Cache
*
* A game only has one instance of a Cache and it is used to store all externally loaded assets such
* as images, sounds and data files as a result of Loader calls. Cache items use string based keys for look-up.
*
* @package Phaser.Cache
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
*/
Phaser.Cache = function (game) {
/**
* Local reference to Game.
*/
this.game = game;
/**
* Canvas key-value container.
* @type {object}
* @private
*/
this._canvases = {};
/**
* Image key-value container.
* @type {object}
*/
this._images = {};
/**
* RenderTexture key-value container.
* @type {object}
*/
this._textures = {};
/**
* Sound key-value container.
* @type {object}
*/
this._sounds = {};
/**
* Text key-value container.
* @type {object}
*/
this._text = {};
/**
* Tilemap key-value container.
* @type {object}
*/
this._tilemaps = {};
this.addDefaultImage();
this.onSoundUnlock = new Phaser.Signal;
};
Phaser.Cache.prototype = {
/**
* Add a new canvas.
* @param key {string} Asset key for this canvas.
* @param canvas {HTMLCanvasElement} Canvas DOM element.
* @param context {CanvasRenderingContext2D} Render context of this canvas.
*/
addCanvas: function (key, canvas, context) {
this._canvases[key] = { canvas: canvas, context: context };
},
/**
* Add a new canvas.
* @param key {string} Asset key for this canvas.
* @param canvas {RenderTexture} A RenderTexture.
*/
addRenderTexture: function (key, texture) {
var frame = new Phaser.Animation.Frame(0, 0, 0, texture.width, texture.height, '', '');
this._textures[key] = { texture: texture, frame: frame };
},
/**
* Add a new sprite sheet.
* @param key {string} Asset key for the sprite sheet.
* @param url {string} URL of this sprite sheet file.
* @param data {object} Extra sprite sheet data.
* @param frameWidth {number} Width of the sprite sheet.
* @param frameHeight {number} Height of the sprite sheet.
* @param frameMax {number} How many frames stored in the sprite sheet.
*/
addSpriteSheet: function (key, url, data, frameWidth, frameHeight, frameMax) {
this._images[key] = { url: url, data: data, spriteSheet: true, frameWidth: frameWidth, frameHeight: frameHeight };
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]);
this._images[key].frameData = Phaser.Animation.Parser.spriteSheet(this.game, key, frameWidth, frameHeight, frameMax);
},
/**
* Add a new tilemap.
* @param key {string} Asset key for the texture atlas.
* @param url {string} URL of this texture atlas file.
* @param data {object} Extra texture atlas data.
* @param atlasData {object} Texture atlas frames data.
*/
addTilemap: function (key, url, data, mapData, format) {
this._tilemaps[key] = { url: url, data: data, spriteSheet: true, mapData: mapData, format: format };
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]);
},
/**
* Add a new texture atlas.
* @param key {string} Asset key for the texture atlas.
* @param url {string} URL of this texture atlas file.
* @param data {object} Extra texture atlas data.
* @param atlasData {object} Texture atlas frames data.
*/
addTextureAtlas: function (key, url, data, atlasData, format) {
this._images[key] = { url: url, data: data, spriteSheet: true };
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]);
if (format == Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY)
{
this._images[key].frameData = Phaser.Animation.Parser.JSONData(this.game, atlasData, key);
}
else if (format == Phaser.Loader.TEXTURE_ATLAS_JSON_HASH)
{
this._images[key].frameData = Phaser.Animation.Parser.JSONDataHash(this.game, atlasData, key);
}
else if (format == Phaser.Loader.TEXTURE_ATLAS_XML_STARLING)
{
this._images[key].frameData = Phaser.Animation.Parser.XMLData(this.game, atlasData, key);
}
},
/**
* Add a new Bitmap Font.
* @param key {string} Asset key for the font texture.
* @param url {string} URL of this font xml file.
* @param data {object} Extra font data.
* @param xmlData {object} Texture atlas frames data.
*/
addBitmapFont: function (key, url, data, xmlData) {
this._images[key] = { url: url, data: data, spriteSheet: true };
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]);
Phaser.Loader.Parser.bitmapFont(this.game, xmlData, key);
// this._images[key].frameData = Phaser.Animation.Parser.XMLData(this.game, xmlData, key);
},
/**
* Adds a default image to be used when a key is wrong / missing.
* Is mapped to the key __default
*/
addDefaultImage: function () {
this._images['__default'] = { url: null, data: null, spriteSheet: false };
this._images['__default'].frame = new Phaser.Animation.Frame(0, 0, 0, 32, 32, '', '');
var base = new PIXI.BaseTexture();
base.width = 32;
base.height = 32;
base.hasLoaded = true; // avoids a hanging event listener
PIXI.BaseTextureCache['__default'] = base;
PIXI.TextureCache['__default'] = new PIXI.Texture(base);
},
/**
* Add a new image.
* @param key {string} Asset key for the image.
* @param url {string} URL of this image file.
* @param data {object} Extra image data.
*/
addImage: function (key, url, data) {
this._images[key] = { url: url, data: data, spriteSheet: false };
this._images[key].frame = new Phaser.Animation.Frame(0, 0, 0, data.width, data.height, '', '');
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]);
},
/**
* Add a new sound.
* @param key {string} Asset key for the sound.
* @param url {string} URL of this sound file.
* @param data {object} Extra sound data.
*/
addSound: function (key, url, data, webAudio, audioTag) {
webAudio = webAudio || true;
audioTag = audioTag || false;
var locked = this.game.sound.touchLocked;
var decoded = false;
if (audioTag)
{
decoded = true;
}
this._sounds[key] = { url: url, data: data, isDecoding: false, decoded: decoded, webAudio: webAudio, audioTag: audioTag };
},
reloadSound: function (key) {
var _this = this;
if (this._sounds[key])
{
this._sounds[key].data.src = this._sounds[key].url;
this._sounds[key].data.addEventListener('canplaythrough', function () {
return _this.reloadSoundComplete(key);
}, false);
this._sounds[key].data.load();
}
},
reloadSoundComplete: function (key) {
if (this._sounds[key])
{
this._sounds[key].locked = false;
this.onSoundUnlock.dispatch(key);
}
},
updateSound: function (key, property, value) {
if (this._sounds[key])
{
this._sounds[key][property] = value;
}
},
/**
* Add a new decoded sound.
* @param key {string} Asset key for the sound.
* @param data {object} Extra sound data.
*/
decodedSound: function (key, data) {
this._sounds[key].data = data;
this._sounds[key].decoded = true;
this._sounds[key].isDecoding = false;
},
/**
* Add a new text data.
* @param key {string} Asset key for the text data.
* @param url {string} URL of this text data file.
* @param data {object} Extra text data.
*/
addText: function (key, url, data) {
this._text[key] = {
url: url,
data: data
};
},
/**
* Get canvas by key.
* @param key Asset key of the canvas you want.
* @return {object} The canvas you want.
*/
getCanvas: function (key) {
if (this._canvases[key])
{
return this._canvases[key].canvas;
}
return null;
},
/**
* Checks if an image key exists.
* @param key Asset key of the image you want.
* @return {boolean} True if the key exists, otherwise false.
*/
checkImageKey: function (key) {
if (this._images[key])
{
return true;
}
return false;
},
/**
* Get image data by key.
* @param key Asset key of the image you want.
* @return {object} The image data you want.
*/
getImage: function (key) {
if (this._images[key])
{
return this._images[key].data;
}
return null;
},
/**
* Get tilemap data by key.
* @param key Asset key of the tilemap you want.
* @return {object} The tilemap data. The tileset image is in the data property, the map data in mapData.
*/
getTilemap: function (key) {
if (this._tilemaps[key])
{
return this._tilemaps[key];
}
return null;
},
/**
* Get frame data by key.
* @param key Asset key of the frame data you want.
* @return {object} The frame data you want.
*/
getFrameData: function (key) {
if (this._images[key] && this._images[key].frameData)
{
return this._images[key].frameData;
}
return null;
},
/**
* Get a single frame out of a frameData set by key.
* @param key Asset key of the frame data you want.
* @return {object} The frame data you want.
*/
getFrameByIndex: function (key, frame) {
if (this._images[key] && this._images[key].frameData)
{
return this._images[key].frameData.getFrame(frame);
}
return null;
},
/**
* Get a single frame out of a frameData set by key.
* @param key Asset key of the frame data you want.
* @return {object} The frame data you want.
*/
getFrameByName: function (key, frame) {
if (this._images[key] && this._images[key].frameData)
{
return this._images[key].frameData.getFrameByName(frame);
}
return null;
},
/**
* Get a single frame by key. You'd only do this to get the default Frame created for a non-atlas/spritesheet image.
* @param key Asset key of the frame data you want.
* @return {object} The frame data you want.
*/
getFrame: function (key) {
if (this._images[key] && this._images[key].spriteSheet == false)
{
return this._images[key].frame;
}
return null;
},
/**
* Get a single frame by key. You'd only do this to get the default Frame created for a non-atlas/spritesheet image.
* @param key Asset key of the frame data you want.
* @return {object} The frame data you want.
*/
getTextureFrame: function (key) {
if (this._textures[key])
{
return this._textures[key].frame;
}
return null;
},
/**
* Get a RenderTexture by key.
* @param key Asset key of the RenderTexture you want.
* @return {object} The RenderTexture you want.
*/
getTexture: function (key) {
if (this._textures[key])
{
return this._textures[key];
}
return null;
},
/**
* Get sound by key.
* @param key Asset key of the sound you want.
* @return {object} The sound you want.
*/
getSound: function (key) {
if (this._sounds[key])
{
return this._sounds[key];
}
return null;
},
/**
* Get sound data by key.
* @param key Asset key of the sound you want.
* @return {object} The sound data you want.
*/
getSoundData: function (key) {
if (this._sounds[key])
{
return this._sounds[key].data;
}
return null;
},
/**
* Check whether an asset is decoded sound.
* @param key Asset key of the sound you want.
* @return {object} The sound data you want.
*/
isSoundDecoded: function (key) {
if (this._sounds[key])
{
return this._sounds[key].decoded;
}
},
/**
* Check whether an asset is decoded sound.
* @param key Asset key of the sound you want.
* @return {object} The sound data you want.
*/
isSoundReady: function (key) {
return (this._sounds[key] && this._sounds[key].decoded && this.game.sound.touchLocked == false);
},
/**
* Check whether an asset is sprite sheet.
* @param key Asset key of the sprite sheet you want.
* @return {object} The sprite sheet data you want.
*/
isSpriteSheet: function (key) {
if (this._images[key])
{
return this._images[key].spriteSheet;
}
return false;
},
/**
* Get text data by key.
* @param key Asset key of the text data you want.
* @return {object} The text data you want.
*/
getText: function (key) {
if (this._text[key])
{
return this._text[key].data;
}
return null;
},
getKeys: function (array) {
var output = [];
for (var item in array)
{
if (item !== '__default')
{
output.push(item);
}
}
return output;
},
/**
* Returns an array containing all of the keys of Images in the Cache.
* @return {Array} The string based keys in the Cache.
*/
getImageKeys: function () {
return this.getKeys(this._images);
},
/**
* Returns an array containing all of the keys of Sounds in the Cache.
* @return {Array} The string based keys in the Cache.
*/
getSoundKeys: function () {
return this.getKeys(this._sounds);
},
/**
* Returns an array containing all of the keys of Text Files in the Cache.
* @return {Array} The string based keys in the Cache.
*/
getTextKeys: function () {
return this.getKeys(this._text);
},
removeCanvas: function (key) {
delete this._canvases[key];
},
removeImage: function (key) {
delete this._images[key];
},
removeSound: function (key) {
delete this._sounds[key];
},
removeText: function (key) {
delete this._text[key];
},
/**
* Clean up cache memory.
*/
destroy: function () {
for (var item in this._canvases)
{
delete this._canvases[item['key']];
}
for (var item in this._images)
{
delete this._images[item['key']];
}
for (var item in this._sounds)
{
delete this._sounds[item['key']];
}
for (var item in this._text)
{
delete this._text[item['key']];
}
}
};