forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.js
More file actions
245 lines (190 loc) · 7.21 KB
/
Copy pathLoader.js
File metadata and controls
245 lines (190 loc) · 7.21 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
var BaseLoader = require('../../loader/BaseLoader');
var Class = require('../../utils/Class');
var NumberArray = require('../../utils/array/NumberArray');
var AnimationJSONFile = require('../../loader/filetypes/AnimationJSONFile');
var AtlasJSONFile = require('../../loader/filetypes/AtlasJSONFile');
var BinaryFile = require('../../loader/filetypes/BinaryFile');
var BitmapFontFile = require('../../loader/filetypes/BitmapFontFile');
var GLSLFile = require('../../loader/filetypes/GLSLFile');
var HTMLFile = require('../../loader/filetypes/HTMLFile');
var ImageFile = require('../../loader/filetypes/ImageFile');
var JSONFile = require('../../loader/filetypes/JSONFile');
var ScriptFile = require('../../loader/filetypes/ScriptFile');
var SpriteSheet = require('../../loader/filetypes/SpriteSheet');
var SVGFile = require('../../loader/filetypes/SVGFile');
var TextFile = require('../../loader/filetypes/TextFile');
var UnityAtlasFile = require('../../loader/filetypes/UnityAtlasFile');
var XMLFile = require('../../loader/filetypes/XMLFile');
var AudioFile = require('../../loader/filetypes/AudioFile');
var TilemapCSVFile = require('../../loader/filetypes/TilemapCSVFile');
var TilemapJSONFile = require('../../loader/filetypes/TilemapJSONFile');
var Loader = new Class({
Extends: BaseLoader,
initialize:
function Loader (scene)
{
BaseLoader.call(this, scene);
this._multilist = {};
},
// key can be either a string, an object or an array of objects
image: function (key, url, xhrSettings)
{
return ImageFile.create(this, key, url, xhrSettings);
},
animation: function (key, url, xhrSettings)
{
return AnimationJSONFile.create(this, key, url, xhrSettings);
},
json: function (key, url, xhrSettings)
{
return JSONFile.create(this, key, url, xhrSettings);
},
script: function (key, url, xhrSettings)
{
return ScriptFile.create(this, key, url, xhrSettings);
},
xml: function (key, url, xhrSettings)
{
return XMLFile.create(this, key, url, xhrSettings);
},
binary: function (key, url, xhrSettings)
{
return BinaryFile.create(this, key, url, xhrSettings);
},
text: function (key, url, xhrSettings)
{
return TextFile.create(this, key, url, xhrSettings);
},
glsl: function (key, url, xhrSettings)
{
return GLSLFile.create(this, key, url, xhrSettings);
},
html: function (key, url, width, height, xhrSettings)
{
return HTMLFile.create(this, key, url, width, height, xhrSettings);
},
svg: function (key, url, xhrSettings)
{
return SVGFile.create(this, key, url, xhrSettings);
},
// config can include: frameWidth, frameHeight, startFrame, endFrame, margin, spacing
spritesheet: function (key, url, config, xhrSettings)
{
return SpriteSheet.create(this, key, url, config, xhrSettings);
},
// config can include: instances
audio: function (key, urls, config, xhrSettings)
{
return AudioFile.create(this, key, urls, config, xhrSettings);
},
tilemapCSV: function (key, url, xhrSettings)
{
return TilemapCSVFile.create(this, key, url, xhrSettings);
},
tilemapJSON: function (key, url, xhrSettings)
{
return TilemapJSONFile.create(this, key, url, xhrSettings);
},
// ---------------------------------------------------
// Multi-File Loaders
// ---------------------------------------------------
unityAtlas: function (key, textureURL, atlasURL, textureXhrSettings, atlasXhrSettings)
{
// Returns an object with two properties: 'texture' and 'data'
var files = new UnityAtlasFile(key, textureURL, atlasURL, this.path, textureXhrSettings, atlasXhrSettings);
this.addFile(files.texture);
this.addFile(files.data);
return this;
},
atlas: function (key, textureURL, atlasURL, textureXhrSettings, atlasXhrSettings)
{
// Returns an object with two properties: 'texture' and 'data'
var files = new AtlasJSONFile(key, textureURL, atlasURL, this.path, textureXhrSettings, atlasXhrSettings);
this.addFile(files.texture);
this.addFile(files.data);
return this;
},
bitmapFont: function (key, textureURL, xmlURL, textureXhrSettings, xmlXhrSettings)
{
// Returns an object with two properties: 'texture' and 'data'
var files = new BitmapFontFile(key, textureURL, xmlURL, this.path, textureXhrSettings, xmlXhrSettings);
this.addFile(files.texture);
this.addFile(files.data);
return this;
},
multiatlas: function (key, textureURLs, atlasURLs, textureXhrSettings, atlasXhrSettings)
{
if (typeof textureURLs === 'number')
{
var total = textureURLs;
var suffix = (atlasURLs === undefined) ? '' : atlasURLs;
textureURLs = NumberArray(0, total, key + suffix, '.png');
atlasURLs = NumberArray(0, total, key + suffix, '.json');
}
else
{
if (!Array.isArray(textureURLs))
{
textureURLs = [ textureURLs ];
}
if (!Array.isArray(atlasURLs))
{
atlasURLs = [ atlasURLs ];
}
}
var file;
var i = 0;
var multiKey;
this._multilist[key] = [];
for (i = 0; i < textureURLs.length; i++)
{
multiKey = '_MA_IMG_' + key + '_' + i.toString();
file = new ImageFile(multiKey, textureURLs[i], this.path, textureXhrSettings);
this.addFile(file);
this._multilist[key].push(multiKey);
}
for (i = 0; i < atlasURLs.length; i++)
{
multiKey = '_MA_JSON_' + key + '_' + i.toString();
file = new JSONFile(multiKey, atlasURLs[i], this.path, atlasXhrSettings);
this.addFile(file);
this._multilist[key].push(multiKey);
}
},
loadArray: function (files)
{
if (Array.isArray(files))
{
for (var i = 0; i < files.length; i++)
{
this.file(files[i]);
}
}
return (this.list.size > 0);
},
file: function (file)
{
var entry;
switch (file.type)
{
case 'spritesheet':
entry = this.spritesheet(file.key, file.url, file.config, file.xhrSettings);
break;
case 'atlas':
entry = this.atlas(file.key, file.textureURL, file.atlasURL, file.textureXhrSettings, file.atlasXhrSettings);
break;
case 'bitmapFont':
entry = this.bitmapFont(file.key, file.textureURL, file.xmlURL, file.textureXhrSettings, file.xmlXhrSettings);
break;
case 'multiatlas':
entry = this.multiatlas(file.key, file.textureURLs, file.atlasURLs, file.textureXhrSettings, file.atlasXhrSettings);
break;
// image, json, xml, binary, text, glsl, svg
default:
entry = this[file.type](file.key, file.url, file.xhrSettings);
break;
}
return entry;
}
});
module.exports = Loader;