BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / util / particleEmitter.js

particleEmitter.js

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