Skip to content

Commit 0b1d32b

Browse files
committed
New registerFileType argument, new Global Plugin mapping and createEntry method
* PluginManager.registerFileType has a new property `addToScene` which allows you to inject the new file type into the LoaderPlugin of the given Scene. You could use this to add the file type into the Scene in which it was loaded. * PluginManager.install has a new property `mapping`. This allows you to give a Global Plugin a property key, so that it is automatically injected into any Scenes as a Scene level instance. This allows you to have a single global plugin running in the PluginManager, that is injected into every Scene automatically. * PluginManager.createEntry is a new private method to create a plugin entry and return it. This avoids code duplication in several other methods, which now use this instead.
1 parent cad3271 commit 0b1d32b

3 files changed

Lines changed: 83 additions & 42 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
* Container.setInteractive can now be called without any arguments as long as you have called Container.setSize first (thanks rex)
1414
* Bob.reset will now reset the position, frame, flip, visible and alpha values of the Bob.
1515
* VisibilityHandler now takes a game instance as its sole argument, instead of an event emitter.
16+
* PluginManager.registerFileType has a new property `addToScene` which allows you to inject the new file type into the LoaderPlugin of the given Scene. You could use this to add the file type into the Scene in which it was loaded.
17+
* PluginManager.install has a new property `mapping`. This allows you to give a Global Plugin a property key, so that it is automatically injected into any Scenes as a Scene level instance. This allows you to have a single global plugin running in the PluginManager, that is injected into every Scene automatically.
18+
* PluginManager.createEntry is a new private method to create a plugin entry and return it. This avoids code duplication in several other methods, which now use this instead.
19+
* The Plugin File Type has a new optional argument `mapping`, which allows a global plugin to be injected into a Scene as a reference.
1620

1721
### Bug Fixes
1822

src/plugins/PluginManager.js

Lines changed: 79 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var Remove = require('../utils/array/Remove');
1919
* @property {string} key - The unique name of this plugin within the plugin cache.
2020
* @property {function} plugin - An instance of the plugin.
2121
* @property {boolean} [active] - Is the plugin active or not?
22+
* @property {string} [mapping] - If this plugin is to be injected into the Scene Systems, this is the property key map used.
2223
*/
2324

2425
/**
@@ -157,15 +158,16 @@ var PluginManager = new Class({
157158
{
158159
entry = list[i];
159160

160-
// { key: 'TestPlugin', plugin: TestPlugin, start: true }
161+
// { key: 'TestPlugin', plugin: TestPlugin, start: true, mapping: 'test' }
161162

162163
key = GetFastValue(entry, 'key', null);
163164
plugin = GetFastValue(entry, 'plugin', null);
164165
start = GetFastValue(entry, 'start', false);
166+
mapping = GetFastValue(entry, 'mapping', null);
165167

166168
if (key && plugin)
167169
{
168-
this.install(key, plugin, start);
170+
this.install(key, plugin, start, mapping);
169171
}
170172
}
171173

@@ -200,6 +202,10 @@ var PluginManager = new Class({
200202
/**
201203
* Called by the Scene Systems class. Tells the plugin manager to install all Scene plugins into it.
202204
*
205+
* First it will install global references, i.e. references from the Game systems into the Scene Systems (and Scene if mapped.)
206+
* Then it will install Core Scene Plugins followed by Scene Plugins registered with the PluginManager.
207+
* Finally it will install any references to Global Plugins that have a Scene mapping property into the Scene itself.
208+
*
203209
* @method Phaser.Plugins.PluginManager#addToScene
204210
* @protected
205211
* @since 3.8.0
@@ -212,6 +218,7 @@ var PluginManager = new Class({
212218
{
213219
var i;
214220
var pluginKey;
221+
var pluginList;
215222
var game = this.game;
216223
var scene = sys.scene;
217224
var map = sys.settings.map;
@@ -236,7 +243,7 @@ var PluginManager = new Class({
236243

237244
for (var s = 0; s < scenePlugins.length; s++)
238245
{
239-
var pluginList = scenePlugins[s];
246+
pluginList = scenePlugins[s];
240247

241248
for (i = 0; i < pluginList.length; i++)
242249
{
@@ -270,6 +277,19 @@ var PluginManager = new Class({
270277
}
271278
}
272279
}
280+
281+
// And finally, inject any 'global scene plugins'
282+
pluginList = this.plugins;
283+
284+
for (i = 0; i < pluginList.length; i++)
285+
{
286+
var entry = pluginList[i];
287+
288+
if (entry.mapping)
289+
{
290+
scene[entry.mapping] = entry.plugin;
291+
}
292+
}
273293
},
274294

275295
/**
@@ -377,11 +397,13 @@ var PluginManager = new Class({
377397
*
378398
* @param {string} key - The unique handle given to this plugin within the Plugin Manager.
379399
* @param {function} plugin - The plugin code. This should be the non-instantiated version.
380-
* @param {boolean} [start=false] - Automatically start the plugin running?
400+
* @param {boolean} [start=false] - Automatically start the plugin running? This is always `true` if you provide a mapping value.
401+
* @param {string} [mapping] - If this plugin is injected into the Phaser.Scene class, this is the property key to use.
381402
*/
382-
install: function (key, plugin, start)
403+
install: function (key, plugin, start, mapping)
383404
{
384405
if (start === undefined) { start = false; }
406+
if (mapping === undefined) { mapping = null; }
385407

386408
if (typeof plugin !== 'function')
387409
{
@@ -395,16 +417,21 @@ var PluginManager = new Class({
395417
return;
396418
}
397419

420+
if (mapping !== null)
421+
{
422+
start = true;
423+
}
424+
425+
console.log('install', key, start, mapping);
426+
398427
if (!this.game.isBooted)
399428
{
400-
this._pendingGlobal.push({ key: key, plugin: plugin, start: start });
429+
this._pendingGlobal.push({ key: key, plugin: plugin, start: start, mapping: mapping });
401430
}
402431
else
403432
{
404433
// Add it to the plugin store
405-
PluginCache.registerCustom(key, plugin);
406-
407-
// gamePlugins[key] = plugin;
434+
PluginCache.registerCustom(key, plugin, mapping);
408435

409436
if (start)
410437
{
@@ -513,26 +540,46 @@ var PluginManager = new Class({
513540
}
514541
else if (!entry)
515542
{
516-
var plugin = this.getClass(key);
543+
entry = this.createEntry(key, runAs);
544+
}
517545

518-
if (plugin)
519-
{
520-
var instance = new plugin(this);
546+
return (entry) ? entry.plugin : null;
547+
},
521548

522-
entry = {
523-
key: runAs,
524-
plugin: instance,
525-
active: true
526-
};
549+
/**
550+
* Creates a new instance of a global plugin, adds an entry into the plugins array and returns it.
551+
*
552+
* @method Phaser.Plugins.PluginManager#createEntry
553+
* @private
554+
* @since 3.9.0
555+
*
556+
* @param {string} key - The key of the plugin to create an instance of.
557+
* @param {string} [runAs] - Run the plugin under a new key. This allows you to run one plugin multiple times.
558+
*
559+
* @return {?Phaser.Plugins.BasePlugin} The plugin that was started, or `null` if invalid key given.
560+
*/
561+
createEntry: function (key, runAs)
562+
{
563+
var entry = PluginCache.getCustom(key);
527564

528-
this.plugins.push(entry);
565+
if (entry)
566+
{
567+
var instance = new entry.plugin(this);
529568

530-
instance.init();
531-
instance.start();
532-
}
569+
entry = {
570+
key: runAs,
571+
plugin: instance,
572+
active: true,
573+
mapping: entry.mapping
574+
};
575+
576+
this.plugins.push(entry);
577+
578+
instance.init();
579+
instance.start();
533580
}
534581

535-
return (entry) ? entry.plugin : null;
582+
return entry;
536583
},
537584

538585
/**
@@ -593,20 +640,9 @@ var PluginManager = new Class({
593640

594641
if (plugin && autoStart)
595642
{
596-
var instance = new plugin(this);
597-
598-
entry = {
599-
key: key,
600-
plugin: instance,
601-
active: true
602-
};
603-
604-
this.plugins.push(entry);
643+
entry = this.createEntry(key, key);
605644

606-
instance.init();
607-
instance.start();
608-
609-
return instance;
645+
return (entry) ? entry.plugin : null;
610646
}
611647
else if (plugin)
612648
{
@@ -755,10 +791,16 @@ var PluginManager = new Class({
755791
*
756792
* @param {string} key - The key of the Game Object that the given callbacks will create, i.e. `image`, `sprite`.
757793
* @param {function} callback - The callback to invoke when the Game Object Factory is called.
794+
* @param {Phaser.Scene} [addToScene] - Optionally add this file type into the Loader Plugin owned by the given Scene.
758795
*/
759-
registerFileType: function (key, callback)
796+
registerFileType: function (key, callback, addToScene)
760797
{
761798
FileTypesManager.register(key, callback);
799+
800+
if (addToScene && addToScene.sys.load)
801+
{
802+
addToScene.sys.load[key] = callback;
803+
}
762804
},
763805

764806
/**

src/scene/Systems.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,6 @@ var Systems = new Class({
235235

236236
pluginManager.addToScene(this, DefaultPlugins.Global, [ DefaultPlugins.CoreScene, GetScenePlugins(this), GetPhysicsPlugins(this) ]);
237237

238-
// pluginManager.installSceneGlobal(this, DefaultPlugins.Global);
239-
// pluginManager.installSceneLocal(this, DefaultPlugins.CoreScene);
240-
// pluginManager.installSceneLocal(this, GetScenePlugins(this));
241-
// pluginManager.installSceneLocal(this, GetPhysicsPlugins(this));
242-
243238
this.events.emit('boot', this);
244239

245240
this.settings.isBooted = true;

0 commit comments

Comments
 (0)