-
Notifications
You must be signed in to change notification settings - Fork 2
/
ParticleManager.js
104 lines (89 loc) · 3.33 KB
/
ParticleManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var ParticleManager = function() {};
/**
* Initialize/Construction method - Initializes a blank particle manager
*
* @param Number count: The number of particles to initialize
*
* @return ParticleManager: Returns itself
*/
ParticleManager.prototype.initialize = function(count) {
this.particles = [];
this.particleCount = count;
this.deadCount = 0;
this.active = true;
this.particleColors = {
blue: { r: 77, g: 109, b: 243, a: 1 },
yellow: { r: 255, g: 242, b: 0, a: 1 },
red: { r: 238, g: 28, b: 36, a: 1 },
pink: { r: 255, g: 163, b: 177, a: 1 },
purple: { r: 111, g: 49, b: 152, a: 1 },
green: { r: 34, g: 177, b: 76, a: 1 },
white: { r: 255, g: 255, b: 255, a: 1}
};
this.gravity = .3;
this.tolerance = 0.0009;
return this;
};
/**
* Create a particle explosion at origin. Sets the particles' colors, accelerations and directions
*
* @param Object origin: An object with x and y values representing the epicenter of the particle explosion
* @param String type: The type of the enemy exploding
*
* @return ParticleManager: Returns itself
*/
ParticleManager.prototype.create = function(origin, type) {
var colors = [];
if (type === 'hank') {
colors = [this.particleColors.blue, this.particleColors.red, this.particleColors.yellow];
}
if (type === 'dean') {
colors = [this.particleColors.blue, this.particleColors.red, this.particleColors.pink];
}
if (type === 'brock') {
colors = [this.particleColors.green, this.particleColors.yellow, this.particleColors.red];
}
if (type == 'player') {
colors = [this.particleColors.white, this.particleColors.red, this.particleColors.white];
}
for (var i = 0; i < this.particleCount; i++) {
var particle = new Particle();
particle.fill = colors[Math.floor(Math.random() * 3)]
particle.position.x = origin.x;
particle.position.y = origin.y;
particle.age = 0;
particle.verticalBounces = 0;
particle.mass = Math.max(0.8, Math.random())
particle.angle = (360 / this.particleCount) * i;
particle.ageRate = (Math.floor(Math.random() * 3));
particle.ageLimit = 70.0;
particle.acceleration.x = (Math.random() * 100) / 7;
particle.acceleration.y = (Math.random() * 100) / 8;
if (particle.angle >= 90 && particle.angle < 180) {
particle.acceleration.y = -particle.acceleration.y;
} else if (particle.angle >= 180 && particle.angle < 270) {
particle.acceleration.y = -particle.acceleration.y;
particle.acceleration.x = -particle.acceleration.x;
} else if (particle.angle >= 270 && particle.angle < 360) {
particle.acceleration.x = -particle.acceleration.x;
}
particle.alive = true;
this.particles.push(particle);
}
return this;
}
/**
* Update all the particles based on the time scalar
*
* @param Number timeScalar: Time since last frame adjusted by game engine
*/
ParticleManager.prototype.update = function(timeScalar) {
for (var i = 0; i < this.particleCount; i++ ) {
// if a large portion of the particles are dead, deactivate this manager
if (this.deadCount && this.particleCount/this.deadCount > .95) { this.active = false; break; }
if (!this.particles[i].alive) { this.deadCount++; continue; }
var particle = this.particles[i];
particle.acceleration.y -= this.gravity * particle.mass * timeScalar;
particle.update(timeScalar);
}
};