particles
@@ -41,6 +41,11 @@ Math.randomRange = function(min, max)
41
41
return min + (Math.random() * (max - min));
42
42
}
43
43
44
Math.lerp = function(a, b, t)
45
{
46
return a + (b - a) * t;
47
}
48
44
49
Array.prototype.random = function ()
45
50
{
46
51
return this[Math.floor((Math.random()*this.length))];
@@ -41,6 +41,11 @@ Math.randomRange = function(min, max)41
return min + (Math.random() * (max - min));41
return min + (Math.random() * (max - min));42
}42
}43
43
44
Math.lerp = function(a, b, t)45
{46
return a + (b - a) * t;47
}48
44
Array.prototype.random = function () 49
Array.prototype.random = function () 45
{50
{46
return this[Math.floor((Math.random()*this.length))];51
return this[Math.floor((Math.random()*this.length))];@@ -0,0 +1,127 @@
1
class ParticleEmitter
2
{
3
static pool = [];
4
5
constructor()
6
{
7
8
}
9
10
static unPool() //get a particle from the static pool
11
{
12
if(ParticleEmitter.pool.length > 0)
13
{
14
return ParticleEmitter.pool.pop()
15
}
16
17
return new Particle();
18
}
19
20
static returnToPool(particle)
21
{
22
if(!ParticleEmitter.pool.contains(particle))
23
{
24
ParticleEmitter.pool.push(particle);
25
}
26
}
27
}
28
29
class Particle
30
{
31
position = null;
32
rotation = 0;
33
velocity = null;
34
angularVelocity = 0;
35
acceleration = null;
36
angularAcceleration = 0;
37
sprite = null;
38
animTimer = 0;
39
animationSpeed = 0;
40
subImage = 0;
41
startSize = null
42
endSize = null;
43
fadeInTime = 0;
44
fadeInStart = 0;
45
fadeOutTime = 0;
46
lifeTime = 0;
47
lifeTimeStart = 0;
48
49
constructor()
50
{
51
this.position = new vector(0,0);
52
this.velocity = new vector(0,0);
53
this.acceleration = new vector(0,0)
54
this.startSize = new vector(0,0)
55
this.endSize = new vector(0,0)
56
}
57
58
emit(x, y, r, vx, vy, va, ax, ay, ar, spr, ssx, ssy, esx, esy, lmin, lmax, fi = 0, fo = 0, as = 0)
59
{
60
this.position.x = x;
61
this.position.y = y;
62
this.rotation = r;
63
this.velocity.x = vx;
64
this.velocity.y = vy;
65
this.angularVelocity = va;
66
this.acceleration.x = ax;
67
this.acceleration.y = ay;
68
this.angularAcceleration = ar;
69
this.sprite = spr;
70
this.startSize.x = ssx;
71
this.startSize.y = ssy;
72
this.endSize.x = esx;
73
this.endSize.y = esy;
74
this.lifeTime = Math.randomRange(lmin, lmax);
75
this.lifeTimeStart = this.lifeTime;
76
this.fadeInTime = fi;
77
this.fadeInStart = fi;
78
this.fadeOutTime = fo;
79
this.animationSpeed = as;
80
this.animTimer = as;
81
}
82
83
update(deltaTime)
84
{
85
this.position.x += this.velocity.x;
86
this.position.y += this.velocity.y;
87
this.velocity.x += this.acceleration.x;
88
this.velocity.y += this.acceleration.y;
89
90
this.rotation += this.angularVelocity;
91
this.angularVelocity += this.angularAcceleration;
92
93
if(this.fadeInTime > 0)
94
this.fadeInTime -= deltaTime;
95
96
if(this.animationSpeed > 0)
97
{
98
this.animTimer -= deltaTime;
99
100
if(this.animTimer <= 0)
101
{
102
this.animTimer = this.animationSpeed;
103
this.subImage = this.subImage % this.sprite.subImages;
104
}
105
}
106
}
107
108
draw(scene)
109
{
110
let foAlpha = Math.min(1, this.lifeTime / this.fadeOutTime)
111
112
let fiAlpha = this.fadeInTime / this.fadeInStart;
113
114
this.sprite.draw(
115
scene,
116
Math.round(this.position.x),
117
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),
120
this.rotation,
121
false,
122
false,
123
Math.min(fiAlpha, foAlpha),
124
this.subImage
125
);
126
}
127
}
@@ -0,0 +1,127 @@1
class ParticleEmitter2
{3
static pool = [];4
5
constructor()6
{7
8
}9
10
static unPool() //get a particle from the static pool11
{12
if(ParticleEmitter.pool.length > 0)13
{14
return ParticleEmitter.pool.pop()15
}16
17
return new Particle();18
}19
20
static returnToPool(particle)21
{22
if(!ParticleEmitter.pool.contains(particle))23
{24
ParticleEmitter.pool.push(particle);25
}26
}27
}28
29
class Particle30
{31
position = null;32
rotation = 0;33
velocity = null;34
angularVelocity = 0;35
acceleration = null;36
angularAcceleration = 0;37
sprite = null;38
animTimer = 0;39
animationSpeed = 0;40
subImage = 0;41
startSize = null42
endSize = null;43
fadeInTime = 0;44
fadeInStart = 0;45
fadeOutTime = 0;46
lifeTime = 0;47
lifeTimeStart = 0;48
49
constructor()50
{51
this.position = new vector(0,0);52
this.velocity = new vector(0,0);53
this.acceleration = new vector(0,0)54
this.startSize = new vector(0,0)55
this.endSize = new vector(0,0)56
}57
58
emit(x, y, r, vx, vy, va, ax, ay, ar, spr, ssx, ssy, esx, esy, lmin, lmax, fi = 0, fo = 0, as = 0)59
{60
this.position.x = x;61
this.position.y = y;62
this.rotation = r;63
this.velocity.x = vx;64
this.velocity.y = vy;65
this.angularVelocity = va;66
this.acceleration.x = ax;67
this.acceleration.y = ay;68
this.angularAcceleration = ar;69
this.sprite = spr;70
this.startSize.x = ssx;71
this.startSize.y = ssy;72
this.endSize.x = esx;73
this.endSize.y = esy;74
this.lifeTime = Math.randomRange(lmin, lmax);75
this.lifeTimeStart = this.lifeTime;76
this.fadeInTime = fi;77
this.fadeInStart = fi;78
this.fadeOutTime = fo;79
this.animationSpeed = as;80
this.animTimer = as;81
}82
83
update(deltaTime)84
{85
this.position.x += this.velocity.x;86
this.position.y += this.velocity.y;87
this.velocity.x += this.acceleration.x;88
this.velocity.y += this.acceleration.y;89
90
this.rotation += this.angularVelocity;91
this.angularVelocity += this.angularAcceleration;92
93
if(this.fadeInTime > 0)94
this.fadeInTime -= deltaTime;95
96
if(this.animationSpeed > 0)97
{98
this.animTimer -= deltaTime;99
100
if(this.animTimer <= 0)101
{102
this.animTimer = this.animationSpeed;103
this.subImage = this.subImage % this.sprite.subImages;104
}105
}106
}107
108
draw(scene)109
{110
let foAlpha = Math.min(1, this.lifeTime / this.fadeOutTime)111
112
let fiAlpha = this.fadeInTime / this.fadeInStart;113
114
this.sprite.draw(115
scene,116
Math.round(this.position.x), 117
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), 120
this.rotation, 121
false,122
false,123
Math.min(fiAlpha, foAlpha),124
this.subImage125
);126
}127
}