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
199 lines (149 loc) · 4.52 KB
/
Copy pathPluginManager.js
File metadata and controls
199 lines (149 loc) · 4.52 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* Phaser - PluginManager
*
* TODO: We can optimise this a lot by using separate hashes per function (update, render, etc)
*/
Phaser.PluginManager = function(game, parent) {
this.game = game;
this._parent = parent;
this.plugins = [];
this._pluginsLength = 0;
};
Phaser.PluginManager.prototype = {
/**
* Add a new Plugin to the PluginManager.
* The plugins game and parent reference are set to this game and pluginmanager parent.
* @type {Phaser.Plugin}
*/
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
if (typeof plugin['preUpdate'] === 'function') {
plugin.hasPreUpdate = true;
result = true;
}
if (typeof plugin['update'] === 'function') {
plugin.hasUpdate = true;
result = true;
}
if (typeof plugin['postUpdate'] === 'function') {
plugin.hasPostUpdate = true;
result = true;
}
if (typeof plugin['preRender'] === 'function') {
plugin.hasPreRender = true;
result = true;
}
if (typeof plugin['render'] === 'function') {
plugin.hasRender = true;
result = true;
}
if (typeof plugin['postRender'] === 'function') {
plugin.hasPostRender = true;
result = true;
}
// The plugin must have at least one of the above functions to be added to the PluginManager.
if (result) {
if (plugin.hasPreUpdate || plugin.hasUpdate || plugin.hasPostUpdate)
{
plugin.active = true;
}
if (plugin.hasPreRender || plugin.hasRender || plugin.hasPostRender)
{
plugin.visible = true;
}
this._pluginsLength = this.plugins.push(plugin);
return plugin;
}
else
{
return null;
}
},
remove: function (plugin) {
// TODO
this._pluginsLength--;
},
preUpdate: function () {
if (this._pluginsLength == 0)
{
return;
}
for (this._p = 0; this._p < this._pluginsLength; this._p++) {
if (this.plugins[this._p].active && this.plugins[this._p].hasPreUpdate) {
this.plugins[this._p].preUpdate();
}
}
},
update: function () {
if (this._pluginsLength == 0)
{
return;
}
for (this._p = 0; this._p < this._pluginsLength; this._p++) {
if (this.plugins[this._p].active && this.plugins[this._p].hasUpdate) {
this.plugins[this._p].update();
}
}
},
postUpdate: function () {
if (this._pluginsLength == 0)
{
return;
}
for (this._p = 0; this._p < this._pluginsLength; this._p++) {
if (this.plugins[this._p].active && this.plugins[this._p].hasPostUpdate) {
this.plugins[this._p].postUpdate();
}
}
},
preRender: function () {
if (this._pluginsLength == 0)
{
return;
}
for (this._p = 0; this._p < this._pluginsLength; this._p++) {
if (this.plugins[this._p].visible && this.plugins[this._p].hasPreRender) {
this.plugins[this._p].preRender();
}
}
},
render: function () {
if (this._pluginsLength == 0)
{
return;
}
for (this._p = 0; this._p < this._pluginsLength; this._p++) {
if (this.plugins[this._p].visible && this.plugins[this._p].hasRender) {
this.plugins[this._p].render();
}
}
},
postRender: function () {
if (this._pluginsLength == 0)
{
return;
}
for (this._p = 0; this._p < this._pluginsLength; this._p++) {
if (this.plugins[this._p].visible && this.plugins[this._p].hasPostRender) {
this.plugins[this._p].postRender();
}
}
},
destroy: function () {
this.plugins.length = 0;
this._pluginsLength = 0;
this.game = null;
this._parent = null;
}
};