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
|
|
|
|
2018-05-10 16:14:33 +00:00
|
|
|
// Contains the plugins that Phaser uses globally and locally.
|
|
|
|
// These are the source objects, not instantiated.
|
2018-01-16 02:08:04 +00:00
|
|
|
var plugins = {};
|
2018-01-09 01:30:10 +00:00
|
|
|
|
2018-02-12 23:13:16 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* The PluginManager is global and belongs to the Game instance, not a Scene.
|
|
|
|
* It handles the installation and removal of all global and Scene based plugins.
|
|
|
|
* Plugins automatically register themselves with the PluginManager in their respective classes.
|
|
|
|
*
|
|
|
|
* @class PluginManager
|
2018-05-10 16:14:33 +00:00
|
|
|
* @memberOf Phaser.Plugins
|
2018-02-12 23:13:16 +00:00
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Game} game - [description]
|
|
|
|
*/
|
2018-01-09 01:30:10 +00:00
|
|
|
var PluginManager = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2018-01-18 13:59:32 +00:00
|
|
|
function PluginManager (game)
|
2018-01-09 01:30:10 +00:00
|
|
|
{
|
2018-02-12 23:13:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-05-10 16:14:33 +00:00
|
|
|
* @name Phaser.Plugins.PluginManager#game
|
2018-02-12 23:13:16 +00:00
|
|
|
* @type {Phaser.Game}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-16 02:08:04 +00:00
|
|
|
this.game = game;
|
2018-01-18 05:21:14 +00:00
|
|
|
|
2018-05-10 16:14:33 +00:00
|
|
|
// Plugins currently running and managed by this Plugin Manager
|
|
|
|
// These are Game instance specific
|
|
|
|
this.activePlugins = [];
|
|
|
|
|
|
|
|
if (game.isBooted)
|
|
|
|
{
|
|
|
|
this.boot();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
game.events.once('boot', this.boot, this);
|
|
|
|
}
|
2018-01-16 02:08:04 +00:00
|
|
|
},
|
|
|
|
|
2018-02-12 23:13:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-05-10 16:14:33 +00:00
|
|
|
* @method Phaser.Plugins.PluginManager#boot
|
2018-02-12 23:13:16 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
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-05-10 16:14:33 +00:00
|
|
|
|
|
|
|
// Any plugins to install?
|
|
|
|
/*
|
|
|
|
var list = this.game.config.installPlugins;
|
|
|
|
|
|
|
|
if (list)
|
|
|
|
{
|
|
|
|
for (var key in list)
|
|
|
|
{
|
|
|
|
this.add(key, list[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2018-01-16 02:08:04 +00:00
|
|
|
},
|
2018-01-09 01:30:10 +00:00
|
|
|
|
2018-02-12 23:13:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-05-10 16:14:33 +00:00
|
|
|
* @method Phaser.Plugins.PluginManager#installGlobal
|
2018-02-12 23:13:16 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Scenes.Systems} sys - [description]
|
|
|
|
* @param {array} globalPlugins - [description]
|
|
|
|
*/
|
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-02-12 23:13:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-05-10 16:14:33 +00:00
|
|
|
* @method Phaser.Plugins.PluginManager#installLocal
|
2018-02-12 23:13:16 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Scenes.Systems} sys - [description]
|
|
|
|
* @param {array} scenePlugins - [description]
|
|
|
|
*/
|
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-05-10 16:14:33 +00:00
|
|
|
/**
|
|
|
|
* Register a plugin with the PluginManager. This is the same as calling the
|
|
|
|
* static function, but is available via the game pluginManager instance.
|
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
*
|
|
|
|
* @method Phaser.Plugins.PluginManager#register
|
|
|
|
* @since 3.8.0
|
|
|
|
*
|
|
|
|
* @param {string} key - [description]
|
|
|
|
* @param {object} plugin - [description]
|
|
|
|
* @param {string} mapping - [description]
|
|
|
|
*/
|
|
|
|
register: function (key, plugin, mapping)
|
|
|
|
{
|
|
|
|
plugins[key] = { plugin: plugin, mapping: mapping };
|
|
|
|
},
|
|
|
|
|
|
|
|
add: function (plugin)
|
|
|
|
{
|
|
|
|
var instance = new plugin(this.game);
|
|
|
|
|
|
|
|
var key = instance.key;
|
|
|
|
|
|
|
|
plugins[key] = { plugin: plugin };
|
|
|
|
|
|
|
|
this.activePlugins.push(instance);
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Plugins.PluginManager#get
|
|
|
|
* @since 3.8.0
|
|
|
|
*
|
|
|
|
* @param {string} key - [description]
|
|
|
|
*
|
|
|
|
* @return {Phaser.Plugins.Plugin} A Plugin object
|
|
|
|
*/
|
|
|
|
get: function (key)
|
|
|
|
{
|
|
|
|
return (plugins[key]) ? plugins[key].plugin : null;
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function (time, delta)
|
|
|
|
{
|
|
|
|
var activePlugins = this.activePlugins;
|
|
|
|
|
|
|
|
for (var i = 0; i < activePlugins.length; i++)
|
|
|
|
{
|
|
|
|
var plugin = activePlugins[i];
|
|
|
|
|
|
|
|
if (plugin.active)
|
|
|
|
{
|
|
|
|
plugin.step(time, delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-12 23:13:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-05-10 16:14:33 +00:00
|
|
|
* @method Phaser.Plugins.PluginManager#remove
|
2018-02-12 23:13:16 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {string} key - [description]
|
|
|
|
*/
|
2018-01-16 02:08:04 +00:00
|
|
|
remove: function (key)
|
|
|
|
{
|
|
|
|
delete plugins[key];
|
|
|
|
},
|
2018-01-09 01:30:10 +00:00
|
|
|
|
2018-02-12 23:13:16 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-05-10 16:14:33 +00:00
|
|
|
* @method Phaser.Plugins.PluginManager#destroy
|
2018-02-12 23:13:16 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
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-02-12 23:13:16 +00:00
|
|
|
/**
|
|
|
|
* Static method called directly by the Plugins
|
|
|
|
* 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-05-10 16:14:33 +00:00
|
|
|
* @method Phaser.Plugins.PluginManager.register
|
2018-02-12 23:13:16 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {string} key - [description]
|
|
|
|
* @param {object} plugin - [description]
|
|
|
|
* @param {string} mapping - [description]
|
|
|
|
*/
|
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;
|