xxxxxxxxxx
111
let gridSize = 20;
let creatureSize = 5;
let insects = [];
let numCols;
let numRows;
let initInsects = 10;
let maxInsects = 100;
let food = [];
let initFood = 30;
let foodAmount = 10;
let minFoodAmount = 1;
let trails = [];
let home;
function setup() {
createCanvas(600, 600);
background(0);
frameRate(120);
numCols = floor(width / gridSize);
numRows = floor(height / gridSize);
//create the trail grid
for (let i = 0; i < numCols; i++) {
trails[i] = [];
for (let j = 0; j < numRows; j++) {
//each grid cell is one trail
trails[i][j] = null;
}
}
//create the home
home = new Home();
//create the gravity
gravity = createVector(0, 0);
//create the insects
for (let i = 0; i < initInsects; i++) {
insects.push(new Insect(home.x, home.y, creatureSize));
}
//create the food
for (let i = 0; i < initFood; i++) {
let x = random(width);
let y = random(height);
food.push(new Food(x, y, foodAmount));
}
}
function draw() {
background(0, 30);
//draw the trails
for (let i = 0; i < numCols; i++) {
for (let j = 0; j < numRows; j++) {
if (trails[i][j] == null) {
continue;
} else if (trails[i][j] != null && trails[i][j].life <= 0) {
trails[i][j] = null;
} else {
trails[i][j].draw();
}
}
}
//update and draw the insects
for (let i = 0; i < insects.length; i++) {
insects[i].update();
insects[i].draw();
}
//draw the food
for (let i = 0; i < food.length; i++) {
food[i].draw();
if (food[i].amount <= minFoodAmount) {
//remove the food
food.splice(i, 1);
// add new food somewhere else
let x = random(width);
let y = random(height);
food.push(new Food(x, y, foodAmount));
}
}
//draw the home
home.draw();
//add more insects if key is pressed
if (mouseIsPressed) {
if (insects.length >= maxInsects) {
return;
}
//get the grid position
let x = floor(mouseX / gridSize) * gridSize;
let y = floor(mouseY / gridSize) * gridSize;
//add a creature
let c = new Insect(x, y, creatureSize);
insects.push(c);
}
}