Skip to content

Commit 4c61839

Browse files
committed
Working through updating the emitter and particle classes to use the new MinMax format
1 parent 4f1bd5a commit 4c61839

3 files changed

Lines changed: 212 additions & 97 deletions

File tree

v3/src/gameobjects/particles/Particle.js

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var Class = require('../../utils/Class');
2+
var DegToRad = require('../../math/DegToRad');
23

34
var Particle = new Class({
45

@@ -14,6 +15,8 @@ var Particle = new Class({
1415
this.x = x;
1516
this.y = y;
1617

18+
// Add Acceleration (and Bounce?)
19+
1720
this.velocityX = 0;
1821
this.velocityY = 0;
1922

@@ -35,14 +38,14 @@ var Particle = new Class({
3538
this.start = {
3639
tint: 0xFFFFFF,
3740
alpha: 1,
38-
scale: 1,
41+
scale: { x: 1, y: 1 },
3942
angle: 0
4043
};
4144

4245
this.end = {
4346
tint: 0xFFFFFF,
4447
alpha: 1,
45-
scale: 1,
48+
scale: { x: 1, y: 1 },
4649
angle: 0
4750
};
4851
},
@@ -74,14 +77,16 @@ var Particle = new Class({
7477

7578
start.tint = 0xFFFFFF;
7679
start.alpha = 1;
77-
start.scale = 1;
80+
start.scale.x = 1;
81+
start.scale.y = 1;
7882
start.angle = 0;
7983

8084
var end = this.end;
8185

8286
end.tint = 0xFFFFFF;
8387
end.alpha = 1;
84-
end.scale = 1;
88+
end.scale.x = 1;
89+
end.scale.y = 1;
8590
end.angle = 0;
8691

8792
return this;
@@ -92,19 +97,82 @@ var Particle = new Class({
9297
return (this.lifeStep > 0);
9398
},
9499

95-
/*
96-
setPosition: function (x, y)
100+
// var rad = DegToRad(Between(this.minEmitAngle, this.maxEmitAngle));
101+
// var speed = Between(this.minSpeed, this.maxSpeed);
102+
// var vx = Math.cos(rad) * speed;
103+
// var vy = Math.sin(rad) * speed;
104+
105+
// particle.velocityX = vx;
106+
// particle.velocityY = vy;
107+
// particle.life = Math.max(this.life, Number.MIN_VALUE);
108+
// particle.lifeStep = particle.life;
109+
// particle.start.scale = this.startScale;
110+
// particle.end.scale = this.endScale;
111+
// particle.scaleX = this.startScale;
112+
// particle.scaleY = this.startScale;
113+
// particle.start.alpha = this.startAlpha;
114+
// particle.end.alpha = this.endAlpha;
115+
// particle.start.rotation = DegToRad(this.startAngle);
116+
// particle.end.rotation = DegToRad(this.endAngle);
117+
// particle.color = (particle.color & 0x00FFFFFF) | (((this.startAlpha * 0xFF)|0) << 24);
118+
// particle.index = this.alive.length;
119+
120+
emit: function (emitter)
97121
{
98-
this.x = x;
99-
this.y = y;
122+
var rad = DegToRad(emitter.angle.getRandom());
123+
124+
this.velocityX = Math.cos(rad) * emitter.velocity.getRandomX();
125+
this.velocityY = Math.sin(rad) * emitter.velocity.getRandomY();
126+
127+
this.life = Math.max(this.life, Number.MIN_VALUE);
128+
this.lifeStep = this.life;
129+
130+
emitter.scale.copyX(this.start.scale);
131+
emitter.scale.copyY(this.end.scale);
132+
133+
this.start.alpha = emitter.alpha.min;
134+
this.end.alpha = emitter.alpha.max;
135+
136+
this.start.rotation = emitter.particleAngle.min;
137+
this.end.rotation = emitter.particleAngle.max;
138+
139+
this.color = (this.color & 0x00ffffff) | (((this.start.alpha * 0xff) | 0) << 24);
140+
141+
this.index = emitter.alive.length;
100142
},
101143

102-
setScale: function (x, y)
144+
// particle.velocityX += gravityX;
145+
// particle.velocityY += gravityY;
146+
// particle.x += particle.velocityX * emitterStep;
147+
// particle.y += particle.velocityY * emitterStep;
148+
// particle.normLifeStep = particle.lifeStep / particle.life;
149+
150+
// var norm = 1 - particle.normLifeStep;
151+
// var alphaEase = this.easingFunctionAlpha(norm);
152+
// var scaleEase = this.easingFunctionScale(norm);
153+
// var rotationEase = this.easingFunctionRotation(norm);
154+
// var alphaf = (particle.end.alpha - particle.start.alpha) * alphaEase + particle.start.alpha;
155+
// var scale = (particle.end.scale - particle.start.scale) * scaleEase + particle.start.scale;
156+
// var rotation = (particle.end.rotation - particle.start.rotation) * rotationEase + particle.start.rotation;
157+
158+
// particle.scaleX = particle.scaleY = scale;
159+
// particle.color = (particle.color & 0x00FFFFFF) | (((alphaf * 0xFF)|0) << 24);
160+
// particle.rotation = rotation;
161+
162+
update: function (emitter, step)
103163
{
104-
this.scaleX = x;
105-
this.scaleY = y;
164+
this.normLifeStep = 1 - this.lifeStep / this.life;
165+
166+
this.velocityX += (emitter.gravity.x * step);
167+
this.velocityY += (emitter.gravity.y * step);
168+
169+
this.x += this.velocityX * step;
170+
this.y += this.velocityY * step;
171+
172+
this.lifeStep -= step;
173+
174+
return (this.lifeStep <= 0);
106175
}
107-
*/
108176

109177
});
110178

0 commit comments

Comments
 (0)