So I want these creatures to move towards food when it is in their vision, the food is inside of a table and randomly spawned.
Here is the code I used to move the creatures:
function moveCreatures(dt)
for index, creature in ipairs(creatures) do
dir = rand(1, creature.chanceToMove+4)
if dir == 1 and creature.x < gridXCount then
creature.x = creature.x + 1
creature.survive = creature.survive - creature.cost
elseif dir == 2 and creature.x > 2 then
creature.x = creature.x - 1
creature.survive = creature.survive - creature.cost
elseif dir == 3 and creature.y < gridYCount then
creature.y = creature.y + 1
creature.survive = creature.survive - creature.cost
elseif dir == 4 and creature.y > 1 then
creature.y = creature.y - 1
creature.survive = creature.survive - creature.cost
end
end
end
Looking for help
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Looking for help
So what is not happening? What is going wrong?
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
Re: Looking for help
In higher abstraction it looks like:
It looks like that you don't use the dt and the moving distance will be always 1.
Code: Select all
function moveCreatures (creatures, dt)
for index, creature in ipairs (creatures) do
local direction = chooseDirection (creature)
if canMoveInDirection (creature, direction, dt) then
moveInDirection (creature, direction, dt)
takeEnergy (creature, dt)
end
end
end
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 1 guest