xxxxxxxxxx
let foodX;
let foodY;
let waterX;
let waterY;
function setup() {
createCanvas(windowWidth, windowHeight);
background(0);
foodX = windowWidth-windowWidth/3-100;
foodY = windowHeight/2;
waterX = (windowWidth/3);
waterY = windowHeight/2;
}
let start = true;
let p1;
let times = 100;
let population = [times];
let tickSpeed = 5;
let tickCounter = 0;
let full = 500;
function draw() {
//Particle Class
class Particle {
constructor(color, age, hLevel, tLevel, speed, position)
{
this.size = age/3;
this.x = random(windowWidth/2-100, windowWidth/2+100);
this.y = random(windowHeight/2-100, windowHeight/2+100);
this.R = random(0,10);
this.G = random(0,10);
this.B = random(0,10);
this.thirst = full;
this.hunger = full;
this.appetite = hLevel;
this.quenchness = tLevel;
this.age = age;
this.speed = speed;
this.pos = position;
this.startSpeed = this.speed;
if (color == "R")
{
this.R = random(100,255);
}else if (color == "B")
{
this.B = random(100,255);
}else if (color == "G")
{
this.G = random(100,255);
}else if (color == "W")
{
this.R = random(200, 255);
this.G = random(200, 255);
this.B = random(200, 255);
}
}
move()
{
//Regular Random Movement
let dX = random(-this.speed, this.speed);
let dY = random(-this.speed, this.speed);
//If they are hungry move towards food
if (this.hunger <= 100)
{
if (this.x >= foodX)
{
dX = random(-this.speed, 0);
}else if (this.x <= foodX)
{
dX = random(0, this.speed);
}
if(this.y >= foodY)
{
dY = random(-this.speed, 0);
}else if (this.y <= foodY)
{
dY = random(0, this.speed);
}
}
//If they are thirsty move towards water
if (this.thirst <= 100)
{
if (this.x >= waterX)
{
dX = random(-this.speed, 0);
}else if (this.x <= waterX)
{
dX = random(0, this.speed);
}
if(this.y >= waterY)
{
dY = random(-this.speed, 0);
}else if (this.y <= waterY)
{
dY = random(0, this.speed);
}
}
if (this.x >= windowWidth)
{
this.x = 1;
}else if (this.x <= 0)
{
this.x = windowWidth-1;
}
if (this.y >= windowHeight)
{
this.y = 1;
}else if (this.y <= 0)
{
this.y = windowHeight-1;
}
fill(this.R, this.G, this.B);
noStroke();
smooth();
rect(this.x+dX, this.y+dY, this.size, this.size);
this.x+=dX;
this.y+=dY;
}
update()
{
this.thirst -= this.quenchness;
this.hunger -= this.appetite;
this.eat();
this.drink();
this.die();
this.age += 0.05;
this.size = this.age/3;
//Speed decreases with age
if (this.age >= 50)
{
this.speed = this.startSpeed/(this.age/50);
}
}
eat()
{
let disX = foodX-this.x;
let disY = foodY-this.y;
if (disX > -100 && disX < 10)
{
if (disY > -100 && disY < 10)
{
this.hunger = full;
}
}
}
drink()
{
let disX = waterX-this.x;
let disY = waterY-this.y;
if (disX > -10 && disX < 100)
{
if (disY > -100 && disY < 10)
{
this.thirst = full;
}
}
}
die()
{
if (this.hunger <= -full)
{
population[this.position] = population[times-1];
population[times-1] = null;
times--;
}else if (this.thirst <= -full)
{
population[this.position] = population[times-1];
population[times-1] = null;
times--;
}
}
}
if (start)
{
start=false;
for (let i = 0; i < times; i++)
{
p1 = new Particle("W", random(15, 20), random(1, 3), random(1, 3), random(3, 10), i);
population[i] = p1;
population[i].move();
}
}
//Population movement
if (tickCounter == tickSpeed && times != 0)
{
background(0);
tickCounter = 0;
fill(10, 255, 10);
rect(foodX, foodY, 100, 100);
fill(10, 255, 10);
textFont('Anton');
textAlign(CENTER);
textSize(40);
text("Food", foodX+50, foodY-50);
fill(10, 10, 255);
rect(waterX, waterY, 100, 100);
fill(10, 10, 255);
textFont('Anton');
textAlign(CENTER);
textSize(40);
text("Water", waterX+50, waterY-50);
for (let i = 0; i < times; i++)
{
population[i].move();
population[i].update();
}
}else {
tickCounter++;
}
}