8000 Moved ops to EmitterOp class and fixed loads of issues and added back… · ITCSsDeveloper/phaser@afc73e4 · GitHub
Skip to content

Commit afc73e4

Browse files
committed
Moved ops to EmitterOp class and fixed loads of issues and added back in all the setters
1 parent 816b228 commit afc73e4

3 files changed

Lines changed: 372 additions & 68 deletions

File tree

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
var Class = require('../../utils/Class');
2+
var FloatBetween = require('../../math/FloatBetween');
3+
var GetEaseFunction = require('../../tweens/builder/GetEaseFunction');
4+
var GetFastValue = require('../../utils/object/GetFastValue');
5+
var Wrap = require('../../math/Wrap');
6+
7+
var EmitterOp = new Class({
8+
9+
initialize:
10+
11+
function EmitterOp (config, key, defaultValue)
12+
{
13+
this.propertyKey = key;
14+
this.propertyValue = GetFastValue(config, key, defaultValue);
15+
this.defaultValue = defaultValue;
16+
17+
this.steps = 0;
18+
this.counter = 0;
19+
20+
this.start = 0;
21+
this.end = 0;
22+
this.ease;
23+
24+
this.onEmit = this.defaultEmit;
25+
this.onUpdate = this.defaultUpdate;
26+
27+
this.setMethods();
28+
},
29+
30+
onChange: function (value)
31+
{
32+
this.propertyValue = value;
33+
34+
return this.setMethods();
35+
},
36+
37+
setMethods: function ()
38+
{
39+
var value = this.propertyValue;
40+
41+
var t = typeof(value);
42+
43+
if (t === 'number')
44+
{
45+
// Explicit static value:
46+
// x: 400
47+
48+
this.onEmit = this.staticValueEmit;
49+
this.onUpdate = this.staticValueUpdate;
50+
}
51+
else if (Array.isArray(value))
52+
{
53+
// Picks a random element from the array:
54+
// x: [ 100, 200, 300, 400 ]
55+
56+
this.onEmit = this.randomStaticValueEmit;
57+
}
58+
else if (t === 'function')
59+
{
60+
// The same as setting just the onUpdate function and no onEmit
61+
// Custom callback, must return a value:
62+
63+
/*
64+
x: function (particle, key, t, value)
65+
{
66+
return value + 50;
67+
}
68+
*/
69+
70+
this.onUpdate = value;
71+
}
72+
else if (t === 'object' && (this.has(value, 'random') || this.hasBoth(value, 'start', 'end') || this.hasBoth(value, 'min', 'max')))
73+
{
74+
this.start = (this.has(value, 'start')) ? value.start : value.min;
75+
this.end = (this.has(value, 'end')) ? value.end : value.max;
76+
77+
var isRandom = (this.hasBoth(value, 'min', 'max') || this.has(value, 'random'));
78+
79+
// A random starting value (using 'min | max' instead of 'start | end' automatically implies a random value)
80+
81+
// x: { start: 100, end: 400, random: true } OR { min: 100, max: 400 } OR { random: [ 100, 400 ] }
82+
83+
if (isRandom)
84+
{
85+
var rnd = value.random;
86+
87+
// x: { random: [ 100, 400 ] } = the same as doing: x: { start: 100, end: 400, random: true }
88+
if (Array.isArray(rnd))
89+
{
90+
this.start = rnd[0];
91+
this.end = rnd[1];
92+
}
93+
94+
this.onEmit = this.randomRangedValueEmit;
95+
}
96+
97+
if (this.has(value, 'steps'))
98+
{
99+
// A stepped (per emit) range
100+
101+
// x: { start: 100, end: 400, steps: 64 }
102+
103+
// Increments a value stored in the emitter
104+
105+
this.steps = value.steps;
106+
this.counter = this.start;
107+
108+
this.onEmit = this.steppedEmit;
109+
}
110+
else
111+
{
112+
// An eased range (defaults to Linear if not specified)
113+
114+
// x: { start: 100, end: 400, [ ease: 'Linear' ] }
115+
116+
var easeType = (this.has(value, 'ease')) ? value.ease : 'Linear';
117+
118+
this.ease = GetEaseFunction(easeType);
119+
120+
if (!isRandom)
121+
{
122+
this.onEmit = this.easedValueEmit;
123+
}
124+
125+
this.onUpdate = this.easeValueUpdate;
126+
}
127+
}
128+
else if (t === 'object' && this.hasEither(value, 'onEmit', 'onUpdate'))
129+
{
130+
// Custom onEmit and onUpdate callbacks
131+
132+
/*
133+
x: {
134+
// Called at the start of the particles life, when it is being created
135+
onEmit: function (particle, key, t, value)
136+
{
137+
return value;
138+
},
139+
140+
// Called during the particles life on each update
141+
onUpdate: function (particle, key, t, value)
142+
{
143+
return value;
144+
}
145+
}
146+
*/
147+
148+
if (this.has(value, 'onEmit'))
149+
{
150+
this.onEmit = value.onEmit;
151+
}
152+
153+
if (this.has(value, 'onUpdate'))
154+
{
155+
this.onUpdate = value.onUpdate;
156+
}
157+
}
158+
159+
return this;
160+
},
161+
162+
has: function (object, key)
163+
{
164+
return (object.hasOwnProperty(key));
165+
},
166+
167+
hasBoth: function (object, key1, key2)
168+
{
169+
return (object.hasOwnProperty(key1) && object.hasOwnProperty(key2));
170+
},
171+
172+
hasEither: function (object, key1, key2)
173+
{
174+
return (object.hasOwnProperty(key1) || object.hasOwnProperty(key2));
175+
},
176+
177+
// The returned value sets what the property will be at the START of the particles life, on emit
178+
defaultEmit: function (particle, key, value)
179+
{
180+
return value;
181+
},
182+
183+
// The returned value updates the property for the duration of the particles life
184+
defaultUpdate: function (particle, key, t, value)
185+
{
186+
return value;
187+
},
188+
189+
staticValueEmit: function ()
190+
{
191+
return this.propertyValue;
192+
},
193+
194+
staticValueUpdate: function ()
195+
{
196+
return this.propertyValue;
197+
},
198+
199+
randomStaticValueEmit: function ()
200+
{
201+
var randomIndex = Math.floor(Math.random() * this.propertyValue.length);
202+
203+
return this.propertyValue[randomIndex];
204+
},
205+
206+
randomRangedValueEmit: function (particle, key)
207+
{
208+
var value = FloatBetween(this.start, this.end);
209+
210+
var data = particle.data[key];
211+
212+
if (data)
213+
{
214+
data.min = value;
215+
}
216+
217+
return value;
218+
},
219+
220+
steppedEmit: function ()
221+
{
222+
var next = this.counter + ((this.end - this.start) / this.steps);
223+
224+
this.counter = Wrap(next, this.start, this.end);
225+
226+
return this.counter;
227+
},
228+
229+
easedValueEmit: function (particle, key)
230+
{
231+
var data = particle.data[key];
232+
233+
data.min = this.start;
234+
data.max = this.end;
235+
236+
return this.start;
237+
},
238+
239+
easeValueUpdate: function (particle, key, t, value)
240+
{
241+
var data = particle.data[key];
242+
243+
return (data.max - data.min) * this.ease(t) + data.min;
244+
}
245+
246+
});
247+
248+
module.exports = EmitterOp;

v3/src/gameobjects/particles/Particle.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,45 @@ var Particle = new Class({
5555
return (this.lifeCurrent > 0);
5656
},
5757

58-
emit: function ()
58+
emit: function (x, y)
5959
{
6060
var emitter = this.emitter;
6161

6262
this.frame = emitter.getFrame();
6363

6464
if (emitter.zone)
6565
{
66+
// Updates particle.x and particle.y during this call
6667
emitter.zone.getRandomPoint(this);
6768
}
6869

69-
this.x += emitter.x.onEmit(this, 'x');
70-
this.y += emitter.y.onEmit(this, 'y');
70+
if (x === undefined)
71+
{
72+
if (emitter.follow)
73+
{
74+
this.x += emitter.follow.x + emitter.followOffset.x;
75+
}
76+
77+
this.x += emitter.x.onEmit(this, 'x');
78+
}
79+
else
80+
{
81+
this.x += x;
82+
}
83+
84+
if (y === undefined)
85+
{
86+
if (emitter.follow)
87+
{
88+
this.y += emitter.follow.y + emitter.followOffset.y;
89+
}
90+
91+
this.y += emitter.y.onEmit(this, 'y');
92+
}
93+
else
94+
{
95+
this.y += y;
96+
}
7197

7298
var sx = emitter.speedX.onEmit(this, 'speedX');
7399
var sy = (emitter.speedY) ? emitter.speedY.onEmit(this, 'speedY') : sx;

0 commit comments

Comments
 (0)