Looking for help

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
LoneDesire
Prole
Posts: 1
Joined: Sat Jul 02, 2022 8:48 pm

Looking for help

Post 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
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Looking for help

Post by togFox »

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
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Looking for help

Post 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.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 1 guest