Skip to content

Commit 816b228

Browse files
committed
Loads of little fixes all across the emitter classes.
1 parent 80a652e commit 816b228

3 files changed

Lines changed: 145 additions & 249 deletions

File tree

v3/src/gameobjects/particles/GetValueOp.js

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1+
var FloatBetween = require('../../math/FloatBetween');
12
var GetEaseFunction = require('../../tweens/builder/GetEaseFunction');
2-
var GetValue = require('../../utils/object/GetValue');
33
var GetFastValue = require('../../utils/object/GetFastValue');
4-
var FloatBetween = require('../../math/FloatBetween');
4+
var Wrap = require('../../math/Wrap');
55

6-
function hasOnEmit (def)
6+
function has (object, key)
77
{
8-
return (!!def.onEmit && typeof def.onEmit === 'function');
8+
return (object.hasOwnProperty(key));
99
}
1010

11-
function hasOnUpdate (def)
11+
function hasBoth (object, key1, key2)
1212
{
13-
return (!!def.onUpdate && typeof def.onUpdate === 'function');
13+
return (object.hasOwnProperty(key1) && object.hasOwnProperty(key2));
1414
}
1515

1616
function hasGetters (def)
1717
{
18-
return hasOnEmit(def) || hasOnUpdate(def);
18+
return (has(def, 'onEmit')) || (has(def, 'onUpdate'));
1919
}
2020

21+
var steps = 0;
22+
var counter = 0;
23+
2124
var GetValueOp = function (config, key, defaultValue)
2225
{
2326
var propertyValue = GetFastValue(config, key, defaultValue);
@@ -73,43 +76,61 @@ var GetValueOp = function (config, key, defaultValue)
7376

7477
particleUpdate = propertyValue;
7578
}
76-
else if (t === 'object' && propertyValue.hasOwnProperty('start') && propertyValue.hasOwnProperty('end'))
79+
else if (t === 'object' && (has(propertyValue, 'random') || hasBoth(propertyValue, 'start', 'end') || hasBoth(propertyValue, 'min', 'max')))
7780
{
78-
var start = propertyValue.start;
79-
var end = propertyValue.end;
81+
var start = (propertyValue.hasOwnProperty('start')) ? propertyValue.start : propertyValue.min;
82+
var end = (propertyValue.hasOwnProperty('end')) ? propertyValue.end : propertyValue.max;
83+
var isRandom = false;
8084

8185
// A random starting value:
8286

83-
// x: { start: 100, end: 400, randomStart: true }
87+
// x: { start: 100, end: 400, random: true } OR { min: 100, max: 400, random: true } OR { random: [ 100, 400 ] }
8488

85-
if (propertyValue.hasOwnProperty('randomStart'))
89+
if (has(propertyValue, 'random'))
8690
{
87-
particleEmit = function ()
91+
isRandom = true;
92+
93+
var rnd = propertyValue.random;
94+
95+
// x: { random: [ 100, 400 ] } = the same as doing: x: { start: 100, end: 400, random: true }
96+
if (Array.isArray(rnd))
8897
{
89-
return FloatBetween(start, end);
90-
};
91-
}
92-
else
93-
{
94-
particleEmit = function ()
98+
start = rnd[0];
99+
end = rnd[1];
100+
}
101+
102+
particleEmit = function (particle, key)
95103
{
96-
return start;
104+
var data = particle.data[key];
105+
106+
var value = FloatBetween(start, end);
107+
108+
data.min = value;
109+
110+
return value;
97111
};
98112
}
99113

100-
if (propertyValue.hasOwnProperty('steps'))
114+
if (has(propertyValue, 'steps'))
101115
{
102116
// A stepped (per emit) range
103117

104118
// x: { start: 100, end: 400, steps: 64 }
105119

106120
// Increments a value stored in the emitter
107121

108-
particleUpdate = function (particle, key)
122+
steps = propertyValue.steps;
123+
counter = start;
124+
125+
particleEmit = function ()
109126
{
110-
var emitter = particle.emitter;
127+
var value = counter;
128+
129+
var i = value + ((end - start) / steps);
130+
131+
counter = Wrap(i, start, end);
111132

112-
return emitter.getNext(key);
133+
return counter;
113134
};
114135
}
115136
else
@@ -122,12 +143,25 @@ var GetValueOp = function (config, key, defaultValue)
122143

123144
var easeFunc = GetEaseFunction(ease);
124145

146+
if (!isRandom)
147+
{
148+
particleEmit = function (particle, key)
149+
{
150+
var data = particle.data[key];
151+
152+
data.min = start;
153+
data.max = end;
154+
155+
return start;
156+
};
157+
}
158+
125159
particleUpdate = function (particle, key, t, value)
126160
{
127161
var data = particle.data[key];
128162

129-
return data.calc * easeFunc(t) + data.min;
130-
}
163+
return (data.max - data.min) * easeFunc(t) + data.min;
164+
};
131165
}
132166
}
133167
else if (t === 'object' && hasGetters(propertyValue))
@@ -150,12 +184,12 @@ var GetValueOp = function (config, key, defaultValue)
150184
}
151185
*/
152186

153-
if (hasOnEmit(propertyValue))
187+
if (has(propertyValue, 'onEmit'))
154188
{
155189
particleEmit = propertyValue.onEmit;
156190
}
157191

158-
if (hasOnUpdate(propertyValue))
192+
if (has(propertyValue, 'onUpdate'))
159193
{
160194
particleUpdate = propertyValue.onUpdate;
161195
}

v3/src/gameobjects/particles/Particle.js

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

54
var Particle = new Class({
65

@@ -44,10 +43,10 @@ var Particle = new Class({
4443
// ease data
4544
this.data = {
4645
tint: { min: 0xffffff, max: 0xffffff, current: 0xffffff },
47-
alpha: { min: 1, max: 1, calc: 0 },
48-
angle: { min: 0, max: 0, calc: 0 },
49-
scaleX: { min: 1, max: 1, calc: 0 },
50-
scaleY: { min: 1, max: 1, calc: 0 }
46+
alpha: { min: 1, max: 1 },
47+
angle: { min: 0, max: 0 },
48+
scaleX: { min: 1, max: 1 },
49+
scaleY: { min: 1, max: 1 }
5150
};
5251
},
5352

@@ -67,15 +66,15 @@ var Particle = new Class({
6766
emitter.zone.getRandomPoint(this);
6867
}
6968

70-
this.x += emitter.x.getNext();
71-
this.y += emitter.y.getNext();
69+
this.x += emitter.x.onEmit(this, 'x');
70+
this.y += emitter.y.onEmit(this, 'y');
7271

73-
var sx = emitter.speedX.getNext();
74-
var sy = emitter.speedY.getNext();
72+
var sx = emitter.speedX.onEmit(this, 'speedX');
73+
var sy = (emitter.speedY) ? emitter.speedY.onEmit(this, 'speedY') : sx;
7574

7675
if (emitter.radial)
7776
{
78-
var rad = DegToRad(emitter.emitterAngle.getNext());
77+
var rad = DegToRad(emitter.emitterAngle.onEmit(this, 'angle'));
7978

8079
this.velocityX = Math.cos(rad) * Math.abs(sx);
8180
this.velocityY = Math.sin(rad) * Math.abs(sy);
@@ -86,104 +85,17 @@ var Particle = new Class({
8685
this.velocityY = sy;
8786
}
8887

89-
this.life = emitter.lifespan.getNext();
88+
this.life = emitter.lifespan.onEmit(this, 'lifespan');
9089
this.lifeCurrent = this.life;
9190

92-
// eased values
91+
this.scaleX = emitter.scaleX.onEmit(this, 'scaleX');
92+
this.scaleY = (emitter.scaleY) ? emitter.scaleY.onEmit(this, 'scaleY') : this.scaleX;
9393

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

180-
// Set initial values
181-
this.scaleX = dataScaleX.min;
182-
this.scaleY = dataScaleY.min;
183-
this.angle = dataAngle.min;
184-
this.rotation = DegToRad(dataAngle.min);
97+
this.alpha = emitter.alpha.onEmit(this, 'alpha');
18598

186-
this.alpha = dataAlpha.min;
18799
this.color = (this.color & 0x00FFFFFF) | (((this.alpha * 0xFF) | 0) << 24);
188100

189101
this.index = emitter.alive.length;
@@ -197,21 +109,27 @@ var Particle = new Class({
197109
// How far along in life is this particle? (t = 0 to 1)
198110
var t = 1 - (this.lifeCurrent / this.life);
199111

200-
this.velocityX += (emitter.gravity.x * step);
201-
this.velocityY += (emitter.gravity.y * step);
112+
this.velocityX += (emitter.gravityX * step);
113+
this.velocityY += (emitter.gravityY * step);
202114

203115
this.x += this.velocityX * step;
204116
this.y += this.velocityY * step;
205117

206-
var data = this.data;
118+
this.scaleX = emitter.scaleX.onUpdate(this, 'scaleX', t, this.scaleX);
207119

208-
this.scaleX = data.scaleX.calc * emitter.easingFunctionScale(t) + data.scaleX.min;
209-
this.scaleY = data.scaleY.calc * emitter.easingFunctionScale(t) + data.scaleY.min;
120+
if (emitter.scaleY)
121+
{
122+
this.scaleY = emitter.scaleY.onUpdate(this, 'scaleY', t, this.scaleY);
123+
}
124+
else
125+
{
126+
this.scaleY = this.scaleX;
127+
}
210128

211-
this.angle = data.angle.calc * emitter.easingFunctionRotation(t) + data.angle.min;
129+
this.angle = emitter.particleAngle.onUpdate(this, 'angle', t, this.angle);
212130
this.rotation = DegToRad(this.angle);
213131

214-
this.alpha = data.alpha.calc * emitter.easingFunctionAlpha(t) + data.alpha.min;
132+
this.alpha = emitter.alpha.onUpdate(this, 'alpha', t, this.alpha);
215133

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

0 commit comments

Comments
 (0)