Skip to content

Commit 12a9233

Browse files
committed
Added ability for particle frames to be either random or cycled, with quantity option
1 parent 6e95285 commit 12a9233

2 files changed

Lines changed: 74 additions & 8 deletions

File tree

v3/src/gameobjects/particles/Particle.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ var Particle = new Class({
132132
this.maxVelocityX = emitter.maxVelocityX.onEmit(this, 'maxVelocityX');
133133
this.maxVelocityY = emitter.maxVelocityY.onEmit(this, 'maxVelocityY');
134134

135-
this.delayCurrent = emitter.lifespan.onEmit(this, 'delay');
135+
this.delayCurrent = emitter.delay.onEmit(this, 'delay');
136136

137137
this.life = emitter.lifespan.onEmit(this, 'lifespan');
138138
this.lifeCurrent = this.life;
@@ -239,6 +239,7 @@ var Particle = new Class({
239239
if (this.delayCurrent > 0)
240240
{
241241
this.delayCurrent -= delta;
242+
242243
return false;
243244
}
244245

v3/src/gameobjects/particles/ParticleEmitter.js

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var RandomZone = require('./zones/RandomZone');
1111
var Rectangle = require('../../geom/rectangle/Rectangle');
1212
var StableSort = require('../../utils/array/StableSort');
1313
var Vector2 = require('../../math/Vector2');
14+
var Wrap = require('../../math/Wrap');
1415

1516
var ParticleEmitter = new Class({
1617

@@ -38,6 +39,14 @@ var ParticleEmitter = new Class({
3839

3940
this.defaultFrame = manager.defaultFrame;
4041

42+
this.randomFrame = true;
43+
44+
this.frameQuantity = 1;
45+
46+
this.currentFrame = 0;
47+
48+
this._frameCounter = 0;
49+
4150
this.x = new EmitterOp(config, 'x', 0);
4251
this.y = new EmitterOp(config, 'y', 0);
4352

@@ -169,11 +178,11 @@ var ParticleEmitter = new Class({
169178
this.followOffset = new Vector2(GetFastValue(config, 'followOffset', 0));
170179
this.trackVisible = GetFastValue(config, 'trackVisible', false);
171180

172-
var frame = GetFastValue(config, 'frame', null);
181+
var frameConfig = GetFastValue(config, 'frame', null);
173182

174-
if (frame)
183+
if (frameConfig)
175184
{
176-
this.setFrame(frame);
185+
this.setFrame(frameConfig);
177186
}
178187
},
179188

@@ -215,16 +224,72 @@ var ParticleEmitter = new Class({
215224
{
216225
return this.defaultFrame;
217226
}
218-
else
227+
else if (this.randomFrame)
219228
{
220229
return GetRandomElement(this.frames);
221230
}
231+
else
232+
{
233+
var frame = this.frames[this.currentFrame];
234+
235+
this._frameCounter++;
236+
237+
if (this._frameCounter === this.frameQuantity)
238+
{
239+
this._frameCounter = 0;
240+
this.currentFrame = Wrap(this.currentFrame + 1, 0, this._frameLength);
241+
}
242+
243+
return frame;
244+
}
222245
},
223246

224-
// Either a single frame (numeric / string based), or an array of frames to randomly pick from
225-
setFrame: function (frames)
247+
// frame: 0
248+
// frame: 'red'
249+
// frame: [ 0, 1, 2, 3 ]
250+
// frame: [ 'red', 'green', 'blue', 'pink', 'white' ]
251+
// frame: { frames: [ 'red', 'green', 'blue', 'pink', 'white' ], [cycle: bool], [quantity: int] }
252+
253+
setFrame: function (frames, pickRandom, quantity)
226254
{
227-
this.manager.setEmitterFrames(frames, this);
255+
if (pickRandom === undefined) { pickRandom = true; }
256+
if (quantity === undefined) { quantity = 1; }
257+
258+
this.randomFrame = pickRandom;
259+
this.frameQuantity = quantity;
260+
this.currentFrame = 0;
261+
this._frameCounter = 0;
262+
263+
var t = typeof (frames);
264+
265+
if (Array.isArray(frames) || t === 'string' || t === 'number')
266+
{
267+
this.manager.setEmitterFrames(frames, this);
268+
}
269+
else if (t === 'object')
270+
{
271+
var frameConfig = frames;
272+
var frames = GetFastValue(frameConfig, 'frames', null);
273+
274+
if (frames)
275+
{
276+
this.manager.setEmitterFrames(frames, this);
277+
}
278+
279+
var isCycle = GetFastValue(frameConfig, 'cycle', false);
280+
281+
this.randomFrame = (isCycle) ? false : true;
282+
283+
this.frameQuantity = GetFastValue(frameConfig, 'quantity', quantity);
284+
}
285+
286+
this._frameLength = this.frames.length;
287+
288+
if (this._frameLength === 1)
289+
{
290+
this.frameQuantity = 1;
291+
this.randomFrame = false;
292+
}
228293

229294
return this;
230295
},

0 commit comments

Comments
 (0)