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.
|
|
|
|
function PluginManager (game, config)
|
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-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-18 05:21:14 +00:00
|
|
|
sys[pluginKey] = game[pluginKey];
|
|
|
|
|
|
|
|
// Scene level injection
|
|
|
|
if (map.hasOwnProperty(pluginKey))
|
|
|
|
{
|
|
|
|
scene[map[pluginKey]] = sys[pluginKey];
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
var source = plugins[pluginKey];
|
|
|
|
|
|
|
|
// console.log('PluginManager.local', pluginKey, 'to', source.mapping);
|
2018-01-16 22:28:29 +00:00
|
|
|
|
2018-01-18 05:21:14 +00:00
|
|
|
sys[source.mapping] = new source.plugin(scene);
|
2018-01-17 03:41:58 +00:00
|
|
|
|
2018-01-18 05:21:14 +00:00
|
|
|
// Scene level injection
|
|
|
|
if (map.hasOwnProperty(source.mapping))
|
|
|
|
{
|
|
|
|
scene[map[source.mapping]] = sys[source.mapping];
|
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 ()
|
|
|
|
{
|
|
|
|
plugins = {};
|
|
|
|
}
|
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-09 01:30:10 +00:00
|
|
|
|
2018-01-18 05:21:14 +00:00
|
|
|
// console.log('PluginManager.register', key, mapping);
|
2018-01-16 02:08:04 +00:00
|
|
|
};
|
2018-01-09 01:30:10 +00:00
|
|
|
|
|
|
|
module.exports = PluginManager;
|