-
Notifications
You must be signed in to change notification settings - Fork 0
/
particlesystem.js
40 lines (32 loc) · 990 Bytes
/
particlesystem.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
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Smoke Particle System
// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles
class ParticleSystem {
constructor(num, v, img) {
this.particles = []; // Initialize the arraylist
this.origin = v.copy(); // Store the origin point
this.img = img;
for (let i = 0; i < num; i++) {
this.particles.push(new Particle(this.origin, this.img)); // Add "num" amount of particles to the arraylist
}
}
run() {
for (let particle of this.particles) {
particle.run();
}
this.particles = this.particles.filter(particle => !particle.isDead());
}
// Method to add a force vector to all particles currently in the system
applyForce(dir) {
// Enhanced loop!!!
for (let particle of this.particles) {
particle.applyForce(dir);
}
}
addParticle() {
this.particles.push(new Particle(this.origin, this.img));
}
}