Help implementing game logic from fish fillets

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: Help implementing game logic from fish fillets

Post by darkfrei »

Added map tiles and rough collision between agent and map; blocks and map.

Attachments
push-blocks-03.love
(2.83 KiB) Downloaded 89 times
Last edited by darkfrei on Fri Apr 28, 2023 1:19 pm, edited 1 time in total.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
glitchapp
Party member
Posts: 237
Joined: Tue Oct 05, 2021 10:34 am
Contact:

Re: Help implementing game logic from fish fillets

Post by glitchapp »

Implemented!, I think this is useful once I'm not in the debug phase so that the walls do not blink with red or green color when something collide with them, but for the time being I like the the walls blinking, it helps me visualize where the collision borders are. To use it in my game of course I have to deactivate the random map because it makes no sense inside the puzzle. Thanks again for the help, I upload the implemented random generation walls map just to show that it works, but it will be disabled in the future as it break the puzzle and would make the game unplayable.

Just to make sure I'm understood here is the code I will comment in the future:

Code: Select all

for i = 1, 30 do
	local x = math.random(pb.grigWidth)
	local y = math.random(pb.grigHeight)
	--createMapTile (pb.staticTilesMap, y, x)
end

-- horizontal border
for x = 1, pb.grigWidth do
	local y1 = 1
	local y2 = pb.grigHeight
	--createMapTile (pb.staticTilesMap, y1, x)
	--createMapTile (pb.staticTilesMap, y2, x)
end

-- vertical border
for y = 1, pb.grigHeight do
	if not pb.staticTilesMap[y] then pb.staticTilesMap[y] = {} end
	local x1 = 1
	local x2 = pb.grigWidth
	--createMapTile (pb.staticTilesMap, y, x1)
	--createMapTile (pb.staticTilesMap, y, x2)
end
I uploaded with and without the random generation walls.

I use the chance to ask help to solve the problem of the fish moving outside the grid, how can I make it stops every 4 cycles or when the coordinate is integer? How can I check that a number is integer in Lua? Thanks in advance!
Attachments
withoutrandomwallsluasok.love
(27.45 KiB) Downloaded 94 times
luasok.love
(27.45 KiB) Downloaded 101 times
Last edited by glitchapp on Tue Feb 01, 2022 10:55 am, edited 1 time in total.
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: Help implementing game logic from fish fillets

Post by darkfrei »

glitchapp wrote: Tue Feb 01, 2022 10:27 am I use the chance to ask help to solve the problem of the fish moving outside the grid, how can I make it stops every 4 cycles or when the coordinate is integer? How can I check that a number is integer in Lua? Thanks in advance!
Very simple solution:

Code: Select all

if x < minX then x = minX end
if x+w > maxX then x = maxX-w end
Here x is x position of any object, w is the width of it. maxX and minX are coordinates of borders.
Maybe somewhere need to be +1 or -1 of real value.

The code must be reworked for several agents and that one agent can move more than one object.
Something like:

Fish will swim to one direction. Calculate the delta of movement and diskret value.
If collides, make a list with collidings, preapply the diskret values to all of them.
If any of them cannot be moved then no object will be moved at all.
If they are free that apply the calculated movement to agent and all this blocks.

The gravity needs smooth value to respect the dt too. We can add the y value (vertical position in pixels/units), that will be converted to the ty (vertical position in tiles). Also we are need a flag if colliding with this block is deadly for agent.

Code: Select all

If blockFalling (block, dt) then -- checks if the block is on the any support / checks the collision after small movement
	fallBlock(block, dt) -- moves the block smoothly and applies the movement to the ty
	block.deadly = true
else 
	-- no need to move it
	block.deadly = false
end
Last edited by darkfrei on Tue Feb 01, 2022 11:14 am, edited 1 time in total.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
glitchapp
Party member
Posts: 237
Joined: Tue Oct 05, 2021 10:34 am
Contact:

Re: Help implementing game logic from fish fillets

Post by glitchapp »

Wow it seems so simple once you wrote the code, I need time, I'm testing your code and see if it works, I will post the results soon.

Code: Select all

	
	local minX=1
	local maxX=1
	local minY=1
	local maxY=1

	w=4
	h=4

	
	if self.agent.vx <minX then self.agent.vx= minX end
	if self.agent.vx+w> maxX then self.agent.vx=w end
I don't really know what I'm doing, it slows down but it does not stop yet... I will update as soon as I understand the variables
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: Help implementing game logic from fish fillets

Post by darkfrei »

glitchapp wrote: Tue Feb 01, 2022 11:11 am Wow it seems so simple once you wrote the code, I need time, I'm testing your code and see if it works, I will post the results soon.

Code: Select all

	
	local minX=1
	local maxX=1
	local minY=1
	local maxY=1

	w=4
	h=4

	
	if self.agent.vx <minX then self.agent.vx= minX end
	if self.agent.vx+w> maxX then self.agent.vx=w end
I don't really know what I'm doing, it slows down but it does not stop yet... I will update as soon as I understand the variables
if your field is 30x20 then

Code: Select all

	local minX=1
	local maxX=30
	local minY=1
	local maxY=20
Try this in the new main.lua:

Code: Select all

function love.load()
	minX = 100
	maxX = 400
	minY = 200
	maxY = 300
	w=30
	h=20
end

 
function love.update(dt)
	
end

function love.draw()
	local x, y = love.mouse.getPosition()
	if x < minX then x = minX end
	if x+w > maxX then x = maxX-w end
	if y < minY then y = minY end
	if y+h > maxY then y = maxY-h end
	-- border and rectangle:
	love.graphics.rectangle('line', minX, minY, maxX-minX, maxY-minY)
	love.graphics.rectangle('fill', x, y, w, h)
end

function love.keypressed(key, scancode, isrepeat)
	if key == "escape" then
		love.event.quit()
	end
end
2022-02-01T13_05_13-Untitled.png
2022-02-01T13_05_13-Untitled.png (5.51 KiB) Viewed 2998 times
Attachments
main.lua
(620 Bytes) Downloaded 95 times
Last edited by darkfrei on Fri Apr 28, 2023 1:20 pm, edited 1 time in total.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
glitchapp
Party member
Posts: 237
Joined: Tue Oct 05, 2021 10:34 am
Contact:

Re: Help implementing game logic from fish fillets

Post by glitchapp »

Code: Select all

	local minX=1
	local maxX=30
	local minY=1
	local maxY=26

	w=4
	h=2

		if self.agent.x < minX then self.agent.x = minX end
		if self.agent.y < minY then self.agent.y = minY end
		if self.agent.x+w > maxX then self.agent.x=self.agent.x-w end
		if self.agent.y+h > maxY then self.agent.y=self.agent.y-h end
That is what I added, it does not work. The grid is 30x26, the big fish is 3x2

Sorry your code does not do what I'm trying to accomplish, I think I did not explain properly what I mean, I need the big fish to move one cell at a time, just like the second fish does now. It moves freely and that breaks the puzzle, the puzzle needs to work by just being able to move one cell at a time, I mean the fish or objects can not be between two cells

I will keep that code to avoid possible bugs in the future, but the walls already prevents the fish to go outside the grid, by being "inside" what I meant is not being able to move freely between cells but one cell at a time. I will keep the smooth animation, that is nice, but it must stop in one cell not in between.
Attachments
luasok.love
(27.59 KiB) Downloaded 94 times
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: Help implementing game logic from fish fillets

Post by darkfrei »

glitchapp wrote: Tue Feb 01, 2022 12:08 pm Sorry your code does not do what I'm trying to accomplish, I think I did not explain properly what I mean, I need the big fish to move one cell at a time, just like the second fish does now. It moves freely and that breaks the puzzle, the puzzle needs to work by just being able to move one cell at a time, I mean the fish or objects can not be between two cells
We are need something like:

Code: Select all

if fish.moves then
	-- not possible to move it, the fish swims until the next tile
	if not lib.moveFish (fish) then -- move fish and check if it is already in the next tile
		fish.moves = false
	end
elseif love.love.keyboard.isScancodeDown('w', 'a', 's', 'd')
	-- add the speed direction and remember the target tile
	fish.moves = true
	
end
Last edited by darkfrei on Tue Feb 01, 2022 12:51 pm, edited 1 time in total.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
glitchapp
Party member
Posts: 237
Joined: Tue Oct 05, 2021 10:34 am
Contact:

Re: Help implementing game logic from fish fillets

Post by glitchapp »

Yes that code make sense! I will try to implement it!

I need to check if coordinates is integer, if it's integer the fish is inside the cell, if not is not. How can I check that a number is integer in Lua?

something like:

Code: Select all

-- if fish is inside the cell it can react to controls otherwise  wait till fish move to the next cell
if self.agent.x = int then fishcanmove=true else fishcanmove=false
just in case there is an idiomatic misunderstood( english is not my mother tongue)
integer: 1,2,3
not integer 1.2, 1.3, 2.4... etc
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: Help implementing game logic from fish fillets

Post by darkfrei »

glitchapp wrote: Tue Feb 01, 2022 12:37 pm Yes that code make sense! I will try to implement it!

I need to check if coordinates is integer, if it's integer the fish is inside the cell, if not is not. How can I check that a number is integer in Lua?
For my opinion the right way is to remember the target tile and move it to there even on the "too high speed".

For example:

Code: Select all

local tx1 = fish.tx -- integer position in tiles
local ty1 = fish.ty
if fish.swimDirection = "right" then
	fish.target_tx = tx+1
end
and

Code: Select all

if fish.swimDirection = "right" then
	fish.x = fish.x + dt * fish.vx -- smooth solution
	fish.tx = getTilePosition(fish.x) -- diskrete solution, fish position in tiles (with fraction)
	if fish.tx >= fish.target_tx then
		-- we are in the next tile
		-- it works with huge dt or high speed too.
	end
end
Maybe we are need to compare two smooth values, that the animation of movement is ok.

Code: Select all

function love.load()
	x = 100
	targetX = nil
	vx = 150
	y = 100
	r = 20
	moves = false
end

function love.update(dt)
	-- input works for not moving object only
	if not moves then
		local dDown = love.keyboard.isScancodeDown('d')
		local aDown = love.keyboard.isScancodeDown('a')
		if dDown and not aDown then
			moves = "right"
			targetX = x + 100
		elseif aDown and not dDown then
			moves = "left"
			targetX = x - 100
		end
	end
	-- need to move in this tick too
	if moves then
		if moves == "right" then
			x = x + dt*vx
			if x >= targetX then
				x = targetX
				moves = false
			end
		elseif moves == "left" then
			x = x - dt*vx
			if x <= targetX then
				x = targetX
				moves = false
			end
		end
	end
end

function love.draw()
	love.graphics.print ('press A or D')
	for i = 1, 7 do
		love.graphics.line (i*100, 0, i*100, 200)
	end
	love.graphics.circle('line', x, y, r)
end

function love.keypressed(key, scancode, isrepeat)
	if key == "escape" then
		love.event.quit()
	end
end
2022-02-01T14_19_32-Untitled.png
2022-02-01T14_19_32-Untitled.png (8.55 KiB) Viewed 2960 times
main.lua
(1.03 KiB) Downloaded 92 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
glitchapp
Party member
Posts: 237
Joined: Tue Oct 05, 2021 10:34 am
Contact:

Re: Help implementing game logic from fish fillets

Post by glitchapp »

Exactly! that's what I need, perfect, I'm going to try to implement it now
Post Reply

Who is online

Users browsing this forum: No registered users and 67 guests