2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2018-01-09 01:30:10 +00:00
|
|
|
var Class = require('../utils/Class');
|
2018-01-16 02:08:04 +00:00
|
|
|
|
|
|
|
var plugins = {};
|
2018-01-09 01:30:10 +00:00
|
|
|
|
|
|
|
var PluginManager = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2018-01-16 02:08:04 +00:00
|
|
|
// The PluginManager is global and belongs to the Game instance, not a Scene.
|
2018-01-18 13:59:32 +00:00
|
|
|
function PluginManager (game)
|
2018-01-09 01:30:10 +00:00
|
|
|
{
|
2018-01-16 02:08:04 +00:00
|
|
|
this.game = game;
|
2018-01-18 05:21:14 +00:00
|
|
|
|
|
|
|
game.events.once('boot', this.boot, this);
|
2018-01-16 02:08:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
boot: function ()
|
|
|
|
{
|
2018-01-31 03:38:10 +00:00
|
|
|
this.game.events.once('destroy', this.destroy, this);
|
2018-01-16 02:08:04 +00:00
|
|
|
},
|
2018-01-09 01:30:10 +00:00
|
|
|
|
2018-01-18 05:21:14 +00:00
|
|
|
installGlobal: function (sys, globalPlugins)
|
2018-01-16 02:08:04 +00:00
|
|
|
{
|
2018-01-18 05:21:14 +00:00
|
|
|
var game = sys.game;
|
|
|
|
var scene = sys.scene;
|
|
|
|
var map = sys.settings.map;
|
2018-01-16 02:08:04 +00:00
|
|
|
|
2018-01-18 05:21:14 +00:00
|
|
|
// Reference the GlobalPlugins from Game into Scene.Systems
|
2018-01-16 22:28:29 +00:00
|
|
|
for (var i = 0; i < globalPlugins.length; i++)
|
2018-01-16 02:08:04 +00:00
|
|
|
{
|
2018-01-18 05:21:14 +00:00
|
|
|
var pluginKey = globalPlugins[i];
|
|
|
|
|
|
|
|
// console.log('PluginManager.global', pluginKey);
|
2018-01-16 02:08:04 +00:00
|
|
|
|
2018-01-30 13:15:50 +00:00
|
|
|
if (game[pluginKey])
|
2018-01-18 05:21:14 +00:00
|
|
|
{
|
2018-01-30 13:15:50 +00:00
|
|
|
sys[pluginKey] = game[pluginKey];
|
|
|
|
|
|
|
|
// Scene level injection
|
|
|
|
if (map.hasOwnProperty(pluginKey))
|
|
|
|
{
|
|
|
|
scene[map[pluginKey]] = sys[pluginKey];
|
|
|
|
}
|
2018-01-18 05:21:14 +00:00
|
|
|
}
|
2018-01-16 22:28:29 +00:00
|
|
|
}
|
2018-01-18 05:21:14 +00:00
|
|
|
},
|
2018-01-16 22:28:29 +00:00
|
|
|
|
2018-01-18 05:21:14 +00:00
|
|
|
installLocal: function (sys, scenePlugins)
|
|
|
|
{
|
|
|
|
var scene = sys.scene;
|
|
|
|
var map = sys.settings.map;
|
2018-01-18 13:59:32 +00:00
|
|
|
var isBooted = sys.settings.isBooted;
|
2018-01-18 05:21:14 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < scenePlugins.length; i++)
|
2018-01-16 22:28:29 +00:00
|
|
|
{
|
2018-01-18 05:21:14 +00:00
|
|
|
var pluginKey = scenePlugins[i];
|
|
|
|
|
2018-01-30 13:15:50 +00:00
|
|
|
if (!plugins[pluginKey])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-18 05:21:14 +00:00
|
|
|
var source = plugins[pluginKey];
|
|
|
|
|
2018-01-18 13:59:32 +00:00
|
|
|
var plugin = new source.plugin(scene);
|
2018-01-16 22:28:29 +00:00
|
|
|
|
2018-01-18 13:59:32 +00:00
|
|
|
sys[source.mapping] = plugin;
|
2018-01-17 03:41:58 +00:00
|
|
|
|
2018-01-18 05:21:14 +00:00
|
|
|
// Scene level injection
|
|
|
|
if (map.hasOwnProperty(source.mapping))
|
|
|
|
{
|
2018-01-18 13:59:32 +00:00
|
|
|
scene[map[source.mapping]] = plugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scene is already booted, usually because this method is being called at run-time, so boot the plugin
|
|
|
|
if (isBooted)
|
|
|
|
{
|
|
|
|
plugin.boot();
|
2018-01-16 02:08:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 01:30:10 +00:00
|
|
|
},
|
|
|
|
|
2018-01-16 02:08:04 +00:00
|
|
|
remove: function (key)
|
|
|
|
{
|
|
|
|
delete plugins[key];
|
|
|
|
},
|
2018-01-09 01:30:10 +00:00
|
|
|
|
2018-01-16 02:08:04 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
2018-01-31 03:38:10 +00:00
|
|
|
this.game = null;
|
2018-01-16 02:08:04 +00:00
|
|
|
}
|
2018-01-09 01:30:10 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-01-16 02:08:04 +00:00
|
|
|
// Static method called directly by the Plugins
|
2018-01-18 05:21:14 +00:00
|
|
|
// Key is a reference used to get the plugin from the plugins object (i.e. InputPlugin)
|
|
|
|
// Plugin is the object to instantiate to create the plugin
|
|
|
|
// Mapping is what the plugin is injected into the Scene.Systems as (i.e. input)
|
2018-01-16 02:08:04 +00:00
|
|
|
|
2018-01-18 05:21:14 +00:00
|
|
|
PluginManager.register = function (key, plugin, mapping)
|
2018-01-16 02:08:04 +00:00
|
|
|
{
|
2018-01-18 05:21:14 +00:00
|
|
|
plugins[key] = { plugin: plugin, mapping: mapping };
|
2018-01-16 02:08:04 +00:00
|
|
|
};
|
2018-01-09 01:30:10 +00:00
|
|
|
|
|
|
|
module.exports = PluginManager;
|