Skip to content

Commit 5744459

Browse files
committed
Loader now emits a progress event which can be used for progress bar loaders, etc. Also added setBasePath and removed console logs.
1 parent 13ed054 commit 5744459

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

src/loader/LoaderPlugin.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ var LoaderPlugin = new Class({
3333
// Inject the available filetypes into the Loader
3434
FileTypesManager.install(this);
3535

36-
// Move to a 'setURL' method?
3736
this.baseURL = '';
3837
this.path = '';
3938

@@ -46,6 +45,9 @@ var LoaderPlugin = new Class({
4645

4746
this.crossOrigin = undefined;
4847

48+
this.totalToLoad = 0;
49+
this.progress = 0;
50+
4951
this.list = new CustomSet();
5052
this.inflight = new CustomSet();
5153
this.failed = new CustomSet();
@@ -57,12 +59,23 @@ var LoaderPlugin = new Class({
5759

5860
boot: function ()
5961
{
62+
// Set values from scene / game configs
63+
64+
65+
6066
var eventEmitter = this.systems.events;
6167

6268
eventEmitter.on('shutdown', this.shutdown, this);
6369
eventEmitter.on('destroy', this.destroy, this);
6470
},
6571

72+
setBaseURL: function (url)
73+
{
74+
this.baseURL = url;
75+
76+
return this;
77+
},
78+
6679
setPath: function (path)
6780
{
6881
if (path.substr(-1) !== '/')
@@ -103,13 +116,16 @@ var LoaderPlugin = new Class({
103116

104117
start: function ()
105118
{
106-
console.log(this.scene.sys.settings.key, '- LoaderPlugin start. Files to load:', this.list.size);
119+
// console.log(this.scene.sys.settings.key, '- Loader start. Files to load:', this.list.size);
107120

108121
if (!this.isReady())
109122
{
110123
return;
111124
}
112125

126+
this.progress = 0;
127+
this.totalToLoad = this.list.size;
128+
113129
this.emit('start', this);
114130

115131
if (this.list.size === 0)
@@ -134,7 +150,9 @@ var LoaderPlugin = new Class({
134150

135151
updateProgress: function ()
136152
{
153+
this.progress = 1 - (this.list.size / this.totalToLoad);
137154

155+
this.emit('progress', this.progress);
138156
},
139157

140158
processLoadQueue: function ()
@@ -186,15 +204,19 @@ var LoaderPlugin = new Class({
186204

187205
if (success)
188206
{
207+
this.emit('load', previousFile);
189208
this.queue.set(previousFile);
190209
}
191210
else
192211
{
212+
this.emit('loaderror', previousFile);
193213
this.failed.set(previousFile);
194214
}
195215

196216
this.inflight.delete(previousFile);
197217

218+
this.updateProgress();
219+
198220
if (this.list.size > 0)
199221
{
200222
// console.log('nextFile - still something in the list');
@@ -211,19 +233,20 @@ var LoaderPlugin = new Class({
211233
{
212234
// console.log('---> LoaderPlugin.finishedLoading PROCESSING', this.queue.size, 'files');
213235

214-
if(this.state === CONST.LOADER_PROCESSING)
236+
if (this.state === CONST.LOADER_PROCESSING)
215237
{
216238
return;
217239
}
218240

241+
this.progress = 1;
242+
219243
this.state = CONST.LOADER_PROCESSING;
220244

221245
this.storage.clear();
222246

223247
this.queue.each(function (file)
224248
{
225249
// console.log('%c Calling process on ' + file.key, 'color: #000000; background: #ffff00;');
226-
227250
file.onProcess(this.processUpdate.bind(this));
228251
}, this);
229252
},
@@ -284,7 +307,7 @@ var LoaderPlugin = new Class({
284307

285308
processComplete: function ()
286309
{
287-
console.log(this.scene.sys.settings.key, '- Loader Complete. Loaded:', this.storage.size, 'Failed:', this.failed.size);
310+
// console.log(this.scene.sys.settings.key, '- Loader Complete. Loaded:', this.storage.size, 'Failed:', this.failed.size);
288311

289312
this.list.clear();
290313
this.inflight.clear();
@@ -515,6 +538,8 @@ var LoaderPlugin = new Class({
515538
this.storage.clear();
516539

517540
this.removeAllListeners('start');
541+
this.removeAllListeners('load');
542+
this.removeAllListeners('loaderror');
518543
this.removeAllListeners('complete');
519544

520545
this.tag = '';

0 commit comments

Comments
 (0)