BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / util / particleEmitter.js

particleEmitter.js

particleEmitter.js 6.0 KiB Download Raw
1class ParticleEmitter
2{
3 static pool = [];
4 enabled = false;
5 mine = [];
6 position = null;
7
8 emissionRate = 0;
9 emissionTimer = 0;
10 emissionAngle = 0;
11
12 startRotation = 0;
13 startVelocity = 0;
14 startAngularVelocity = 0;
15 acceleration = null;
16 angularAcceleration = 0;
17 sprite = null;
18 startSize = null;
19 endSize = null;
20 lifetimeMin = 0;
21 lifetimeMax = 0;
22 fadeIn = 0;
23 fadeOut = 0;
24 animationSpeed = 0;
25 startFrame = 0;
26
27 friction = null;
28 angularFriction = 0;
29
30 constructor (position, sprite, emissionRate, emissionAngle,
31 startRotation, startVelocity, startAngularVelocity, acceleration,
32 angularAcceleration, startSize, endSize, lifetimeMin,
33 lifetimeMax = -1, fadeIn = 0, fadeOut = 0, startFrame = 0, animationSpeed = 0)
34 {
35 if (lifetimeMax < 0)
36 { lifetimeMax = lifetimeMin; }
37
38 this.position = position;
39 this.sprite = sprite;
40
41 this.emissionRate = emissionRate;
42 this.emissionTimer = emissionRate;
43 this.emissionAngle = emissionAngle;
44
45 this.startRotation = startRotation;
46 this.startVelocity = startVelocity * 10;
47 this.startAngularVelocity = startAngularVelocity * 10;
48 this.acceleration = acceleration.stretch(10);
49 this.angularAcceleration = angularAcceleration * 10;
50 this.startSize = startSize;
51 this.endSize = endSize;
52 this.lifetimeMin = lifetimeMin;
53 this.lifetimeMax = lifetimeMax;
54 this.fadeIn = fadeIn;
55 this.fadeOut = fadeOut;
56 this.startFrame = startFrame;
57 this.animationSpeed = animationSpeed;
58
59 this.friction = new vector(0, 0);
60 }
61
62 start ()
63 {
64 this.enabled = true;
65 }
66
67 stop ()
68 {
69 this.enabled = false;
70 }
71
72 emit (amount = 1)
73 {
74 for (let i = 0; i < amount; i++)
75 {
76 const newParticle = ParticleEmitter.unPool();
77 this.mine.push(newParticle);
78
79 const velocity = vector.fromAngle(this.emissionAngle).stretch(this.startVelocity);
80 newParticle.emit(this, this.position.x, this.position.y, this.startRotation,
81 velocity.x, velocity.y, this.startAngularVelocity, this.acceleration.x,
82 this.acceleration.y, this.angularAcceleration, this.sprite, this.startSize.x,
83 this.startSize.y, this.endSize.x, this.endSize.y, this.lifetimeMin,
84 this.lifetimeMax, this.fadeIn, this.fadeOut, this.startFrame, this.animationSpeed,
85 this.friction.getCopy(), this.angularFriction);
86 }
87 }
88
89 update (deltaTime)
90 {
91 if (this.enabled)
92 { this.emissionTimer -= deltaTime; }
93
94 if (this.emissionTimer <= 0)
95 {
96 this.emissionTimer = this.emissionRate;
97 this.emit();
98 }
99
100 // Backwards so dispose()/remove during update cannot skip particles.
101 for (let i = this.mine.length - 1; i >= 0; i--)
102 {
103 this.mine[i].update(deltaTime);
104 }
105 }
106
107 draw (scene)
108 {
109 for (let i = 0; i < this.mine.length; i++)
110 {
111 this.mine[i].draw(scene);
112 }
113 }
114
115 dispose (particle)
116 {
117 this.mine.remove(particle);
118 ParticleEmitter.returnToPool(particle);
119 }
120
121 static unPool () //get a particle from the static pool
122 {
123 if (ParticleEmitter.pool.length > 0)
124 {
125 return ParticleEmitter.pool.pop();
126 }
127
128 return new Particle();
129 }
130
131 static returnToPool (particle)
132 {
133 if (!ParticleEmitter.pool.contains(particle))
134 {
135 ParticleEmitter.pool.push(particle);
136 }
137 }
138}
139
140class Particle
141{
142 parent = null;
143 position = null;
144 rotation = 0;
145 velocity = null;
146 angularVelocity = 0;
147 acceleration = null;
148 angularAcceleration = 0;
149 sprite = null;
150 animTimer = 0;
151 animationSpeed = 0;
152 subImage = 0;
153 startSize = null;
154 endSize = null;
155 fadeInTime = 0;
156 fadeInStart = 0;
157 fadeOutTime = 0;
158 lifeTime = 0;
159 lifeTimeStart = 0;
160 friction = null;
161 angularfriction = 0;
162
163 constructor ()
164 {
165 this.position = new vector(0, 0);
166 this.velocity = new vector(0, 0);
167 this.acceleration = new vector(0, 0);
168 this.startSize = new vector(0, 0);
169 this.endSize = new vector(0, 0);
170 }
171
172 emit (parent, x, y, r, vx, vy, va, ax, ay, ar, spr, ssx, ssy, esx, esy, lmin, lmax, fi = 0, fo = 0, sf = 0, as = 0, fric, anfric)
173 {
174 this.parent = parent;
175 this.position.x = x;
176 this.position.y = y;
177 this.rotation = r;
178 this.velocity.x = vx;
179 this.velocity.y = vy;
180 this.angularVelocity = va;
181 this.acceleration.x = ax;
182 this.acceleration.y = ay;
183 this.angularAcceleration = ar;
184 this.sprite = spr;
185 this.startSize.x = ssx;
186 this.startSize.y = ssy;
187 this.endSize.x = esx;
188 this.endSize.y = esy;
189 this.lifeTime = Math.randomRange(lmin, lmax);
190 this.lifeTimeStart = this.lifeTime;
191 this.fadeInTime = fi;
192 this.fadeInStart = fi;
193 this.fadeOutTime = fo;
194 this.animationSpeed = as;
195 this.animTimer = as;
196 this.subImage = sf;
197 this.friction = fric ? fric.getCopy() : new vector(0, 0);
198 this.angularFriction = anfric;
199 }
200
201 update (deltaTime)
202 {
203 this.position.x += this.velocity.x * deltaTime;
204 this.position.y += this.velocity.y * deltaTime;
205 this.velocity.x += this.acceleration.x * deltaTime;
206 this.velocity.y += this.acceleration.y * deltaTime;
207
208 this.velocity.x += this.friction.x * deltaTime * -Math.sign(this.velocity.x * 10);
209 this.velocity.y += this.friction.y * deltaTime * -Math.sign(this.velocity.y * 10);
210
211 this.rotation += this.angularVelocity * deltaTime;
212 this.angularVelocity += this.angularAcceleration * deltaTime;
213
214 this.angularVelocity += this.angularFriction * deltaTime * -Math.sign(this.angularVelocity * 10);
215
216 this.lifeTime -= deltaTime;
217
218 if (this.fadeInTime > 0)
219 { this.fadeInTime -= deltaTime; }
220
221 if (this.animationSpeed > 0)
222 {
223 this.animTimer -= deltaTime;
224
225 if (this.animTimer <= 0)
226 {
227 this.animTimer = this.animationSpeed;
228 this.subImage = (this.subImage + 1) % this.sprite.subImages;
229 }
230 }
231
232 if (this.lifeTime <= 0)
233 { this.parent.dispose(this); }
234 }
235
236 draw (scene)
237 {
238 const foAlpha = this.fadeOutTime > 0
239 ? Math.min(1, this.lifeTime / this.fadeOutTime)
240 : 1;
241
242 const fiAlpha = this.fadeInStart > 0
243 ? 1 - (this.fadeInTime / this.fadeInStart)
244 : 1;
245
246 this.sprite.draw(
247 scene,
248 Math.round(this.position.x),
249 Math.round(this.position.y),
250 Math.lerp(this.endSize.x, this.startSize.x, this.lifeTime / this.lifeTimeStart),
251 Math.lerp(this.endSize.y, this.startSize.y, this.lifeTime / this.lifeTimeStart),
252 this.rotation,
253 false,
254 false,
255 Math.min(fiAlpha, foAlpha),
256 this.subImage
257 );
258 }
259}