-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorld.js
60 lines (51 loc) · 1.42 KB
/
World.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
class World{
constructor(num){
this.food = new Food(num);
this.predators = [];
for (var i = 0; i < num/8; i++) {
let dna = new DNA();
this.predators.push(new Predator(createVector(random(width),random(height)),dna));
}
this.preys = [];
for (i = 0; i < num; i++) {
let dna = new DNA();
this.preys.push(new Prey(createVector(random(width),random(height)),dna));
}
}
born(x, y) {
l = new PVector(x,y);
dna = new DNA();
this.preys.push(new Prey(l,dna));
}
// Run the world
run() {
// Deal with food
this.food.run();
for (var i = this.predators.length-1; i >= 0; i--) {
let p = this.predators[i];
p.eat(this.preys);
p.run();
if (p.dead()) {
this.predators.splice(i, 1);
}
let child = p.reproduce();
if (child != null) this.predators.push(child);
}
// Cycle through the ArrayList backwards b/c we are deleting
for (let i = this.preys.length-1; i >= 0; i--) {
// All bloops run and eat
let p = this.preys[i];
p.run();
//b.run(predators);
p.eat(this.food);
// If it's dead, kill it and make food
if (p.dead()) {
this.preys.splice(i, 1);
this.food.add(p.position);
}
// Perhaps this bloop would like to make a baby?
let child = p.reproduce();
if (child != null) this.preys.push(child);
}
}
}