Skip to content

Commit c8b2c2b

Browse files
committed
Support for randomScale, alpha and angle. Also renamed angle to emitterAngle.
1 parent 4fb7430 commit c8b2c2b

2 files changed

Lines changed: 135 additions & 28 deletions

File tree

v3/src/gameobjects/particles/Particle.js

Lines changed: 102 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Class = require('../../utils/Class');
22
var DegToRad = require('../../math/DegToRad');
3+
var FloatBetween = require('../../math/FloatBetween');
34

45
var Particle = new Class({
56

@@ -21,10 +22,11 @@ var Particle = new Class({
2122
this.scaleX = 1;
2223
this.scaleY = 1;
2324

24-
this.angle = 0;
25-
2625
this.alpha = 1;
2726

27+
// degs
28+
this.angle = 0;
29+
2830
// rads
2931
this.rotation = 0;
3032

@@ -69,7 +71,7 @@ var Particle = new Class({
6971

7072
if (emitter.radial)
7173
{
72-
var rad = DegToRad(emitter.angle.getRandom());
74+
var rad = DegToRad(emitter.emitterAngle.getRandom());
7375

7476
this.velocityX = Math.cos(rad) * Math.abs(sx);
7577
this.velocityY = Math.sin(rad) * Math.abs(sy);
@@ -84,23 +86,101 @@ var Particle = new Class({
8486
this.lifeCurrent = this.life;
8587

8688
// eased values
87-
emitter.scale.copyXToMinMax(this.data.scaleX);
88-
emitter.scale.copyYToMinMax(this.data.scaleY);
89-
emitter.particleAngle.copyToMinMax(this.data.angle);
90-
emitter.alpha.copyToMinMax(this.data.alpha);
89+
90+
var dataScaleX = this.data.scaleX;
91+
var dataScaleY = this.data.scaleY;
92+
var dataAngle = this.data.angle;
93+
var dataAlpha = this.data.alpha;
94+
95+
emitter.scale.copyXToMinMax(dataScaleX);
96+
emitter.scale.copyYToMinMax(dataScaleY);
97+
emitter.angle.copyToMinMax(dataAngle);
98+
emitter.alpha.copyToMinMax(dataAlpha);
99+
100+
// Random overrides
101+
102+
if (emitter.randomScaleX)
103+
{
104+
var randomScaleX = FloatBetween(emitter.randomScaleX[0], emitter.randomScaleX[1]);
105+
106+
// If there is no current ease value set we override them both
107+
if (dataScaleX.min === dataScaleX.max)
108+
{
109+
dataScaleX.min = randomScaleX;
110+
dataScaleX.max = randomScaleX;
111+
}
112+
else
113+
{
114+
// Otherwise we just reset the start value, so it still eases to the end value
115+
dataScaleX.min = randomScaleX;
116+
}
117+
}
118+
119+
if (emitter.randomScaleY)
120+
{
121+
var randomScaleY = FloatBetween(emitter.randomScaleY[0], emitter.randomScaleY[1]);
122+
123+
// If there is no current ease value set we override them both
124+
if (dataScaleY.min === dataScaleY.max)
125+
{
126+
dataScaleY.min = randomScaleY;
127+
dataScaleY.max = randomScaleY;
128+
}
129+
else
130+
{
131+
// Otherwise we just reset the start value, so it still eases to the end value
132+
dataScaleY.min = randomScaleY;
133+
}
134+
}
135+
136+
if (emitter.randomAngle)
137+
{
138+
var randomAngle = FloatBetween(emitter.randomAngle[0], emitter.randomAngle[1]);
139+
140+
// If there is no current ease value set we override them both
141+
if (dataAngle.min === dataAngle.max)
142+
{
143+
dataAngle.min = randomAngle;
144+
dataAngle.max = randomAngle;
145+
}
146+
else
147+
{
148+
// Otherwise we just reset the start value, so it still eases to the end value
149+
dataAngle.min = randomAngle;
150+
}
151+
}
152+
153+
if (emitter.randomAlpha)
154+
{
155+
var randomAlpha = FloatBetween(emitter.randomAlpha[0], emitter.randomAlpha[1]);
156+
157+
// If there is no current ease value set we override them both
158+
if (dataAlpha.min === dataAlpha.max)
159+
{
160+
dataAlpha.min = randomAlpha;
161+
dataAlpha.max = randomAlpha;
162+
}
163+
else
164+
{
165+
// Otherwise we just reset the start value, so it still eases to the end value
166+
dataAlpha.min = randomAlpha;
167+
}
168+
}
91169

92170
// Pre-calc ease values
93-
this.data.scaleX.calc = this.data.scaleX.max - this.data.scaleX.min;
94-
this.data.scaleY.calc = this.data.scaleY.max - this.data.scaleY.min;
95-
this.data.angle.calc = this.data.angle.max - this.data.angle.min;
96-
this.data.alpha.calc = this.data.alpha.max - this.data.alpha.min;
171+
dataScaleX.calc = dataScaleX.max - dataScaleX.min;
172+
dataScaleY.calc = dataScaleY.max - dataScaleY.min;
173+
dataAngle.calc = dataAngle.max - dataAngle.min;
174+
dataAlpha.calc = dataAlpha.max - dataAlpha.min;
97175

98176
// Set initial values
99-
this.rotation = DegToRad(this.data.angle.min);
100-
this.alpha = this.data.alpha.min;
177+
this.scaleX = dataScaleX.min;
178+
this.scaleY = dataScaleY.min;
179+
this.angle = dataAngle.min;
180+
this.rotation = DegToRad(dataAngle.min);
181+
182+
this.alpha = dataAlpha.min;
101183
this.color = (this.color & 0x00FFFFFF) | (((this.alpha * 0xFF) | 0) << 24);
102-
this.scaleX = this.data.scaleX.min;
103-
this.scaleY = this.data.scaleY.min;
104184

105185
this.index = emitter.alive.length;
106186
},
@@ -117,12 +197,15 @@ var Particle = new Class({
117197
this.x += this.velocityX * step;
118198
this.y += this.velocityY * step;
119199

120-
this.scaleX = this.data.scaleX.calc * emitter.easingFunctionScale(t) + this.data.scaleX.min;
121-
this.scaleY = this.data.scaleY.calc * emitter.easingFunctionScale(t) + this.data.scaleY.min;
200+
var data = this.data;
201+
202+
this.scaleX = data.scaleX.calc * emitter.easingFunctionScale(t) + data.scaleX.min;
203+
this.scaleY = data.scaleY.calc * emitter.easingFunctionScale(t) + data.scaleY.min;
122204

123-
this.rotation = DegToRad(this.data.angle.calc * emitter.easingFunctionRotation(t) + this.data.angle.min);
205+
this.angle = data.angle.calc * emitter.easingFunctionRotation(t) + data.angle.min;
206+
this.rotation = DegToRad(this.angle);
124207

125-
this.alpha = this.data.alpha.calc * emitter.easingFunctionAlpha(t) + this.data.alpha.min;
208+
this.alpha = data.alpha.calc * emitter.easingFunctionAlpha(t) + data.alpha.min;
126209

127210
this.color = (this.color & 0x00FFFFFF) | (((this.alpha * 0xFF) | 0) << 24);
128211

v3/src/gameobjects/particles/ParticleEmitter.js

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,37 @@ var ParticleEmitter = new Class({
5151

5252
this.alpha = new MinMax2(GetValue(config, 'alpha', 1));
5353

54-
this.angle = new MinMax2(GetValue(config, 'angle', { min: 0, max: 360 }));
54+
this.emitterAngle = new MinMax2(0, 360);
5555

56-
this.particleAngle = new MinMax2(GetValue(config, 'particleAngle', 0));
56+
var configEmitterAngle = GetValue(config, 'emitterAngle', null);
57+
58+
if (configEmitterAngle)
59+
{
60+
this.emitterAngle.min = configEmitterAngle.min;
61+
this.emitterAngle.max = configEmitterAngle.max;
62+
}
63+
64+
this.angle = new MinMax2(GetValue(config, 'angle', 0));
5765

5866
// The lifespan of the particles (in ms)
5967
this.lifespan = new MinMax2(GetValue(config, 'lifespan', 1000));
6068

69+
// Random overrides
70+
71+
this.randomScaleX = GetValue(config, 'randomScaleX', null);
72+
this.randomScaleY = GetValue(config, 'randomScaleY', null);
73+
this.randomScale = GetValue(config, 'randomScale', null);
74+
75+
if (this.randomScale)
76+
{
77+
this.randomScaleX = this.randomScale;
78+
this.randomScaleY = this.randomScale;
79+
}
80+
81+
this.randomAlpha = GetValue(config, 'randomAlpha', null);
82+
83+
this.randomAngle = GetValue(config, 'randomAngle', null);
84+
6185
this.deathCallback = GetValue(config, 'deathCallback', null);
6286
this.deathCallbackScope = GetValue(config, 'deathCallbackScope', null);
6387

@@ -93,9 +117,9 @@ var ParticleEmitter = new Class({
93117

94118
this.blendMode = GetValue(config, 'blendMode', BlendModes.NORMAL);
95119

96-
this.easingFunctionAlpha = GetValue(config, 'alphaEase', GetEaseFunction('Linear'));
97-
this.easingFunctionScale = GetValue(config, 'scaleEase', GetEaseFunction('Linear'));
98-
this.easingFunctionRotation = GetValue(config, 'rotationEase', GetEaseFunction('Linear'));
120+
this.easingFunctionAlpha = GetEaseFunction(GetValue(config, 'alphaEase', 'Linear'));
121+
this.easingFunctionScale = GetEaseFunction(GetValue(config, 'scaleEase', 'Linear'));
122+
this.easingFunctionRotation = GetEaseFunction(GetValue(config, 'rotationEase', 'Linear'));
99123

100124
this.follow = GetValue(config, 'follow', null);
101125
this.followOffset = new Vector2(GetValue(config, 'followOffset', 0));
@@ -230,16 +254,16 @@ var ParticleEmitter = new Class({
230254
return this;
231255
},
232256

233-
setAngle: function (min, max)
257+
setEmitterAngle: function (min, max)
234258
{
235-
this.angle.set(min, max);
259+
this.emitterAngle.set(min, max);
236260

237261
return this;
238262
},
239263

240-
setParticleAngle: function (min, max)
264+
setAngle: function (min, max)
241265
{
242-
this.particleAngle.set(min, max);
266+
this.angle.set(min, max);
243267

244268
return this;
245269
},

0 commit comments

Comments
 (0)