BlueGit

timfa frameworkv3

particleEmitter

timfa Tim · pushed by timfa

Authored 2022-10-20 · 4 years ago

Hash
360a61b78f42fb90a3b09ebe0f861be2eb68fc01
Parent
0665883f
2 files changed +126 −8
framework/util/particleEmitter.js +119 −7
@@ -1,10 +1,114 @@
1 1 class ParticleEmitter
2 2 {
3 3 static pool = [];
4 enabled = false;
5 mine = [];
6 position = null;
4 7
5 constructor()
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)
6 31 {
7
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);
8 112 }
9 113
10 114 static unPool() //get a particle from the static pool
@@ -28,6 +132,7 @@ class ParticleEmitter
28 132
29 133 class Particle
30 134 {
135 parent = null;
31 136 position = null;
32 137 rotation = 0;
33 138 velocity = null;
@@ -55,8 +160,9 @@ class Particle
55 160 this.endSize = new vector(0,0)
56 161 }
57 162
58 emit(x, y, r, vx, vy, va, ax, ay, ar, spr, ssx, ssy, esx, esy, lmin, lmax, fi = 0, fo = 0, as = 0)
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)
59 164 {
165 this.parent = parent;
60 166 this.position.x = x;
61 167 this.position.y = y;
62 168 this.rotation = r;
@@ -78,6 +184,7 @@ class Particle
78 184 this.fadeOutTime = fo;
79 185 this.animationSpeed = as;
80 186 this.animTimer = as;
187 this.subImage = sf;
81 188 }
82 189
83 190 update(deltaTime)
@@ -90,6 +197,8 @@ class Particle
90 197 this.rotation += this.angularVelocity;
91 198 this.angularVelocity += this.angularAcceleration;
92 199
200 this.lifeTime -= deltaTime;
201
93 202 if(this.fadeInTime > 0)
94 203 this.fadeInTime -= deltaTime;
95 204
@@ -100,23 +209,26 @@ class Particle
100 209 if(this.animTimer <= 0)
101 210 {
102 211 this.animTimer = this.animationSpeed;
103 this.subImage = this.subImage % this.sprite.subImages;
212 this.subImage = this.subImage + 1 % this.sprite.subImages;
104 213 }
105 214 }
215
216 if(this.lifeTime <= 0)
217 this.parent.dispose(this);
106 218 }
107 219
108 220 draw(scene)
109 221 {
110 222 let foAlpha = Math.min(1, this.lifeTime / this.fadeOutTime)
111 223
112 let fiAlpha = this.fadeInTime / this.fadeInStart;
224 let fiAlpha = 1 - (this.fadeInTime / this.fadeInStart);
113 225
114 226 this.sprite.draw(
115 227 scene,
116 228 Math.round(this.position.x),
117 229 Math.round(this.position.y),
118 Math.lerp(this.startSize.x, this.endSize.x, this.lifeTime / this.lifeTimeStart),
119 Math.lerp(this.startSize.y, this.endSize.y, this.lifeTime / this.lifeTimeStart),
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),
120 232 this.rotation,
121 233 false,
122 234 false,
@@ -1,10 +1,114 @@
1class ParticleEmitter
1class ParticleEmitter
2{
2{
3 static pool = [];
3 static pool = [];
4 enabled = false;
5 mine = [];
6 position = null;
4
7
5 constructor()
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)
6 {
31 {
7
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);
8 }
112 }
9
113
10 static unPool() //get a particle from the static pool
114 static unPool() //get a particle from the static pool
@@ -28,6 +132,7 @@ class ParticleEmitter
28
132
29class Particle
133class Particle
30{
134{
135 parent = null;
31 position = null;
136 position = null;
32 rotation = 0;
137 rotation = 0;
33 velocity = null;
138 velocity = null;
@@ -55,8 +160,9 @@ class Particle
55 this.endSize = new vector(0,0)
160 this.endSize = new vector(0,0)
56 }
161 }
57
162
58 emit(x, y, r, vx, vy, va, ax, ay, ar, spr, ssx, ssy, esx, esy, lmin, lmax, fi = 0, fo = 0, as = 0)
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)
59 {
164 {
165 this.parent = parent;
60 this.position.x = x;
166 this.position.x = x;
61 this.position.y = y;
167 this.position.y = y;
62 this.rotation = r;
168 this.rotation = r;
@@ -78,6 +184,7 @@ class Particle
78 this.fadeOutTime = fo;
184 this.fadeOutTime = fo;
79 this.animationSpeed = as;
185 this.animationSpeed = as;
80 this.animTimer = as;
186 this.animTimer = as;
187 this.subImage = sf;
81 }
188 }
82
189
83 update(deltaTime)
190 update(deltaTime)
@@ -90,6 +197,8 @@ class Particle
90 this.rotation += this.angularVelocity;
197 this.rotation += this.angularVelocity;
91 this.angularVelocity += this.angularAcceleration;
198 this.angularVelocity += this.angularAcceleration;
92
199
200 this.lifeTime -= deltaTime;
201
93 if(this.fadeInTime > 0)
202 if(this.fadeInTime > 0)
94 this.fadeInTime -= deltaTime;
203 this.fadeInTime -= deltaTime;
95
204
@@ -100,23 +209,26 @@ class Particle
100 if(this.animTimer <= 0)
209 if(this.animTimer <= 0)
101 {
210 {
102 this.animTimer = this.animationSpeed;
211 this.animTimer = this.animationSpeed;
103 this.subImage = this.subImage % this.sprite.subImages;
212 this.subImage = this.subImage + 1 % this.sprite.subImages;
104 }
213 }
105 }
214 }
215
216 if(this.lifeTime <= 0)
217 this.parent.dispose(this);
106 }
218 }
107
219
108 draw(scene)
220 draw(scene)
109 {
221 {
110 let foAlpha = Math.min(1, this.lifeTime / this.fadeOutTime)
222 let foAlpha = Math.min(1, this.lifeTime / this.fadeOutTime)
111
223
112 let fiAlpha = this.fadeInTime / this.fadeInStart;
224 let fiAlpha = 1 - (this.fadeInTime / this.fadeInStart);
113
225
114 this.sprite.draw(
226 this.sprite.draw(
115 scene,
227 scene,
116 Math.round(this.position.x),
228 Math.round(this.position.x),
117 Math.round(this.position.y),
229 Math.round(this.position.y),
118 Math.lerp(this.startSize.x, this.endSize.x, this.lifeTime / this.lifeTimeStart),
230 Math.lerp(this.endSize.x, this.startSize.x, this.lifeTime / this.lifeTimeStart),
119 Math.lerp(this.startSize.y, this.endSize.y, this.lifeTime / this.lifeTimeStart),
231 Math.lerp(this.endSize.y, this.startSize.y, this.lifeTime / this.lifeTimeStart),
120 this.rotation,
232 this.rotation,
121 false,
233 false,
122 false,
234 false,
framework/util/vector.js +7 −1
@@ -29,7 +29,13 @@ class vector
29 29 return angle;
30 30 }
31 31
32
32 static fromAngle(angle)
33 {
34 angle = 360 - ((angle + 180) % 360);
35 angle = (angle / 180) * Math.PI;
36 return new vector(Math.sin(angle), Math.cos(angle));
37 }
38
33 39 normalize()
34 40 {
35 41 var newVector = this.getCopy();
@@ -29,7 +29,13 @@ class vector
29 return angle;
29 return angle;
30 }
30 }
31
31
32
32 static fromAngle(angle)
33 {
34 angle = 360 - ((angle + 180) % 360);
35 angle = (angle / 180) * Math.PI;
36 return new vector(Math.sin(angle), Math.cos(angle));
37 }
38
33 normalize()
39 normalize()
34 {
40 {
35 var newVector = this.getCopy();
41 var newVector = this.getCopy();