I am trying to simulate evolution and while Im waiting to learn a little more about AI I want to get the organisms to move towards the closest piece of food with the tag "Food".
I made this file to add onto the spawn of each organism so it will move towards food but it tries to move towards all of them at once
Javascript, Kaboom.js, Replit.com
// custom component controlling organim movement
export default function MoveToFood() {
return {
id: "MoveToFood",
require: ["pos", "area",],
add() {
//Start organism move towards food
onUpdate("Food", (f) => {
dir = f.pos.sub(this.pos).unit();
this.move(dir.scale(Speed));
})
this.onUpdate(() => {
Starve -= 1;
if (Starve <= 0) {
destroy(this)
}
})
},
}
}