Skip to content

Commit 80a652e

Browse files
committed
Moving to a value ops system for emitters, like the TweenBuilder
1 parent 5cb8f98 commit 80a652e

3 files changed

Lines changed: 292 additions & 39 deletions

File tree

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
var GetEaseFunction = require('../../tweens/builder/GetEaseFunction');
2+
var GetValue = require('../../utils/object/GetValue');
3+
var GetFastValue = require('../../utils/object/GetFastValue');
4+
var FloatBetween = require('../../math/FloatBetween');
5+
6+
function hasOnEmit (def)
7+
{
8+
return (!!def.onEmit && typeof def.onEmit === 'function');
9+
}
10+
11+
function hasOnUpdate (def)
12+
{
13+
return (!!def.onUpdate && typeof def.onUpdate === 'function');
14+
}
15+
16+
function hasGetters (def)
17+
{
18+
return hasOnEmit(def) || hasOnUpdate(def);
19+
}
20+
21+
var GetValueOp = function (config, key, defaultValue)
22+
{
23+
var propertyValue = GetFastValue(config, key, defaultValue);
24+
25+
// The returned value sets what the property will be at the START of the particles life, on emit
26+
var particleEmit = function (particle, key, value) { return value; };
27+
28+
// The returned value updates the property for the duration of the particles life
29+
var particleUpdate = function (particle, key, t, value) { return value; };
30+
31+
var t = typeof(propertyValue);
32+
33+
if (t === 'number')
34+
{
35+
// Explicit static value:
36+
37+
// x: 400
38+
39+
particleEmit = function ()
40+
{
41+
return propertyValue;
42+
};
43+
44+
particleUpdate = function ()
45+
{
46+
return propertyValue;
47+
};
48+
}
49+
else if (Array.isArray(propertyValue))
50+
{
51+
// Picks a random element from the array:
52+
53+
// x: [ 100, 200, 300, 400 ]
54+
55+
particleEmit = function ()
56+
{
57+
var randomIndex = Math.floor(Math.random() * propertyValue.length);
58+
59+
return propertyValue[randomIndex];
60+
};
61+
}
62+
else if (t === 'function')
63+
{
64+
// The same as setting just the onUpdate function and no onEmit
65+
// Custom callback, must return a value:
66+
67+
/*
68+
x: function (particle, key, t, value)
69+
{
70+
return value + 50;
71+
}
72+
*/
73+
74+
particleUpdate = propertyValue;
75+
}
76+
else if (t === 'object' && propertyValue.hasOwnProperty('start') && propertyValue.hasOwnProperty('end'))
77+
{
78+
var start = propertyValue.start;
79+
var end = propertyValue.end;
80+
81+
// A random starting value:
82+
83+
// x: { start: 100, end: 400, randomStart: true }
84+
85+
if (propertyValue.hasOwnProperty('randomStart'))
86+
{
87+
particleEmit = function ()
88+
{
89+
return FloatBetween(start, end);
90+
};
91+
}
92+
else
93+
{
94+
particleEmit = function ()
95+
{
96+
return start;
97+
};
98+
}
99+
100+
if (propertyValue.hasOwnProperty('steps'))
101+
{
102+
// A stepped (per emit) range
103+
104+
// x: { start: 100, end: 400, steps: 64 }
105+
106+
// Increments a value stored in the emitter
107+
108+
particleUpdate = function (particle, key)
109+
{
110+
var emitter = particle.emitter;
111+
112+
return emitter.getNext(key);
113+
};
114+
}
115+
else
116+
{
117+
// An eased range (defaults to Linear if not specified)
118+
119+
// x: { start: 100, end: 400, [ ease: 'Linear' ] }
120+
121+
var ease = (propertyValue.hasOwnProperty('ease')) ? propertyValue.ease : 'Linear';
122+
123+
var easeFunc = GetEaseFunction(ease);
124+
125+
particleUpdate = function (particle, key, t, value)
126+
{
127+
var data = particle.data[key];
128+
129+
return data.calc * easeFunc(t) + data.min;
130+
}
131+
}
132+
}
133+
else if (t === 'object' && hasGetters(propertyValue))
134+
{
135+
// Custom onEmit and onUpdate callbacks
136+
137+
/*
138+
x: {
139+
// Called at the start of the particles life, when it is being created
140+
onEmit: function (particle, key, t, value)
141+
{
142+
return value;
143+
},
144+
145+
// Called during the particles life on each update
146+
onUpdate: function (particle, key, t, value)
147+
{
148+
return value;
149+
}
150+
}
151+
*/
152+
153+
if (hasOnEmit(propertyValue))
154+
{
155+
particleEmit = propertyValue.onEmit;
156+
}
157+
158+
if (hasOnUpdate(propertyValue))
159+
{
160+
particleUpdate = propertyValue.onUpdate;
161+
}
162+
}
163+
164+
return {
165+
onEmit: particleEmit,
166+
onUpdate: particleUpdate
167+
};
168+
};
169+
170+
module.exports = GetValueOp;

v3/src/gameobjects/particles/Particle.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ var Particle = new Class({
66

77
initialize:
88

9-
function Particle ()
9+
function Particle (emitter)
1010
{
11+
this.emitter = emitter;
12+
1113
// Phaser.Texture.Frame
1214
this.frame = null;
1315

@@ -54,24 +56,26 @@ var Particle = new Class({
5456
return (this.lifeCurrent > 0);
5557
},
5658

57-
emit: function (emitter)
59+
emit: function ()
5860
{
61+
var emitter = this.emitter;
62+
5963
this.frame = emitter.getFrame();
6064

6165
if (emitter.zone)
6266
{
6367
emitter.zone.getRandomPoint(this);
6468
}
6569

66-
this.x += emitter.x;
67-
this.y += emitter.y;
70+
this.x += emitter.x.getNext();
71+
this.y += emitter.y.getNext();
6872

69-
var sx = emitter.speed.getRandomX();
70-
var sy = emitter.speed.getRandomY();
73+
var sx = emitter.speedX.getNext();
74+
var sy = emitter.speedY.getNext();
7175

7276
if (emitter.radial)
7377
{
74-
var rad = DegToRad(emitter.emitterAngle.getRandom());
78+
var rad = DegToRad(emitter.emitterAngle.getNext());
7579

7680
this.velocityX = Math.cos(rad) * Math.abs(sx);
7781
this.velocityY = Math.sin(rad) * Math.abs(sy);
@@ -82,7 +86,7 @@ var Particle = new Class({
8286
this.velocityY = sy;
8387
}
8488

85-
this.life = emitter.lifespan.getRandom();
89+
this.life = emitter.lifespan.getNext();
8690
this.lifeCurrent = this.life;
8791

8892
// eased values
@@ -92,9 +96,9 @@ var Particle = new Class({
9296
var dataAngle = this.data.angle;
9397
var dataAlpha = this.data.alpha;
9498

95-
emitter.scale.copyXToMinMax(dataScaleX);
96-
emitter.scale.copyYToMinMax(dataScaleY);
97-
emitter.angle.copyToMinMax(dataAngle);
99+
emitter.scaleX.copyToMinMax(dataScaleX);
100+
emitter.scaleY.copyToMinMax(dataScaleY);
101+
emitter.particleAngle.copyToMinMax(dataAngle);
98102
emitter.alpha.copyToMinMax(dataAlpha);
99103

100104
// Random overrides
@@ -186,8 +190,10 @@ var Particle = new Class({
186190
},
187191

188192
// delta = ms, step = delta / 1000
189-
update: function (emitter, delta, step)
193+
update: function (delta, step)
190194
{
195+
var emitter = this.emitter;
196+
191197
// How far along in life is this particle? (t = 0 to 1)
192198
var t = 1 - (this.lifeCurrent / this.life);
193199

0 commit comments

Comments
 (0)