-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflock.js
More file actions
40 lines (34 loc) · 959 Bytes
/
flock.js
File metadata and controls
40 lines (34 loc) · 959 Bytes
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
// Flock object
// Does very little, simply manages the array of all the boids
class Flock {
// An array for all the boids
constructor() {
this.boids = new Array();
this.kd = new KDTree([]);
for (let i = 0; i < 150; i++) {
this.addBoid(new Boid(random() * width, random() * height));
}
}
run(obstacles) {
for (let i = 0; i < this.boids.length; i++) {
let neighbors = this.kd.getNeighbors(this.boids[i]);
this.boids[i].flock(neighbors, obstacles); // Passing the entire list of boids to each boid individually
}
}
update() {
this.kd = new KDTree(this.boids);
for (let i = 0; i < this.boids.length; i++) {
this.boids[i].update(); // Passing the entire list of boids to each boid individually
}
if (debugMode) {
this.kd.draw();
}
}
addBoid(b) {
this.boids.push(b);
this.kd.insert(b);
}
}