Skip to content

Commit fb7cdbf

Browse files
committed
Added ability to load plugins from external files and have them register with the PluginManager.
1 parent d46662c commit fb7cdbf

4 files changed

Lines changed: 91 additions & 7 deletions

File tree

src/loader/File.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ File.createObjectURL = function (image, blob, defaultType)
204204
*/
205205
File.revokeObjectURL = function (image)
206206
{
207-
if(URL)
207+
if (typeof URL === 'function')
208208
{
209209
URL.revokeObjectURL(image.src);
210210
}

src/loader/Loader.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var GLSLFile = require('./filetypes/GLSLFile');
1515
var HTMLFile = require('./filetypes/HTMLFile');
1616
var ImageFile = require('./filetypes/ImageFile');
1717
var JSONFile = require('./filetypes/JSONFile');
18+
var PluginFile = require('./filetypes/PluginFile');
1819
var ScriptFile = require('./filetypes/ScriptFile');
1920
var SpriteSheet = require('./filetypes/SpriteSheet');
2021
var SVGFile = require('./filetypes/SVGFile');
@@ -75,6 +76,11 @@ var Loader = new Class({
7576
return ScriptFile.create(this, key, url, xhrSettings);
7677
},
7778

79+
plugin: function (key, url, xhrSettings)
80+
{
81+
return PluginFile.create(this, key, url, xhrSettings);
82+
},
83+
7884
xml: function (key, url, xhrSettings)
7985
{
8086
return XMLFile.create(this, key, url, xhrSettings);
@@ -321,7 +327,7 @@ var Loader = new Class({
321327

322328
shutdown: function ()
323329
{
324-
330+
// TODO
325331
}
326332

327333
});

src/loader/filetypes/PluginFile.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
var Class = require('../../utils/Class');
2+
var CONST = require('../const');
3+
var File = require('../File');
4+
var GetFastValue = require('../../utils/object/GetFastValue');
5+
var PluginManager = require('../../plugins/PluginManager');
6+
7+
// Phaser.Loader.FileTypes.PluginFile
8+
9+
var PluginFile = new Class({
10+
11+
Extends: File,
12+
13+
initialize:
14+
15+
function PluginFile (key, url, path, xhrSettings)
16+
{
17+
var fileKey = (typeof key === 'string') ? key : GetFastValue(key, 'key', '');
18+
19+
var fileConfig = {
20+
type: 'script',
21+
extension: GetFastValue(key, 'extension', 'js'),
22+
responseType: 'text',
23+
key: fileKey,
24+
url: GetFastValue(key, 'file', url),
25+
path: path,
26+
xhrSettings: GetFastValue(key, 'xhr', xhrSettings)
27+
};
28+
29+
File.call(this, fileConfig);
30+
},
31+
32+
onProcess: function (callback)
33+
{
34+
this.state = CONST.FILE_PROCESSING;
35+
36+
this.data = document.createElement('script');
37+
this.data.language = 'javascript';
38+
this.data.type = 'text/javascript';
39+
this.data.defer = false;
40+
this.data.text = this.xhrLoader.responseText;
41+
42+
document.head.appendChild(this.data);
43+
44+
// Need to wait for onload?
45+
window[this.key].register(PluginManager);
46+
47+
this.onComplete();
48+
49+
callback(this);
50+
}
51+
52+
});
53+
54+
PluginFile.create = function (loader, key, url, xhrSettings)
55+
{
56+
if (Array.isArray(key))
57+
{
58+
for (var i = 0; i < key.length; i++)
59+
{
60+
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
61+
loader.addFile(new PluginFile(key[i], url, loader.path, xhrSettings));
62+
}
63+
}
64+
else
65+
{
66+
loader.addFile(new PluginFile(key, url, loader.path, xhrSettings));
67+
}
68+
69+
// For method chaining
70+
return loader;
71+
};
72+
73+
module.exports = PluginFile;

src/scene/Systems.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var Systems = new Class({
2828

2929
this.anims;
3030
this.cache;
31+
this.plugins;
3132
this.registry;
3233
this.sound;
3334
this.textures;
@@ -47,13 +48,17 @@ var Systems = new Class({
4748
{
4849
this.game = game;
4950

50-
game.plugins.installGlobal(this, GlobalPlugins);
51+
var pluginManager = game.plugins;
5152

52-
game.plugins.installLocal(this, CoreScenePlugins);
53+
this.plugins = pluginManager;
5354

54-
game.plugins.installLocal(this, GetScenePlugins(this));
55+
pluginManager.installGlobal(this, GlobalPlugins);
5556

56-
game.plugins.installLocal(this, GetPhysicsPlugins(this));
57+
pluginManager.installLocal(this, CoreScenePlugins);
58+
59+
pluginManager.installLocal(this, GetScenePlugins(this));
60+
61+
pluginManager.installLocal(this, GetPhysicsPlugins(this));
5762

5863
this.events.emit('boot', this);
5964

@@ -67,7 +72,7 @@ var Systems = new Class({
6772
plugin = [ plugin ];
6873
}
6974

70-
this.game.plugins.installLocal(this, plugin);
75+
this.plugins.installLocal(this, plugin);
7176
},
7277

7378
step: function (time, delta)

0 commit comments

Comments
 (0)