You can now specify a files payload in the State config, which are loaded automatically before the State is run. Useful for config files, small preloader assets, etc.

This commit is contained in:
Richard Davey 2017-02-07 12:54:51 +00:00
parent a5c22cb2a2
commit 7ef44200bb
4 changed files with 110 additions and 21 deletions

View file

@ -1,4 +1,4 @@
var CHECKSUM = {
build: '971b5a80-ecca-11e6-90b6-d3ad81c3e5e2'
build: '42e0cd50-ed34-11e6-9a71-679050a34535'
};
module.exports = CHECKSUM;

View file

@ -40,7 +40,11 @@ var Settings = {
transparent: GetObjectValue(config, 'transparent', false),
autoResize: GetObjectValue(config, 'autoResize', false),
roundPixels: GetObjectValue(config, 'roundPixels', false),
drawToPrimaryCanvas: GetObjectValue(config, 'drawToPrimaryCanvas', false)
drawToPrimaryCanvas: GetObjectValue(config, 'drawToPrimaryCanvas', false),
// Loader payload array
files: GetObjectValue(config, 'files', false)
};
},

View file

@ -369,42 +369,83 @@ StateManager.prototype = {
state.settings.active = true;
// + arguments
if (state.init)
var loader = state.sys.load;
// Files payload?
if (loader && Array.isArray(state.sys.settings.files))
{
state.init.call(state);
}
loader.reset();
if (state.preload && state.sys.load)
{
state.sys.load.reset();
state.preload.call(state, this.game);
// Is the loader empty?
if (state.sys.load.list.size === 0)
if (loader.loadArray(state.sys.settings.files))
{
this.startCreate(state);
loader.events.once('LOADER_COMPLETE_EVENT', this.payloadComplete.bind(this));
loader.start();
}
else
{
// Start the loader going as we have something in the queue
state.sys.load.events.once('LOADER_COMPLETE_EVENT', this.loadComplete.bind(this));
state.sys.load.start();
this.bootState(state);
}
}
else
{
// No preload? Then there was nothing to load either
this.bootState(state);
}
}
},
payloadComplete: function (event)
{
console.log('payloadComplete');
var state = event.loader.state;
this.bootState(state);
},
bootState: function (state)
{
console.log('bootState', state);
// + arguments
if (state.init)
{
state.init.call(state);
}
var loader = state.sys.load;
if (state.preload && loader)
{
loader.reset();
state.preload.call(state, this.game);
// Is the loader empty?
if (loader.list.size === 0)
{
this.startCreate(state);
}
else
{
// Start the loader going as we have something in the queue
loader.events.once('LOADER_COMPLETE_EVENT', this.loadComplete.bind(this));
loader.start();
}
}
else
{
// No preload? Then there was nothing to load either
this.startCreate(state);
}
},
loadComplete: function (event)
{
console.log('loadComplete');
var state = event.loader.state;
// Make sure to do load-update one last time before state is set to _created

View file

@ -27,6 +27,50 @@ var Loader = function (state)
Loader.prototype = Object.create(BaseLoader.prototype);
Loader.prototype.constructor = Loader;
Loader.prototype.loadArray = function (files)
{
if (Array.isArray(files))
{
for (var i = 0; i < files.length; i++)
{
this.file(files[i]);
}
}
return (this.list.size > 0);
};
Loader.prototype.file = function (file)
{
var entry;
switch (file.type)
{
case 'image':
case 'json':
case 'xml':
case 'binary':
case 'text':
case 'glsl':
entry = this[file.type](file.key, file.url, file.xhrSettings);
break;
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 'multiatlas':
entry = this.multiatlas(file.key, file.textureURLs, file.atlasURLs, file.textureXhrSettings, file.atlasXhrSettings);
break;
}
return entry;
};
Loader.prototype.image = function (key, url, xhrSettings)
{
var file = new ImageFile(key, url, this.path, xhrSettings);