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
2021 lines (1634 loc) · 61.2 KB
/
Copy pathCache.js
File metadata and controls
2021 lines (1634 loc) · 61.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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2015 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Phaser has one single cache in which it stores all assets.
*
* The cache is split up into sections, such as images, sounds, video, json, etc. All assets are stored using
* a unique string-based key as their identifier. Assets stored in different areas of the cache can have the
* same key, for example 'playerWalking' could be used as the key for both a sprite sheet and an audio file,
* because they are unique data types.
*
* The cache is automatically populated by the Phaser.Loader. When you use the loader to pull in external assets
* such as images they are automatically placed into their respective cache. Most common Game Objects, such as
* Sprites and Videos automatically query the cache to extract the assets they need on instantiation.
*
* You can access the cache from within a State via `this.cache`. From here you can call any public method it has,
* including adding new entries to it, deleting them or querying them.
*
* Understand that almost without exception when you get an item from the cache it will return a reference to the
* item stored in the cache, not a copy of it. Therefore if you retrieve an item and then modify it, the original
* object in the cache will also be updated, even if you don't put it back into the cache again.
*
* By default when you change State the cache is _not_ cleared, although there is an option to clear it should
* your game require it. In a typical game set-up the cache is populated once after the main game has loaded and
* then used as an asset store.
*
* @class Phaser.Cache
* @constructor
* @param {Phaser.Game} game - A reference to the currently running game.
*/
Phaser.Cache = function (game) {
/**
* @property {Phaser.Game} game - Local reference to game.
*/
this.game = game;
/**
* Automatically resolve resource URLs to absolute paths for use with the Cache.getURL method.
* @property {boolean} autoResolveURL
*/
this.autoResolveURL = false;
/**
* The main cache object into which all resources are placed.
* @property {object} _cache
* @private
*/
this._cache = {
canvas: {},
image: {},
texture: {},
sound: {},
video: {},
text: {},
json: {},
xml: {},
physics: {},
tilemap: {},
binary: {},
bitmapData: {},
bitmapFont: {},
shader: {},
renderTexture: {}
};
/**
* @property {object} _urlMap - Maps URLs to resources.
* @private
*/
this._urlMap = {};
/**
* @property {Image} _urlResolver - Used to resolve URLs to the absolute path.
* @private
*/
this._urlResolver = new Image();
/**
* @property {string} _urlTemp - Temporary variable to hold a resolved url.
* @private
*/
this._urlTemp = null;
/**
* @property {Phaser.Signal} onSoundUnlock - This event is dispatched when the sound system is unlocked via a touch event on cellular devices.
*/
this.onSoundUnlock = new Phaser.Signal();
/**
* @property {array} _cacheMap - Const to cache object look-up array.
* @private
*/
this._cacheMap = [];
this._cacheMap[Phaser.Cache.CANVAS] = this._cache.canvas;
this._cacheMap[Phaser.Cache.IMAGE] = this._cache.image;
this._cacheMap[Phaser.Cache.TEXTURE] = this._cache.texture;
this._cacheMap[Phaser.Cache.SOUND] = this._cache.sound;
this._cacheMap[Phaser.Cache.TEXT] = this._cache.text;
this._cacheMap[Phaser.Cache.PHYSICS] = this._cache.physics;
this._cacheMap[Phaser.Cache.TILEMAP] = this._cache.tilemap;
this._cacheMap[Phaser.Cache.BINARY] = this._cache.binary;
this._cacheMap[Phaser.Cache.BITMAPDATA] = this._cache.bitmapData;
this._cacheMap[Phaser.Cache.BITMAPFONT] = this._cache.bitmapFont;
this._cacheMap[Phaser.Cache.JSON] = this._cache.json;
this._cacheMap[Phaser.Cache.XML] = this._cache.xml;
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.addDefaultImage();
this.addMissingImage();
};
/**
* @constant
* @type {number}
*/
Phaser.Cache.CANVAS = 1;
/**
* @constant
* @type {number}
*/
Phaser.Cache.IMAGE = 2;
/**
* @constant
* @type {number}
*/
Phaser.Cache.TEXTURE = 3;
/**
* @constant
* @type {number}
*/
Phaser.Cache.SOUND = 4;
/**
* @constant
* @type {number}
*/
Phaser.Cache.TEXT = 5;
/**
* @constant
* @type {number}
*/
Phaser.Cache.PHYSICS = 6;
/**
* @constant
* @type {number}
*/
Phaser.Cache.TILEMAP = 7;
/**
* @constant
* @type {number}
*/
Phaser.Cache.BINARY = 8;
/**
* @constant
* @type {number}
*/
Phaser.Cache.BITMAPDATA = 9;
/**
* @constant
* @type {number}
*/
Phaser.Cache.BITMAPFONT = 10;
/**
* @constant
* @type {number}
*/
Phaser.Cache.JSON = 11;
/**
* @constant
* @type {number}
*/
Phaser.Cache.XML = 12;
/**
* @constant
* @type {number}
*/
Phaser.Cache.VIDEO = 13;
/**
* @constant
* @type {number}
*/
Phaser.Cache.SHADER = 14;
/**
* @constant
* @type {number}
*/
Phaser.Cache.RENDER_TEXTURE = 15;
Phaser.Cache.prototype = {
//////////////////
// Add Methods //
//////////////////
/**
* Add a new canvas object in to the cache.
*
* @method Phaser.Cache#addCanvas
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {HTMLCanvasElement} canvas - The Canvas DOM element.
* @param {CanvasRenderingContext2D} [context] - The context of the canvas element. If not specified it will default go `getContext('2d')`.
*/
addCanvas: function (key, canvas, context) {
if (context === undefined) { context = canvas.getContext('2d'); }
this._cache.canvas[key] = { canvas: canvas, context: context };
},
/**
* Adds an Image file into the Cache. The file must have already been loaded, typically via Phaser.Loader, but can also have been loaded into the DOM.
* If an image already exists in the cache with the same key then it is removed and destroyed, and the new image inserted in its place.
*
* @method Phaser.Cache#addImage
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {string} url - The URL the asset was loaded from. If the asset was not loaded externally set to `null`.
* @param {object} data - Extra image data.
* @return {object} The full image object that was added to the cache.
*/
addImage: function (key, url, data) {
if (this.checkImageKey(key))
{
this.removeImage(key);
}
var img = {
key: key,
url: url,
data: data,
base: new PIXI.BaseTexture(data),
frame: new Phaser.Frame(0, 0, 0, data.width, data.height, key),
frameData: new Phaser.FrameData()
};
img.frameData.addFrame(new Phaser.Frame(0, 0, 0, data.width, data.height, url));
this._cache.image[key] = img;
this._resolveURL(url, img);
return img;
},
/**
* Adds a default image to be used in special cases such as WebGL Filters.
* It uses the special reserved key of `__default`.
* This method is called automatically when the Cache is created.
* This image is skipped when `Cache.destroy` is called due to its internal requirements.
*
* @method Phaser.Cache#addDefaultImage
* @protected
*/
addDefaultImage: function () {
var img = new Image();
img.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgAQMAAABJtOi3AAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAABVJREFUeF7NwIEAAAAAgKD9qdeocAMAoAABm3DkcAAAAABJRU5ErkJggg==";
var obj = this.addImage('__default', null, img);
// Because we don't want to invalidate the sprite batch for an invisible texture
obj.base.skipRender = true;
PIXI.TextureCache['__default'] = new PIXI.Texture(obj.base);
},
/**
* Adds an image to be used when a key is wrong / missing.
* It uses the special reserved key of `__missing`.
* This method is called automatically when the Cache is created.
* This image is skipped when `Cache.destroy` is called due to its internal requirements.
*
* @method Phaser.Cache#addMissingImage
* @protected
*/
addMissingImage: function () {
var img = new Image();
img.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAJ9JREFUeNq01ssOwyAMRFG46v//Mt1ESmgh+DFmE2GPOBARKb2NVjo+17PXLD8a1+pl5+A+wSgFygymWYHBb0FtsKhJDdZlncG2IzJ4ayoMDv20wTmSMzClEgbWYNTAkQ0Z+OJ+A/eWnAaR9+oxCF4Os0H8htsMUp+pwcgBBiMNnAwF8GqIgL2hAzaGFFgZauDPKABmowZ4GL369/0rwACp2yA/ttmvsQAAAABJRU5ErkJggg==";
var obj = this.addImage('__missing', null, img);
PIXI.TextureCache['__missing'] = new PIXI.Texture(obj.base);
},
/**
* Adds a Sound file into the Cache. The file must have already been loaded, typically via Phaser.Loader.
*
* @method Phaser.Cache#addSound
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {string} url - The URL the asset was loaded from. If the asset was not loaded externally set to `null`.
* @param {object} data - Extra sound data.
* @param {boolean} webAudio - True if the file is using web audio.
* @param {boolean} audioTag - True if the file is using legacy HTML audio.
*/
addSound: function (key, url, data, webAudio, audioTag) {
if (webAudio === undefined) { webAudio = true; audioTag = false; }
if (audioTag === undefined) { webAudio = false; audioTag = true; }
var decoded = false;
if (audioTag)
{
decoded = true;
}
this._cache.sound[key] = {
url: url,
data: data,
isDecoding: false,
decoded: decoded,
webAudio: webAudio,
audioTag: audioTag,
locked: this.game.sound.touchLocked
};
this._resolveURL(url, this._cache.sound[key]);
},
/**
* Add a new text data.
*
* @method Phaser.Cache#addText
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {string} url - The URL the asset was loaded from. If the asset was not loaded externally set to `null`.
* @param {object} data - Extra text data.
*/
addText: function (key, url, data) {
this._cache.text[key] = { url: url, data: data };
this._resolveURL(url, this._cache.text[key]);
},
/**
* Add a new physics data object to the Cache.
*
* @method Phaser.Cache#addPhysicsData
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {string} url - The URL the asset was loaded from. If the asset was not loaded externally set to `null`.
* @param {object} JSONData - The physics data object (a JSON file).
* @param {number} format - The format of the physics data.
*/
addPhysicsData: function (key, url, JSONData, format) {
this._cache.physics[key] = { url: url, data: JSONData, format: format };
this._resolveURL(url, this._cache.physics[key]);
},
/**
* Add a new tilemap to the Cache.
*
* @method Phaser.Cache#addTilemap
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {string} url - The URL the asset was loaded from. If the asset was not loaded externally set to `null`.
* @param {object} mapData - The tilemap data object (either a CSV or JSON file).
* @param {number} format - The format of the tilemap data.
*/
addTilemap: function (key, url, mapData, format) {
this._cache.tilemap[key] = { url: url, data: mapData, format: format };
this._resolveURL(url, this._cache.tilemap[key]);
},
/**
* Add a binary object in to the cache.
*
* @method Phaser.Cache#addBinary
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {object} binaryData - The binary object to be added to the cache.
*/
addBinary: function (key, binaryData) {
this._cache.binary[key] = binaryData;
},
/**
* Add a BitmapData object to the cache.
*
* @method Phaser.Cache#addBitmapData
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {Phaser.BitmapData} bitmapData - The BitmapData object to be addded to the cache.
* @param {Phaser.FrameData|null} [frameData=(auto create)] - Optional FrameData set associated with the given BitmapData. If not specified (or `undefined`) a new FrameData object is created containing the Bitmap's Frame. If `null` is supplied then no FrameData will be created.
* @return {Phaser.BitmapData} The BitmapData object to be addded to the cache.
*/
addBitmapData: function (key, bitmapData, frameData) {
bitmapData.key = key;
if (frameData === undefined)
{
frameData = new Phaser.FrameData();
frameData.addFrame(bitmapData.textureFrame);
}
this._cache.bitmapData[key] = { data: bitmapData, frameData: frameData };
return bitmapData;
},
/**
* Add a new Bitmap Font to the Cache.
*
* @method Phaser.Cache#addBitmapFont
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {string} url - The URL the asset was loaded from. If the asset was not loaded externally set to `null`.
* @param {object} data - Extra font data.
* @param {object} atlasData - Texture atlas frames data.
* @param {string} [atlasType='xml'] - The format of the texture atlas ( 'json' or 'xml' ).
* @param {number} [xSpacing=0] - If you'd like to add additional horizontal spacing between the characters then set the pixel value here.
* @param {number} [ySpacing=0] - If you'd like to add additional vertical spacing between the lines then set the pixel value here.
*/
addBitmapFont: function (key, url, data, atlasData, atlasType, xSpacing, ySpacing) {
var obj = {
url: url,
data: data,
font: null,
base: new PIXI.BaseTexture(data)
};
if (xSpacing === undefined) { xSpacing = 0; }
if (ySpacing === undefined) { ySpacing = 0; }
if (atlasType === 'json')
{
obj.font = Phaser.LoaderParser.jsonBitmapFont(atlasData, obj.base, xSpacing, ySpacing);
}
else
{
obj.font = Phaser.LoaderParser.xmlBitmapFont(atlasData, obj.base, xSpacing, ySpacing);
}
this._cache.bitmapFont[key] = obj;
this._resolveURL(url, obj);
},
/**
* Add a new json object into the cache.
*
* @method Phaser.Cache#addJSON
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {string} url - The URL the asset was loaded from. If the asset was not loaded externally set to `null`.
* @param {object} data - Extra json data.
*/
addJSON: function (key, url, data) {
this._cache.json[key] = { url: url, data: data };
this._resolveURL(url, this._cache.json[key]);
},
/**
* Add a new xml object into the cache.
*
* @method Phaser.Cache#addXML
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {string} url - The URL the asset was loaded from. If the asset was not loaded externally set to `null`.
* @param {object} data - Extra text data.
*/
addXML: function (key, url, data) {
this._cache.xml[key] = { url: url, data: data };
this._resolveURL(url, this._cache.xml[key]);
},
/**
* Adds a Video file into the Cache. The file must have already been loaded, typically via Phaser.Loader.
*
* @method Phaser.Cache#addVideo
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {string} url - The URL the asset was loaded from. If the asset was not loaded externally set to `null`.
* @param {object} data - Extra video data.
* @param {boolean} isBlob - True if the file was preloaded via xhr and the data parameter is a Blob. false if a Video tag was created instead.
*/
addVideo: function (key, url, data, isBlob) {
this._cache.video[key] = { url: url, data: data, isBlob: isBlob, locked: true };
this._resolveURL(url, this._cache.video[key]);
},
/**
* Adds a Fragment Shader in to the Cache. The file must have already been loaded, typically via Phaser.Loader.
*
* @method Phaser.Cache#addShader
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {string} url - The URL the asset was loaded from. If the asset was not loaded externally set to `null`.
* @param {object} data - Extra shader data.
*/
addShader: function (key, url, data) {
this._cache.shader[key] = { url: url, data: data };
this._resolveURL(url, this._cache.shader[key]);
},
/**
* Add a new Phaser.RenderTexture in to the cache.
*
* @method Phaser.Cache#addRenderTexture
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {Phaser.RenderTexture} texture - The texture to use as the base of the RenderTexture.
*/
addRenderTexture: function (key, texture) {
this._cache.renderTexture[key] = { texture: texture, frame: new Phaser.Frame(0, 0, 0, texture.width, texture.height, '', '') };
},
/**
* Add a new sprite sheet in to the cache.
*
* @method Phaser.Cache#addSpriteSheet
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {string} url - The URL the asset was loaded from. If the asset was not loaded externally set to `null`.
* @param {object} data - Extra sprite sheet data.
* @param {number} frameWidth - Width of the sprite sheet.
* @param {number} frameHeight - Height of the sprite sheet.
* @param {number} [frameMax=-1] - How many frames stored in the sprite sheet. If -1 then it divides the whole sheet evenly.
* @param {number} [margin=0] - If the frames have been drawn with a margin, specify the amount here.
* @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
*/
addSpriteSheet: function (key, url, data, frameWidth, frameHeight, frameMax, margin, spacing) {
if (frameMax === undefined) { frameMax = -1; }
if (margin === undefined) { margin = 0; }
if (spacing === undefined) { spacing = 0; }
var obj = {
key: key,
url: url,
data: data,
frameWidth: frameWidth,
frameHeight: frameHeight,
margin: margin,
spacing: spacing,
base: new PIXI.BaseTexture(data),
frameData: Phaser.AnimationParser.spriteSheet(this.game, data, frameWidth, frameHeight, frameMax, margin, spacing)
};
this._cache.image[key] = obj;
this._resolveURL(url, obj);
},
/**
* Add a new texture atlas to the Cache.
*
* @method Phaser.Cache#addTextureAtlas
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {string} url - The URL the asset was loaded from. If the asset was not loaded externally set to `null`.
* @param {object} data - Extra texture atlas data.
* @param {object} atlasData - Texture atlas frames data.
* @param {number} format - The format of the texture atlas.
*/
addTextureAtlas: function (key, url, data, atlasData, format) {
var obj = {
key: key,
url: url,
data: data,
base: new PIXI.BaseTexture(data)
};
if (format === Phaser.Loader.TEXTURE_ATLAS_XML_STARLING)
{
obj.frameData = Phaser.AnimationParser.XMLData(this.game, atlasData, key);
}
else if (format === Phaser.Loader.TEXTURE_ATLAS_JSON_PYXEL)
{
obj.frameData = Phaser.AnimationParser.JSONDataPyxel(this.game, atlasData, key);
}
else
{
// Let's just work it out from the frames array
if (Array.isArray(atlasData.frames))
{
obj.frameData = Phaser.AnimationParser.JSONData(this.game, atlasData, key);
}
else
{
obj.frameData = Phaser.AnimationParser.JSONDataHash(this.game, atlasData, key);
}
}
this._cache.image[key] = obj;
this._resolveURL(url, obj);
},
////////////////////////////
// Sound Related Methods //
////////////////////////////
/**
* Reload a Sound file from the server.
*
* @method Phaser.Cache#reloadSound
* @param {string} key - The key of the asset within the cache.
*/
reloadSound: function (key) {
var _this = this;
var sound = this.getSound(key);
if (sound)
{
sound.data.src = sound.url;
sound.data.addEventListener('canplaythrough', function () {
return _this.reloadSoundComplete(key);
}, false);
sound.data.load();
}
},
/**
* Fires the onSoundUnlock event when the sound has completed reloading.
*
* @method Phaser.Cache#reloadSoundComplete
* @param {string} key - The key of the asset within the cache.
*/
reloadSoundComplete: function (key) {
var sound = this.getSound(key);
if (sound)
{
sound.locked = false;
this.onSoundUnlock.dispatch(key);
}
},
/**
* Updates the sound object in the cache.
*
* @method Phaser.Cache#updateSound
* @param {string} key - The key of the asset within the cache.
*/
updateSound: function (key, property, value) {
var sound = this.getSound(key);
if (sound)
{
sound[property] = value;
}
},
/**
* Add a new decoded sound.
*
* @method Phaser.Cache#decodedSound
* @param {string} key - The key of the asset within the cache.
* @param {object} data - Extra sound data.
*/
decodedSound: function (key, data) {
var sound = this.getSound(key);
sound.data = data;
sound.decoded = true;
sound.isDecoding = false;
},
/**
* Check if the given sound has finished decoding.
*
* @method Phaser.Cache#isSoundDecoded
* @param {string} key - The key of the asset within the cache.
* @return {boolean} The decoded state of the Sound object.
*/
isSoundDecoded: function (key) {
var sound = this.getItem(key, Phaser.Cache.SOUND, 'isSoundDecoded');
if (sound)
{
return sound.decoded;
}
},
/**
* Check if the given sound is ready for playback.
* A sound is considered ready when it has finished decoding and the device is no longer touch locked.
*
* @method Phaser.Cache#isSoundReady
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the sound is decoded and the device is not touch locked.
*/
isSoundReady: function (key) {
var sound = this.getItem(key, Phaser.Cache.SOUND, 'isSoundDecoded');
if (sound)
{
return (sound.decoded && !this.game.sound.touchLocked);
}
},
////////////////////////
// Check Key Methods //
////////////////////////
/**
* Checks if a key for the given cache object type exists.
*
* @method Phaser.Cache#checkKey
* @param {integer} cache - The cache to search. One of the Cache consts such as `Phaser.Cache.IMAGE` or `Phaser.Cache.SOUND`.
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the key exists, otherwise false.
*/
checkKey: function (cache, key) {
if (this._cacheMap[cache][key])
{
return true;
}
return false;
},
/**
* Checks if the given URL has been loaded into the Cache.
* This method will only work if Cache.autoResolveURL was set to `true` before any preloading took place.
* The method will make a DOM src call to the URL given, so please be aware of this for certain file types, such as Sound files on Firefox
* which may cause double-load instances.
*
* @method Phaser.Cache#checkURL
* @param {string} url - The url to check for in the cache.
* @return {boolean} True if the url exists, otherwise false.
*/
checkURL: function (url) {
if (this._urlMap[this._resolveURL(url)])
{
return true;
}
return false;
},
/**
* Checks if the given key exists in the Canvas Cache.
*
* @method Phaser.Cache#checkCanvasKey
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the key exists in the cache, otherwise false.
*/
checkCanvasKey: function (key) {
return this.checkKey(Phaser.Cache.CANVAS, key);
},
/**
* Checks if the given key exists in the Image Cache. Note that this also includes Texture Atlases, Sprite Sheets and Retro Fonts.
*
* @method Phaser.Cache#checkImageKey
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the key exists in the cache, otherwise false.
*/
checkImageKey: function (key) {
return this.checkKey(Phaser.Cache.IMAGE, key);
},
/**
* Checks if the given key exists in the Texture Cache.
*
* @method Phaser.Cache#checkTextureKey
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the key exists in the cache, otherwise false.
*/
checkTextureKey: function (key) {
return this.checkKey(Phaser.Cache.TEXTURE, key);
},
/**
* Checks if the given key exists in the Sound Cache.
*
* @method Phaser.Cache#checkSoundKey
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the key exists in the cache, otherwise false.
*/
checkSoundKey: function (key) {
return this.checkKey(Phaser.Cache.SOUND, key);
},
/**
* Checks if the given key exists in the Text Cache.
*
* @method Phaser.Cache#checkTextKey
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the key exists in the cache, otherwise false.
*/
checkTextKey: function (key) {
return this.checkKey(Phaser.Cache.TEXT, key);
},
/**
* Checks if the given key exists in the Physics Cache.
*
* @method Phaser.Cache#checkPhysicsKey
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the key exists in the cache, otherwise false.
*/
checkPhysicsKey: function (key) {
return this.checkKey(Phaser.Cache.PHYSICS, key);
},
/**
* Checks if the given key exists in the Tilemap Cache.
*
* @method Phaser.Cache#checkTilemapKey
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the key exists in the cache, otherwise false.
*/
checkTilemapKey: function (key) {
return this.checkKey(Phaser.Cache.TILEMAP, key);
},
/**
* Checks if the given key exists in the Binary Cache.
*
* @method Phaser.Cache#checkBinaryKey
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the key exists in the cache, otherwise false.
*/
checkBinaryKey: function (key) {
return this.checkKey(Phaser.Cache.BINARY, key);
},
/**
* Checks if the given key exists in the BitmapData Cache.
*
* @method Phaser.Cache#checkBitmapDataKey
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the key exists in the cache, otherwise false.
*/
checkBitmapDataKey: function (key) {
return this.checkKey(Phaser.Cache.BITMAPDATA, key);
},
/**
* Checks if the given key exists in the BitmapFont Cache.
*
* @method Phaser.Cache#checkBitmapFontKey
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the key exists in the cache, otherwise false.
*/
checkBitmapFontKey: function (key) {
return this.checkKey(Phaser.Cache.BITMAPFONT, key);
},
/**
* Checks if the given key exists in the JSON Cache.
*
* @method Phaser.Cache#checkJSONKey
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the key exists in the cache, otherwise false.
*/
checkJSONKey: function (key) {
return this.checkKey(Phaser.Cache.JSON, key);
},
/**
* Checks if the given key exists in the XML Cache.
*
* @method Phaser.Cache#checkXMLKey
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the key exists in the cache, otherwise false.
*/
checkXMLKey: function (key) {
return this.checkKey(Phaser.Cache.XML, key);
},
/**
* Checks if the given key exists in the Video Cache.
*
* @method Phaser.Cache#checkVideoKey
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the key exists in the cache, otherwise false.
*/
checkVideoKey: function (key) {
return this.checkKey(Phaser.Cache.VIDEO, key);
},
/**
* Checks if the given key exists in the Fragment Shader Cache.
*
* @method Phaser.Cache#checkShaderKey
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the key exists in the cache, otherwise false.
*/
checkShaderKey: function (key) {
return this.checkKey(Phaser.Cache.SHADER, key);
},
/**
* Checks if the given key exists in the Render Texture Cache.
*
* @method Phaser.Cache#checkRenderTextureKey
* @param {string} key - The key of the asset within the cache.
* @return {boolean} True if the key exists in the cache, otherwise false.
*/
checkRenderTextureKey: function (key) {
return this.checkKey(Phaser.Cache.RENDER_TEXTURE, key);
},
////////////////
// Get Items //
////////////////
/**
* Get an item from a cache based on the given key and property.
*