forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiFile.js
More file actions
239 lines (208 loc) · 6.23 KB
/
Copy pathMultiFile.js
File metadata and controls
239 lines (208 loc) · 6.23 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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../utils/Class');
/**
* @classdesc
* A MultiFile is a special kind of parent that contains two, or more, Files as children and looks after
* the loading and processing of them all. It is commonly extended and used as a base class for file types such as AtlasJSON or BitmapFont.
*
* You shouldn't create an instance of a MultiFile directly, but should extend it with your own class, setting a custom type and processing methods.
*
* @class MultiFile
* @memberof Phaser.Loader
* @constructor
* @since 3.7.0
*
* @param {Phaser.Loader.LoaderPlugin} loader - The Loader that is going to load this File.
* @param {string} type - The file type string for sorting within the Loader.
* @param {string} key - The key of the file within the loader.
* @param {Phaser.Loader.File[]} files - An array of Files that make-up this MultiFile.
*/
var MultiFile = new Class({
initialize:
function MultiFile (loader, type, key, files)
{
var finalFiles = [];
// Clean out any potential 'null' or 'undefined' file entries
files.forEach(function (file)
{
if (file)
{
finalFiles.push(file);
}
});
/**
* A reference to the Loader that is going to load this file.
*
* @name Phaser.Loader.MultiFile#loader
* @type {Phaser.Loader.LoaderPlugin}
* @since 3.7.0
*/
this.loader = loader;
/**
* The file type string for sorting within the Loader.
*
* @name Phaser.Loader.MultiFile#type
* @type {string}
* @since 3.7.0
*/
this.type = type;
/**
* Unique cache key (unique within its file type)
*
* @name Phaser.Loader.MultiFile#key
* @type {string}
* @since 3.7.0
*/
this.key = key;
/**
* The current index being used by multi-file loaders to avoid key clashes.
*
* @name Phaser.Loader.MultiFile#multiKeyIndex
* @type {number}
* @private
* @since 3.20.0
*/
this.multiKeyIndex = loader.multiKeyIndex++;
/**
* Array of files that make up this MultiFile.
*
* @name Phaser.Loader.MultiFile#files
* @type {Phaser.Loader.File[]}
* @since 3.7.0
*/
this.files = finalFiles;
/**
* The completion status of this MultiFile.
*
* @name Phaser.Loader.MultiFile#complete
* @type {boolean}
* @default false
* @since 3.7.0
*/
this.complete = false;
/**
* The number of files to load.
*
* @name Phaser.Loader.MultiFile#pending
* @type {number}
* @since 3.7.0
*/
this.pending = finalFiles.length;
/**
* The number of files that failed to load.
*
* @name Phaser.Loader.MultiFile#failed
* @type {number}
* @default 0
* @since 3.7.0
*/
this.failed = 0;
/**
* A storage container for transient data that the loading files need.
*
* @name Phaser.Loader.MultiFile#config
* @type {any}
* @since 3.7.0
*/
this.config = {};
/**
* A reference to the Loaders baseURL at the time this MultiFile was created.
* Used to populate child-files.
*
* @name Phaser.Loader.MultiFile#baseURL
* @type {string}
* @since 3.20.0
*/
this.baseURL = loader.baseURL;
/**
* A reference to the Loaders path at the time this MultiFile was created.
* Used to populate child-files.
*
* @name Phaser.Loader.MultiFile#path
* @type {string}
* @since 3.20.0
*/
this.path = loader.path;
/**
* A reference to the Loaders prefix at the time this MultiFile was created.
* Used to populate child-files.
*
* @name Phaser.Loader.MultiFile#prefix
* @type {string}
* @since 3.20.0
*/
this.prefix = loader.prefix;
// Link the files
for (var i = 0; i < finalFiles.length; i++)
{
finalFiles[i].multiFile = this;
}
},
/**
* Checks if this MultiFile is ready to process its children or not.
*
* @method Phaser.Loader.MultiFile#isReadyToProcess
* @since 3.7.0
*
* @return {boolean} `true` if all children of this MultiFile have loaded, otherwise `false`.
*/
isReadyToProcess: function ()
{
return (this.pending === 0 && this.failed === 0 && !this.complete);
},
/**
* Adds another child to this MultiFile, increases the pending count and resets the completion status.
*
* @method Phaser.Loader.MultiFile#addToMultiFile
* @since 3.7.0
*
* @param {Phaser.Loader.File} files - The File to add to this MultiFile.
*
* @return {Phaser.Loader.MultiFile} This MultiFile instance.
*/
addToMultiFile: function (file)
{
this.files.push(file);
file.multiFile = this;
this.pending++;
this.complete = false;
return this;
},
/**
* Called by each File when it finishes loading.
*
* @method Phaser.Loader.MultiFile#onFileComplete
* @since 3.7.0
*
* @param {Phaser.Loader.File} file - The File that has completed processing.
*/
onFileComplete: function (file)
{
var index = this.files.indexOf(file);
if (index !== -1)
{
this.pending--;
}
},
/**
* Called by each File that fails to load.
*
* @method Phaser.Loader.MultiFile#onFileFailed
* @since 3.7.0
*
* @param {Phaser.Loader.File} file - The File that has failed to load.
*/
onFileFailed: function (file)
{
var index = this.files.indexOf(file);
if (index !== -1)
{
this.failed++;
}
}
});
module.exports = MultiFile;