Page 1 of 1

Looking for help

Posted: Sat Jul 30, 2022 3:47 pm
by LoneDesire
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

Re: Looking for help

Posted: Sun Jul 31, 2022 2:41 am
by togFox
So what is not happening? What is going wrong?

Re: Looking for help

Posted: Sun Jul 31, 2022 11:29 am
by darkfrei
In higher abstraction it looks like:

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
It looks like that you don't use the dt and the moving distance will be always 1.