-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.js
51 lines (44 loc) · 1.27 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
// Daniel Shiffman
// https://www.kadenze.com/courses/the-nature-of-code
// http://natureofcode.com/
// Session 5: Evolutionary Computing
// Evolution EcoSystem
// The World we live in
// Has bloops and food
// Constructor
function World(num) {
// Start with initial food and creatures
this.food = new Food(num);
this.bloops = []; // An array for all creatures
for (var i = 0; i < num; i++) {
var l = createVector(random(width), random(height));
var dna = new DNA();
this.bloops.push(new Bloop(l, dna));
}
// Make a new creature
this.born = function(x, y) {
var l = createVector(x, y);
var dna = new DNA();
this.bloops.push(new Bloop(l, dna));
}
// Run the world
this.run = function() {
// Deal with food
this.food.run();
// Cycle through the ArrayList backwards b/c we are deleting
for (var i = this.bloops.length-1; i >= 0; i--) {
// All bloops run and eat
var b = this.bloops[i];
b.run();
b.eat(this.food);
// If it's dead, kill it and make food
if (b.dead()) {
this.bloops.splice(i, 1);
this.food.add(b.position);
}
// Perhaps this bloop would like to make a baby?
var child = b.reproduce();
if (child != null) this.bloops.push(child);
}
}
}