forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginManager.js
More file actions
176 lines (151 loc) · 4.26 KB
/
Copy pathPluginManager.js
File metadata and controls
176 lines (151 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* @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');
var plugins = {};
/**
* @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
* @memberOf Phaser.Boot
* @constructor
* @since 3.0.0
*
* @param {Phaser.Game} game - [description]
*/
var PluginManager = new Class({
initialize:
function PluginManager (game)
{
/**
* [description]
*
* @name Phaser.Boot.PluginManager#game
* @type {Phaser.Game}
* @since 3.0.0
*/
this.game = game;
game.events.once('boot', this.boot, this);
},
/**
* [description]
*
* @method Phaser.Boot.PluginManager#boot
* @since 3.0.0
*/
boot: function ()
{
this.game.events.once('destroy', this.destroy, this);
},
/**
* [description]
*
* @method Phaser.Boot.PluginManager#installGlobal
* @since 3.0.0
*
* @param {Phaser.Scenes.Systems} sys - [description]
* @param {array} globalPlugins - [description]
*/
installGlobal: function (sys, globalPlugins)
{
var game = sys.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];
}
}
}
},
/**
* [description]
*
* @method Phaser.Boot.PluginManager#installLocal
* @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];
if (!plugins[pluginKey])
{
continue;
}
var source = plugins[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();
}
}
},
/**
* [description]
*
* @method Phaser.Boot.PluginManager#remove
* @since 3.0.0
*
* @param {string} key - [description]
*/
remove: function (key)
{
delete plugins[key];
},
/**
* [description]
*
* @method Phaser.Boot.PluginManager#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.game = null;
}
});
/**
* 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)
*
* @method PluginManager.register
* @since 3.0.0
*
* @param {string} key - [description]
* @param {object} plugin - [description]
* @param {string} mapping - [description]
*/
PluginManager.register = function (key, plugin, mapping)
{
plugins[key] = { plugin: plugin, mapping: mapping };
};
module.exports = PluginManager;