Help implementing game logic from fish fillets

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

Re: Help implementing game logic from fish fillets

Post by darkfrei »

The agent must be defined global OR defined local from the lib:

Code: Select all

function yourLib:moveAgent (dx, dy)
	local agent = self.agent -- it calls the yourLib.agent
	local x = agent.x
	local y = agent.y
	-- real changing of the agent's position:
	agent.x = x + dx
	agent.y = y + dy
end
In the push-blocks:

Code: Select all

function pb:moveAgent (dx, dy)
	local agent = self.agent
	local x, y = agent.x, agent.y
	-- real changing of the agent's position:
	agent.x = x + dx
	agent.y = y + dy
end

Code: Select all

if canMove then
	pb:moveAgent (dx, dy)
end
But if there is no smooth movement, we don't need to store two types of position; dx and dy can be only -1 or 0 or 1.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
glitchapp
Party member
Posts: 264
Joined: Tue Oct 05, 2021 10:34 am
Contact:

Re: Help implementing game logic from fish fillets

Post by glitchapp »

Code: Select all

function moveAgent (dx, dy) -- global defined agent
	local agent = self.agent
	local x, y = agent.x, agent.y
	-- real changing of the agent's position:
	agent.x = x + dx
	agent.y = y + dy
end

--function moveBlock (block, dx, dy) -- exactly this block
	--block.x = block.x + dx
	--block.y = block.y + dy
--end
	
function pb:updateAgents (dt)

if problemdebug=="1" then
	if not moves then
		local up =    love.keyboard.isScancodeDown('up')
		local down =  love.keyboard.isScancodeDown('down')
		local right =  love.keyboard.isScancodeDown('right')
		local left =  love.keyboard.isScancodeDown('left')

		local dx, dy = 0, 0
		
		if up and not (down or right or left) then
			moves = "up"
			targetY = self.agent.ty - 1
			dy=-1
		elseif down and not (up or right or left) then
			moves = "down"
			targetY = self.agent.ty + 1
			dy=1
		elseif right and not (up or down or left) then
			moves = "right"
			--targetX = self.agent.tx + 1
			dx=1
		elseif left and not (up or down or right) then
			moves = "left"
			targetX = self.agent.tx - 1
			dx=-1
		end
	end

	-- need to move in this tick too
		local tdx = dt*self.agent.vx -- delta X in tiles
		local tdy = - dt*self.agent.vup -- delta Y in tiles
		local canMove, block = pb:canMove (self.agent, tdx, 0)

		if moves =="right"  then
			--self.agent.tx = self.agent.tx + dt*self.agent.vx
				if canMove then
					
--						if self.agent.tx >= targetX then
	--						self.agent.tx = targetX
							pb:moveAgent (dx, dy)
							moves = false
						--end
				elseif block and block.movable then
						if self.agent.tx >= targetX then
							self.agent.tx = targetX
							moves = false
						local tx = self.agent.tx
							self.agent.tx = targetX
							--moveBlock (dx,dy)
							block.tx = targetX+1
							moves = false
						end
				end
Error game/logic/push-blocks.lua:407: attempt to call method "moveAgent" (a nil value)
Attachments
luasok.love
(444.4 KiB) Downloaded 145 times
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Help implementing game logic from fish fillets

Post by darkfrei »

glitchapp wrote: Sat Feb 12, 2022 12:08 pm

Code: Select all

function moveAgent (dx, dy) -- global defined agent
	local agent = self.agent
	local x, y = agent.x, agent.y
	-- real changing of the agent's position:
	agent.x = x + dx
	agent.y = y + dy
end
Error game/logic/push-blocks.lua:407: attempt to call method "moveAgent" (a nil value)
add the pb: to this function:

Code: Select all

function pb:moveAgent (dx, dy)
	local agent = self.agent -- lib defined agent
	local x, y = agent.x, agent.y
	-- real changing of the agent's position:
	agent.x = x + dx
	agent.y = y + dy
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
glitchapp
Party member
Posts: 264
Joined: Tue Oct 05, 2021 10:34 am
Contact:

Re: Help implementing game logic from fish fillets

Post by glitchapp »

done, now I have the following error:

Error: game/logic/push-blocks.lua:357: attempt to perform arithmetic on local 'dx' (a nil value)

Press right to test it, I only implemented it to the right movement

Wait I found why, local dx, dy = 0, 0 is inside the conditional, I have to put it outside, once I put it outside there's no error anymore, the agent still does not move but I'm trying to find why
Last edited by glitchapp on Sat Feb 12, 2022 5:36 pm, edited 2 times in total.
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Help implementing game logic from fish fillets

Post by darkfrei »

glitchapp wrote: Sat Feb 12, 2022 4:37 pm done, now I have the following error:

Error: game/logic/push-blocks.lua:357: attempt to perform arithmetic on local 'dx' (a nil value)

Press right to test it, I only implemented it to the right movement
I am using the ZeroBrain Studio, as you see the line 407 has dx an dy with underscore, it means that it was not local defined.
You define in under the is statement and it is not available out of it.
Attachments
2022-02-12T18_04_44-ZeroBrane Studio - D__User_Downloads_luasok (9)_game_logic_push-blocks.lua.png
2022-02-12T18_04_44-ZeroBrane Studio - D__User_Downloads_luasok (9)_game_logic_push-blocks.lua.png (102.3 KiB) Viewed 5238 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
glitchapp
Party member
Posts: 264
Joined: Tue Oct 05, 2021 10:34 am
Contact:

Re: Help implementing game logic from fish fillets

Post by glitchapp »

darkfrei wrote: Sat Feb 12, 2022 5:07 pm
glitchapp wrote: Sat Feb 12, 2022 4:37 pm done, now I have the following error:

Error: game/logic/push-blocks.lua:357: attempt to perform arithmetic on local 'dx' (a nil value)

Press right to test it, I only implemented it to the right movement
I am using the ZeroBrain Studio, as you see the line 407 has dx an dy with underscore, it means that it was not local defined.
You define in under the is statement and it is not available out of it.
I know, I found out, I just edited my reply, I think I may should use an ide like that because I use geany and it does not show any of that. Now there's no error, the agent does not move though, I'm trying to find why.

Code: Select all

local dx, dy = 0, 0

function pb:moveAgent (dx, dy) -- global defined agent
	local agent = self.agent
	local x, y = agent.x, agent.y
	-- real changing of the agent's position:
	agent.x = x + dx
	agent.y = y + dy
end

--function moveBlock (block, dx, dy) -- exactly this block
	--block.x = block.x + dx
	--block.y = block.y + dy
--end
	
function pb:updateAgents (dt)

if problemdebug=="1" then
	if not moves then
		local right =  love.keyboard.isScancodeDown('right')
		...
		elseif right and not (up or down or left) then
			moves = "right"
			dx=1
		end
	end

	-- need to move in this tick too
		local tdx = dt*self.agent.vx -- delta X in tiles
		local tdy = - dt*self.agent.vup -- delta Y in tiles
		local canMove, block = pb:canMove (self.agent, tdx, 0)

		if moves =="right"  then
				if canMove then
				
				pb:moveAgent (dx, dy)
				moves = false
				elseif block and block.movable then
						if self.agent.tx >= targetX then
							self.agent.tx = targetX
							moves = false
						local tx = self.agent.tx
							self.agent.tx = targetX
							--moveBlock (dx,dy)
							block.tx = targetX+1
							moves = false
						end
				end
No sorry it's not working I don't know how and where to call that function... there's no movement
Attachments
luasok.love
(625.15 KiB) Downloaded 122 times
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Help implementing game logic from fish fillets

Post by darkfrei »

Hi, the lib was reworked:
2022-02-12T20_54_44-Untitled.png
2022-02-12T20_54_44-Untitled.png (65 KiB) Viewed 5194 times



See the code:
Attachments
push-blocks.lua
(9.31 KiB) Downloaded 121 times
push-blocks-04.love
CC0 - creative commons
(4.68 KiB) Downloaded 137 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
glitchapp
Party member
Posts: 264
Joined: Tue Oct 05, 2021 10:34 am
Contact:

Re: Help implementing game logic from fish fillets

Post by glitchapp »

Oh I can't believe it! you implemented gravity too! the game logic is almost done! Thank you!, I will implement it soon and update the game and upload it here.
Wait! WTF! you implemented everything! the deadly flag, the second player, wow! I can't believe it, I now realize how hard is all of this game logic man, I should have started with something easier but you just saved my game! This is amazing! Should I put you in credits? ah ah you made my day, thank you! I will upload the game with your game logic soon.

the game is completely playable!

Before uploading everything I need to deal with a few bugs first. The first is that when I don't load the deprecated code for the second player the background turns black, some library turns the background black I need to find out and solve it.
Update I found it: Slab, the library in charge of the gui overrides all colors with its own templates, I need to deactivate that.

Solved partially, after drawing Slab I set up colors back to normal, I'm not happy with this, but I can't find where Slab set up the colors, any help with this is welcome.

Code: Select all

Slab.Draw()
love.graphics.setColor(1,1,1)
Things that I'm working to solve:
- There is a small rule missing: Some objects can only be pushed by the big fish, could we make a flag for that? I think that would be super easy.

- SOLVED: For what I see, the new code calculates the grid size directly from the level table, and because I created originally a table for the canvas and a collisions table just with the edges of the level for the game logic, I think the grid is not calculated properly. No problem, I can solve that, I will insert the whole level in the table.

- UNSOLVED: Input is not working, I expected that, the reason is that there actually 3 files listening to keys (push-blocks, shaders.lua and input.lua) and that does not work. I need to put all the inputs in just one file and see if everything works.

I think that's all, it starts looking like a real game and not like a demo! that's so cool! As soon as I solve all of this problems I will update the attached game!

Thanks darkfrei! That's an amazing work!
Attachments
luasok.love
(627 KiB) Downloaded 130 times
Last edited by glitchapp on Sun Feb 13, 2022 9:38 am, edited 2 times in total.
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: Help implementing game logic from fish fillets

Post by pgimeno »

glitchapp wrote: Sun Feb 13, 2022 4:30 amI now realize how hard is all of this game logic man,
Not that you weren't warned ;)

There are still some differences between the rules implemented and the actual game rules.
glitchapp
Party member
Posts: 264
Joined: Tue Oct 05, 2021 10:34 am
Contact:

Re: Help implementing game logic from fish fillets

Post by glitchapp »

pgimeno wrote: Sun Feb 13, 2022 9:27 am
glitchapp wrote: Sun Feb 13, 2022 4:30 amI now realize how hard is all of this game logic man,
Not that you weren't warned ;)
I know what you mean, the game rules seems easier before you start coding.

I had never done this without help. It's amazing that all the rules are implemented.

I updated the game with a new resolution, now it can be enjoyed in full hd.

The last thing that it's missing are inputs, I don't understand what's interfering with it. I'm going to try to test another function to see if I can make it work.

I Found it! A function in charge of the key input in main.lua was not copied, now it can be played!

So let me inaugurate the playable game with a few features changed so that everything what's done till now can be seen.

Code: Select all

--debug game states
    debugmode="no"
    talkies=true
  shader1=true
  
All of those variables are game states that shows the tutorial, hide the debug mode (with all the menus, the grid and extra information) and set the first shader on.

If it annoys you just invert the variables, set debugmode to yes and talkies and shader1 to false.

Now in full HD!

Enjoy!
Attachments
luasok.love
(627.83 KiB) Downloaded 135 times
Last edited by glitchapp on Sun Feb 13, 2022 10:37 am, edited 1 time in total.
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests