2013-08-29 02:52:59 +00:00
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2013 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
2013-08-29 02:52:59 +00:00
|
|
|
*/
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* Description.
|
|
|
|
*
|
|
|
|
* @class Phaser.PluginManager
|
2013-10-02 14:05:55 +00:00
|
|
|
* @classdesc Phaser - PluginManager
|
2013-10-01 12:54:29 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
|
|
|
* @param {Description} parent - Description.
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
Phaser.PluginManager = function(game, parent) {
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - A reference to the currently running game.
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
this.game = game;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Description} _parent - Description.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
this._parent = parent;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {array} plugins - Description.
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
this.plugins = [];
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {array} _pluginsLength - Description.
|
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
this._pluginsLength = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-08-29 06:06:16 +00:00
|
|
|
Phaser.PluginManager.prototype = {
|
2013-08-29 02:52:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new Plugin to the PluginManager.
|
2013-10-01 12:54:29 +00:00
|
|
|
* The plugin's game and parent reference are set to this game and pluginmanager parent.
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.PluginManager#add
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {Phaser.Plugin} plugin - Description.
|
|
|
|
* @return {Phaser.Plugin} Description.
|
2013-08-29 02:52:59 +00:00
|
|
|
*/
|
|
|
|
add: function (plugin) {
|
|
|
|
|
|
|
|
var result = false;
|
|
|
|
|
|
|
|
// Prototype?
|
|
|
|
if (typeof plugin === 'function')
|
|
|
|
{
|
|
|
|
plugin = new plugin(this.game, this._parent);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
plugin.game = this.game;
|
|
|
|
plugin.parent = this._parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for methods now to avoid having to do this every loop
|
2013-09-11 16:32:53 +00:00
|
|
|
if (typeof plugin['preUpdate'] === 'function')
|
|
|
|
{
|
2013-08-29 02:52:59 +00:00
|
|
|
plugin.hasPreUpdate = true;
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:32:53 +00:00
|
|
|
if (typeof plugin['update'] === 'function')
|
|
|
|
{
|
2013-08-29 02:52:59 +00:00
|
|
|
plugin.hasUpdate = true;
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:32:53 +00:00
|
|
|
if (typeof plugin['render'] === 'function')
|
|
|
|
{
|
2013-08-29 02:52:59 +00:00
|
|
|
plugin.hasRender = true;
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:32:53 +00:00
|
|
|
if (typeof plugin['postRender'] === 'function')
|
|
|
|
{
|
2013-08-29 02:52:59 +00:00
|
|
|
plugin.hasPostRender = true;
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The plugin must have at least one of the above functions to be added to the PluginManager.
|
2013-09-11 16:32:53 +00:00
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
if (plugin.hasPreUpdate || plugin.hasUpdate)
|
2013-08-29 02:52:59 +00:00
|
|
|
{
|
|
|
|
plugin.active = true;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:32:53 +00:00
|
|
|
if (plugin.hasRender || plugin.hasPostRender)
|
2013-08-29 02:52:59 +00:00
|
|
|
{
|
|
|
|
plugin.visible = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._pluginsLength = this.plugins.push(plugin);
|
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* Remove a Plugin from the PluginManager.
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.PluginManager#remove
|
|
|
|
* @param {Phaser.Plugin} plugin - The plugin to be removed.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
remove: function (plugin) {
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
this._pluginsLength--;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* Pre-update is called at the very start of the update cycle, before any other subsystems have been updated (including Physics).
|
|
|
|
* It only calls plugins who have active=true.
|
2013-10-01 12:54:29 +00:00
|
|
|
*
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.PluginManager#preUpdate
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
preUpdate: function () {
|
|
|
|
|
|
|
|
if (this._pluginsLength == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:32:53 +00:00
|
|
|
for (this._p = 0; this._p < this._pluginsLength; this._p++)
|
|
|
|
{
|
|
|
|
if (this.plugins[this._p].active && this.plugins[this._p].hasPreUpdate)
|
|
|
|
{
|
2013-08-29 02:52:59 +00:00
|
|
|
this.plugins[this._p].preUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* Update is called after all the core subsystems (Input, Tweens, Sound, etc) and the State have updated, but before the render.
|
|
|
|
* It only calls plugins who have active=true.
|
2013-10-01 12:54:29 +00:00
|
|
|
*
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.PluginManager#update
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
update: function () {
|
|
|
|
|
|
|
|
if (this._pluginsLength == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:32:53 +00:00
|
|
|
for (this._p = 0; this._p < this._pluginsLength; this._p++)
|
2013-08-29 02:52:59 +00:00
|
|
|
{
|
2013-09-11 16:32:53 +00:00
|
|
|
if (this.plugins[this._p].active && this.plugins[this._p].hasUpdate)
|
|
|
|
{
|
|
|
|
this.plugins[this._p].update();
|
2013-08-29 02:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* Render is called right after the Game Renderer completes, but before the State.render.
|
|
|
|
* It only calls plugins who have visible=true.
|
2013-10-01 12:54:29 +00:00
|
|
|
*
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.PluginManager#render
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
render: function () {
|
|
|
|
|
|
|
|
if (this._pluginsLength == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:32:53 +00:00
|
|
|
for (this._p = 0; this._p < this._pluginsLength; this._p++)
|
|
|
|
{
|
|
|
|
if (this.plugins[this._p].visible && this.plugins[this._p].hasRender)
|
|
|
|
{
|
2013-08-29 02:52:59 +00:00
|
|
|
this.plugins[this._p].render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* Post-render is called after the Game Renderer and State.render have run.
|
|
|
|
* It only calls plugins who have visible=true.
|
2013-10-01 12:54:29 +00:00
|
|
|
*
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.PluginManager#postRender
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
postRender: function () {
|
|
|
|
|
|
|
|
if (this._pluginsLength == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:32:53 +00:00
|
|
|
for (this._p = 0; this._p < this._pluginsLength; this._p++)
|
|
|
|
{
|
|
|
|
if (this.plugins[this._p].visible && this.plugins[this._p].hasPostRender)
|
|
|
|
{
|
2013-08-29 02:52:59 +00:00
|
|
|
this.plugins[this._p].postRender();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* Clear down this PluginManager and null out references
|
2013-10-01 12:54:29 +00:00
|
|
|
*
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.PluginManager#destroy
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
destroy: function () {
|
|
|
|
|
|
|
|
this.plugins.length = 0;
|
|
|
|
this._pluginsLength = 0;
|
|
|
|
this.game = null;
|
|
|
|
this._parent = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|