Skip to content

Commit be52515

Browse files
committed
PluginManager parent parameter removed as it's redundant. Also most core functions tidied up and jsdocs fixed.
1 parent 50981fd commit be52515

4 files changed

Lines changed: 87 additions & 85 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Updated
8282
* Grunt update to dev dependencies (thanks @xtian, #695)
8383
* Emitter now emits Phaser.Particle objects instead of Phaser.Sprites, which can be extended as required.
8484
* Emitter has had various local properties removed that were already declared in Phaser.Group which it extends.
85+
* PluginManager parent parameter removed as it's redundant. Also most core functions tidied up and jsdocs fixed.
8586

8687

8788
New Features
@@ -105,6 +106,9 @@ New Features
105106
* Key.enabled boolean allows you to toggle if a Key processes its update method or dispatches any events without deleting and re-creating it.
106107
* Emitter now has minParticleAlpha and maxParticleAlpha values for setting a random alpha on emitted particles.
107108
* Emitter.particleAnchor allows you to control the anchor of emitted Particles. Defaults to 0.5 (same as before) but now under your control.
109+
* Emitter.setAlpha allows you to quickly set the min and max alpha values.
110+
* Emitter.setScale allows you to quickly set the min and max scale values.
111+
* Emitter.blendMode lets you set the blendMode of any emitted Particle (needs a browser that supports canvas blend modes)
108112

109113

110114
Bug Fixes

src/core/Game.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ Phaser.Game.prototype = {
442442
this.sound = new Phaser.SoundManager(this);
443443
this.physics = new Phaser.Physics(this, this.physicsConfig);
444444
this.particles = new Phaser.Particles(this);
445-
this.plugins = new Phaser.PluginManager(this, this);
445+
this.plugins = new Phaser.PluginManager(this);
446446
this.net = new Phaser.Net(this);
447447
this.debug = new Phaser.Utils.Debug(this);
448448

src/core/PluginManager.js

Lines changed: 58 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,42 @@
1313
* @classdesc Phaser - PluginManager
1414
* @constructor
1515
* @param {Phaser.Game} game - A reference to the currently running game.
16-
* @param {Description} parent - Description.
1716
*/
18-
Phaser.PluginManager = function(game, parent) {
17+
Phaser.PluginManager = function(game) {
1918

2019
/**
2120
* @property {Phaser.Game} game - A reference to the currently running game.
2221
*/
2322
this.game = game;
2423

2524
/**
26-
* @property {Description} _parent - Description.
27-
* @private
25+
* @property {array} plugins - An array of all the plugins being managed by this PluginManager.
2826
*/
29-
this._parent = parent;
27+
this.plugins = [];
3028

3129
/**
32-
* @property {array} plugins - Description.
30+
* @property {number} _len - Internal cache var.
31+
* @private
3332
*/
34-
this.plugins = [];
33+
this._len = 0;
3534

3635
/**
37-
* @property {array} _pluginsLength - Description.
36+
* @property {number} _i - Internal cache var.
3837
* @private
39-
* @default
4038
*/
41-
this._pluginsLength = 0;
39+
this._i = 0;
4240

4341
};
4442

4543
Phaser.PluginManager.prototype = {
4644

4745
/**
48-
* Add a new Plugin to the PluginManager.
49-
* The plugin's game and parent reference are set to this game and pluginmanager parent.
46+
* Add a new Plugin into the PluginManager.
47+
* The Plugin must have 2 properties: game and parent. Plugin.game is set to ths game reference the PluginManager uses, and parent is set to the PluginManager.
48+
*
5049
* @method Phaser.PluginManager#add
51-
* @param {Phaser.Plugin} plugin - Description.
52-
* @return {Phaser.Plugin} Description.
50+
* @param {object|Phaser.Plugin} plugin - The Plugin to add into the PluginManager. This can be a function or an existing object.
51+
* @return {Phaser.Plugin} The Plugin that was added to the manager.
5352
*/
5453
add: function (plugin) {
5554

@@ -63,7 +62,7 @@ Phaser.PluginManager.prototype = {
6362
else
6463
{
6564
plugin.game = this.game;
66-
plugin.parent = this._parent;
65+
plugin.parent = this;
6766
}
6867

6968
// Check for methods now to avoid having to do this every loop
@@ -110,7 +109,7 @@ Phaser.PluginManager.prototype = {
110109
plugin.visible = true;
111110
}
112111

113-
this._pluginsLength = this.plugins.push(plugin);
112+
this._len = this.plugins.push(plugin);
114113

115114
// Allows plugins to run potentially destructive code outside of the constructor, and only if being added to the PluginManager
116115
if (typeof plugin['init'] === 'function')
@@ -127,41 +126,48 @@ Phaser.PluginManager.prototype = {
127126
},
128127

129128
/**
130-
* Remove a Plugin from the PluginManager.
129+
* Remove a Plugin from the PluginManager. It calls Plugin.destroy on the plugin before removing it from the manager.
130+
*
131131
* @method Phaser.PluginManager#remove
132132
* @param {Phaser.Plugin} plugin - The plugin to be removed.
133133
*/
134134
remove: function (plugin) {
135135

136-
if (this._pluginsLength === 0)
137-
{
138-
return;
139-
}
136+
this._i = this._len;
140137

141-
for (this._p = 0; this._p < this._pluginsLength; this._p++)
138+
while (this._i--)
142139
{
143-
if (this.plugins[this._p] === plugin)
140+
if (this.plugins[this._i] === plugin)
144141
{
145142
plugin.destroy();
146-
this.plugins.splice(this._p, 1);
147-
this._pluginsLength--;
143+
this.plugins.splice(this._i, 1);
144+
this._len--;
148145
return;
149146
}
150147
}
148+
151149
},
152150

153151
/**
154-
* Removes all Plugins from the PluginManager.
152+
* Remove all Plugins from the PluginManager. It calls Plugin.destroy on every plugin before removing it from the manager.
153+
*
155154
* @method Phaser.PluginManager#removeAll
156155
*/
157156
removeAll: function() {
158157

159-
for (this._p = 0; this._p < this._pluginsLength; this._p++)
158+
this._i = this._len;
159+
160+
while (this._i--)
160161
{
161-
this.plugins[this._p].destroy();
162+
if (this.plugins[this._i] === plugin)
163+
{
164+
plugin.destroy();
165+
}
162166
}
167+
163168
this.plugins.length = 0;
164-
this._pluginsLength = 0;
169+
this._len = 0;
170+
165171
},
166172

167173
/**
@@ -172,16 +178,13 @@ Phaser.PluginManager.prototype = {
172178
*/
173179
preUpdate: function () {
174180

175-
if (this._pluginsLength === 0)
176-
{
177-
return;
178-
}
181+
this._i = this._len;
179182

180-
for (this._p = 0; this._p < this._pluginsLength; this._p++)
183+
while (this._i--)
181184
{
182-
if (this.plugins[this._p].active && this.plugins[this._p].hasPreUpdate)
185+
if (this.plugins[this._i].active && this.plugins[this._i].hasPreUpdate)
183186
{
184-
this.plugins[this._p].preUpdate();
187+
this.plugins[this._i].preUpdate();
185188
}
186189
}
187190

@@ -195,16 +198,13 @@ Phaser.PluginManager.prototype = {
195198
*/
196199
update: function () {
197200

198-
if (this._pluginsLength === 0)
199-
{
200-
return;
201-
}
201+
this._i = this._len;
202202

203-
for (this._p = 0; this._p < this._pluginsLength; this._p++)
203+
while (this._i--)
204204
{
205-
if (this.plugins[this._p].active && this.plugins[this._p].hasUpdate)
205+
if (this.plugins[this._i].active && this.plugins[this._i].hasUpdate)
206206
{
207-
this.plugins[this._p].update();
207+
this.plugins[this._i].update();
208208
}
209209
}
210210

@@ -219,16 +219,13 @@ Phaser.PluginManager.prototype = {
219219
*/
220220
postUpdate: function () {
221221

222-
if (this._pluginsLength === 0)
223-
{
224-
return;
225-
}
222+
this._i = this._len;
226223

227-
for (this._p = 0; this._p < this._pluginsLength; this._p++)
224+
while (this._i--)
228225
{
229-
if (this.plugins[this._p].active && this.plugins[this._p].hasPostUpdate)
226+
if (this.plugins[this._i].active && this.plugins[this._i].hasPostUpdate)
230227
{
231-
this.plugins[this._p].postUpdate();
228+
this.plugins[this._i].postUpdate();
232229
}
233230
}
234231

@@ -242,16 +239,13 @@ Phaser.PluginManager.prototype = {
242239
*/
243240
render: function () {
244241

245-
if (this._pluginsLength === 0)
246-
{
247-
return;
248-
}
242+
this._i = this._len;
249243

250-
for (this._p = 0; this._p < this._pluginsLength; this._p++)
244+
while (this._i--)
251245
{
252-
if (this.plugins[this._p].visible && this.plugins[this._p].hasRender)
246+
if (this.plugins[this._i].visible && this.plugins[this._i].hasRender)
253247
{
254-
this.plugins[this._p].render();
248+
this.plugins[this._i].render();
255249
}
256250
}
257251

@@ -265,32 +259,28 @@ Phaser.PluginManager.prototype = {
265259
*/
266260
postRender: function () {
267261

268-
if (this._pluginsLength === 0)
269-
{
270-
return;
271-
}
262+
this._i = this._len;
272263

273-
for (this._p = 0; this._p < this._pluginsLength; this._p++)
264+
while (this._i--)
274265
{
275-
if (this.plugins[this._p].visible && this.plugins[this._p].hasPostRender)
266+
if (this.plugins[this._i].visible && this.plugins[this._i].hasPostRender)
276267
{
277-
this.plugins[this._p].postRender();
268+
this.plugins[this._i].postRender();
278269
}
279270
}
280271

281272
},
282273

283274
/**
284-
* Clear down this PluginManager and null out references
275+
* Clear down this PluginManager, calls destroy on every plugin and nulls out references.
285276
*
286277
* @method Phaser.PluginManager#destroy
287278
*/
288279
destroy: function () {
289280

290-
this.plugins.length = 0;
291-
this._pluginsLength = 0;
281+
this.removeAll();
282+
292283
this.game = null;
293-
this._parent = null;
294284

295285
}
296286

src/particles/arcade/Emitter.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,6 @@ Phaser.Particles.Arcade.Emitter = function (game, x, y, maxParticles) {
4040
*/
4141
this.type = Phaser.EMITTER;
4242

43-
/**
44-
* @property {number} x - The X position of the top left corner of the emitter in world space.
45-
* @default
46-
*/
47-
// this.x = 0;
48-
49-
/**
50-
* @property {number} y - The Y position of the top left corner of emitter in world space.
51-
* @default
52-
*/
53-
// this.y = 0;
54-
5543
/**
5644
* @property {number} width - The width of the emitter. Particles can be randomly generated from anywhere within this box.
5745
* @default
@@ -165,10 +153,10 @@ Phaser.Particles.Arcade.Emitter = function (game, x, y, maxParticles) {
165153
this.particleAnchor = new Phaser.Point(0.5, 0.5);
166154

167155
/**
168-
* @property {boolean} exists - Determines whether the emitter is being updated by the core game loop.
156+
* @property {number} blendMode - The blendMode as set on the particle when emitted from the Emitter. Defaults to NORMAL. Needs browser capable of supporting canvas blend-modes (most not available in WebGL)
169157
* @default
170158
*/
171-
// this.exists = true;
159+
this.blendMode = Phaser.blendModes.NORMAL;
172160

173161
/**
174162
* The point the particles are emitted from.
@@ -479,6 +467,8 @@ Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function () {
479467
particle.alpha = this.game.rnd.realInRange(this.minParticleAlpha, this.maxParticleAlpha);
480468
}
481469

470+
particle.blendMode = this.blendMode;
471+
482472
particle.body.gravity.y = this.gravity;
483473
particle.body.drag.x = this.particleDrag.x;
484474
particle.body.drag.y = this.particleDrag.y;
@@ -532,7 +522,8 @@ Phaser.Particles.Arcade.Emitter.prototype.setYSpeed = function (min, max) {
532522
};
533523

534524
/**
535-
* A more compact way of setting the angular velocity constraints of the emitter.
525+
* A more compact way of setting the angular velocity constraints of the particles.
526+
*
536527
* @method Phaser.Particles.Arcade.Emitter#setRotation
537528
* @param {number} [min=0] - The minimum value for this range.
538529
* @param {number} [max=0] - The maximum value for this range.
@@ -548,7 +539,7 @@ Phaser.Particles.Arcade.Emitter.prototype.setRotation = function (min, max) {
548539
};
549540

550541
/**
551-
* A more compact way of setting the alpha constraints of the emitter.
542+
* A more compact way of setting the alpha constraints of the particles.
552543
*
553544
* @method Phaser.Particles.Arcade.Emitter#setAlpha
554545
* @param {number} [min=1] - The minimum value for this range.
@@ -564,6 +555,23 @@ Phaser.Particles.Arcade.Emitter.prototype.setAlpha = function (min, max) {
564555

565556
};
566557

558+
/**
559+
* A more compact way of setting the scale constraints of the particles.
560+
*
561+
* @method Phaser.Particles.Arcade.Emitter#setScale
562+
* @param {number} [min=1] - The minimum value for this range.
563+
* @param {number} [max=1] - The maximum value for this range.
564+
*/
565+
Phaser.Particles.Arcade.Emitter.prototype.setScale = function (min, max) {
566+
567+
if (typeof min === 'undefined') { min = 1; }
568+
if (typeof max === 'undefined') { max = 1; }
569+
570+
this.minParticleScale = min;
571+
this.maxParticleScale = max;
572+
573+
};
574+
567575
/**
568576
* Change the emitters center to match the center of any object with a `center` property, such as a Sprite.
569577
* If the object doesn't have a center property it will be set to object.x + object.width / 2

0 commit comments

Comments
 (0)