phaser/src/plugins/PluginManager.js

304 lines
7.1 KiB
JavaScript
Raw Normal View History

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}
*/
var Class = require('../utils/Class');
2018-05-11 00:50:37 +00:00
var EventEmitter = require('eventemitter3');
var IsPlainObject = require('../utils/object/IsPlainObject');
var GetFastValue = require('../utils/object/GetFastValue');
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-05-11 00:50:37 +00:00
var corePlugins = {};
// Contains the plugins that the dev has loaded into their game
var plugins = {};
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]
*/
var PluginManager = new Class({
2018-05-11 00:50:37 +00:00
Extends: EventEmitter,
initialize:
function PluginManager (game)
{
2018-05-11 00:50:37 +00:00
EventEmitter.call(this);
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
*/
this.game = game;
2018-05-11 00:50:37 +00:00
// Plugins currently running and managed by this Plugin Manager.
// These are Game instance specific.
2018-05-10 16:14:33 +00:00
this.activePlugins = [];
2018-05-11 00:50:37 +00:00
// Plugins that should be installed into Scenes
this.scenePlugins = [];
2018-05-10 16:14:33 +00:00
if (game.isBooted)
{
this.boot();
}
else
{
game.events.once('boot', this.boot, this);
}
},
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
*/
boot: function ()
{
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)
{
2018-05-11 00:50:37 +00:00
// this.register(key, list[key]);
2018-05-10 16:14:33 +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]
*/
installGlobal: function (sys, globalPlugins)
{
2018-05-11 00:50:37 +00:00
var game = this.game;
var scene = sys.scene;
var map = sys.settings.map;
// Reference the GlobalPlugins from Game into Scene.Systems
for (var i = 0; i < globalPlugins.length; i++)
{
var pluginKey = globalPlugins[i];
// console.log('PluginManager.global', pluginKey);
if (game[pluginKey])
{
sys[pluginKey] = game[pluginKey];
// Scene level injection
if (map.hasOwnProperty(pluginKey))
{
scene[map[pluginKey]] = sys[pluginKey];
}
}
}
},
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]
*/
installLocal: function (sys, scenePlugins)
{
var scene = sys.scene;
var map = sys.settings.map;
var isBooted = sys.settings.isBooted;
for (var i = 0; i < scenePlugins.length; i++)
{
var pluginKey = scenePlugins[i];
2018-05-11 00:50:37 +00:00
if (!corePlugins[pluginKey])
{
continue;
}
2018-05-11 00:50:37 +00:00
var source = corePlugins[pluginKey];
var plugin = new source.plugin(scene);
sys[source.mapping] = plugin;
// Scene level injection
if (map.hasOwnProperty(source.mapping))
{
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-05-10 16:14:33 +00:00
/**
2018-05-11 00:50:37 +00:00
* Registers a plugin with the PluginManager.
2018-05-10 16:14:33 +00:00
*
2018-05-11 00:50:37 +00:00
* Key is a reference used to get the plugin from the plugins object (i.e. MyPlugin)
* Plugin is the function to instantiate to create a plugin instance.
2018-05-10 16:14:33 +00:00
*
* @method Phaser.Plugins.PluginManager#register
* @since 3.8.0
*
* @param {string} key - [description]
2018-05-11 00:50:37 +00:00
* @param {function} plugin - [description]
2018-05-10 16:14:33 +00:00
*/
2018-05-11 00:50:37 +00:00
register: function (key, plugin, start, isScenePlugin)
2018-05-10 16:14:33 +00:00
{
2018-05-11 00:50:37 +00:00
if (start === undefined) { start = false; }
if (isScenePlugin === undefined) { isScenePlugin = false; }
if (typeof plugin !== 'function')
{
console.warn('Invalid Plugin: ' + key);
return;
}
if (plugins.hasOwnProperty(key))
{
console.warn('Plugin key in use: ' + key);
return;
}
// Add it to the plugin store
plugins[key] = plugin;
if (start)
{
this.start(key);
}
if (isScenePlugin)
{
}
return this;
2018-05-10 16:14:33 +00:00
},
2018-05-11 00:50:37 +00:00
start: function (key)
2018-05-10 16:14:33 +00:00
{
2018-05-11 00:50:37 +00:00
var instance;
var plugin = this.get(key);
2018-05-10 16:14:33 +00:00
2018-05-11 00:50:37 +00:00
if (plugin)
{
instance = new plugin(this);
2018-05-10 16:14:33 +00:00
2018-05-11 00:50:37 +00:00
this.activePlugins.push(instance);
}
2018-05-10 16:14:33 +00:00
return instance;
},
2018-05-11 00:50:37 +00:00
setScenePlugin: function (scene)
{
},
addGameObject: function ()
{
},
addFileType: function ()
{
},
2018-05-10 16:14:33 +00:00
/**
* [description]
*
* @method Phaser.Plugins.PluginManager#get
* @since 3.8.0
*
* @param {string} key - [description]
*
* @return {Phaser.Plugins.Plugin} A Plugin object
*/
get: function (key)
{
2018-05-11 00:50:37 +00:00
return (plugins.hasOwnProperty(key)) ? plugins[key] : null;
2018-05-10 16:14:33 +00:00
},
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]
*/
remove: function (key)
{
delete plugins[key];
},
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
*/
destroy: function ()
{
this.game = null;
}
});
2018-02-12 23:13:16 +00:00
/**
2018-05-11 00:50:37 +00:00
* Static method called directly by the Core internal Plugins.
2018-02-12 23:13:16 +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-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]
*/
PluginManager.register = function (key, plugin, mapping)
{
2018-05-11 00:50:37 +00:00
corePlugins[key] = { plugin: plugin, mapping: mapping };
};
module.exports = PluginManager;