Phase 1
sketch.js
let agents = [];
class Agent {
constructor(pos, vel) {
this.pos = pos;
this.vel = vel;
this.matureAge = random(5, 10);
this.age = 0;
this.isMature = false;
this.isAlive = true;
this.isParent = false;
}
tick() {
this.age = this.age + 1;
if (!this.isParent) {
this.isMature = this.age > this.matureAge;
}
if (this.isAlive) {
this.isAlive = this.age < 50;
}
this.pos.add(this.vel);
// const colors = get(this.x, this.y);
// if (colors[3] === 255) {
// this.isAlive = false;
// }
}
giveBirth() {
this.isParent = true;
this.isMature = false;
const direction = random([-1, 1]);
const vel = this.vel.copy().rotate(direction * (HALF_PI / 2));
// this.isAlive = false;
return new Agent(this.pos.copy(), vel);
}
draw() {
if (this.isAlive) {
fill('black');
circle(this.pos.x, this.pos.y, 2);
}
}
}
function setup() {
createCanvas(windowWidth, windowHeight);
noStroke();
const pos = createVector(windowWidth / 2, windowHeight / 2);
const vel = createVector(0, 1);
agents = [new Agent(pos, vel)];
}
function draw() {
if (!agents.length) {
return;
}
agents.forEach((a) => {
a.tick();
});
agents = agents.filter((a) => a.isAlive);
readyAgents = agents.filter((a) => a.isMature);
readyAgents.forEach((a) => {
if (agents.length < 50) {
if (a.isMature) {
const baby = a.giveBirth();
agents.push(baby);
}
}
});
console.log(agents.map((a) => a.age));
agents.forEach((a) => a.draw());
}
function keyPressed() {
frameRate(0);
}