Page 13 of 20

Re: Help implementing game logic from fish fillets

Posted: Wed Jul 27, 2022 11:02 pm
by darkfrei
Based on the Fishfilletsmini21.08.22.love, I've started from it :)

This part has overlapping!
2022-07-28T00_59_28-level-57.png
2022-07-28T00_59_28-level-57.png (7.17 KiB) Viewed 5860 times
push-blocks.lua
from fish-fillets-mini_2022-07-28.love
(19.17 KiB) Downloaded 689 times
fish-fillets-mini_2022-07-28.love
(1017.69 KiB) Downloaded 199 times

Als rewritten:

Code: Select all

local function setBlockOutline (block)
	local d = 1/20
	local m = {} -- map of outlines
	local tiles = block.tiles -- list of tiles as {x1,y1, x2,y2, x3,y3 ...}
	for i = 1, #tiles, 2 do
		local x, y = tiles[i], tiles[i+1]
		if not m[y] then 
			m[y] = {} 
		end
		if not m[y][x] then 
			m[y][x] = true
		end
	end
	local lines = {}
	for y, xs in pairs (m) do
		for x, tabl in pairs (xs) do
			local h1 = m[y-1] and m[y-1][x]
			local v1 = m[y]   and m[y][x-1]
			local h2 = m[y+1] and m[y+1][x]
			local v2 = m[y]   and m[y][x+1]
			
			if not h1 then
				table.insert (lines, {x+d,y+d, x+1-d, y+d})
			end
			if not h2 then
				table.insert (lines, {x+d,y+1-d, x+1-d, y+1-d})
			end			
			if not v1 then
				table.insert (lines, {x+d,y+d, x+d, y+1-d})
			end
			if not v2 then
				table.insert (lines, {x+1-d,y+d, x+1-d, y+1-d})
			end

		end
	end
	block.lines = lines
end

Re: Help implementing game logic from fish fillets

Posted: Thu Jul 28, 2022 5:38 am
by glitchapp
Yuhuu!!

That piece of code is a marble! Now it is really optimized and everything works as it should.

I'm really going to study that piece of code, I really want to understand all the interactions. I guess the metablocks are blocks behind the blocks, in any case now all the levels work as it should.

I'm going to upload it in the next update.

Think in about it, that code could be used for a lot of games and tools and I hope someone do it. For example, a puzzle game engine like Free Hero Mesh, or any puzzle game you can think about that needs gravity.

You saved my our game and solved the logic!! thank you darkfrei!

Re: Help implementing game logic from fish fillets

Posted: Thu Jul 28, 2022 6:49 am
by darkfrei
glitchapp wrote: Thu Jul 28, 2022 5:38 am Yuhuu!!

That piece of code is a marble! Now it is really optimized and everything works as it should.

I'm really going to study that piece of code, I really want to understand all the interactions. I guess the metablocks are blocks behind the blocks, in any case now all the levels work as it should.

I'm going to upload it in the next update.

Think in about it, that code could be used for a lot of games and tools and I hope someone do it. For example, a puzzle game engine like Free Hero Mesh, or any puzzle game you can think about that needs gravity.

You saved my our game and solved the logic!! thank you darkfrei!
Thanks, you are welcome!
With this new outline system you can see where the blocks have clipping, was this clipping intend? The push-blocks.lua ignores it, it solves the next iteration, not the current one.
no clipping
no clipping
2022-07-28T08_51_43-no-clipping.png (5 KiB) Viewed 5821 times
clipping
clipping
2022-07-28T08_52_30-clipping.png (1.32 KiB) Viewed 5821 times

Re: Help implementing game logic from fish fillets

Posted: Thu Jul 28, 2022 7:43 am
by glitchapp
I completely separated my code from the game logic in the full game, so that now anyone can study it and is easy to maintain and to understand.

All what I inserted in the game logic created by darkfrei can be divided in two files: events, and draw assets, I created two files with all that code (attached here).

Events are the things that triggers voices, effects and other scripts. There are basically 5 type of events:

The first file is called "game/logic/events.lua "

--Objects touched: eventcollisionblocks()
This functions is triggered when a fish touches any object but can not push it (the small fish can not push steel objects)

- Object pushed:function eventobbjectpushed()
That function is triggered when a fish push an object.

Agent position: function eventagentposition()

This function is triggered when any fish is in some area or approach something / somewhere.

An object falls: function eventsblockfalling()
This function is triggered when any specific object falls (for example the briefcase on level 2 or other objects that trigger some events)

The second file is called "game/logic/drawassets" and as you expect it draws the assets instead of flat rectangles for the objects.

Now the game logic is completely clean and separated from my code, the only thing there is in the game logic is calls to those functions.

I have however a last problem that I think is easy to solve but I don't know what's the solution:

The variable block can not be accessed from an external function because is local and I become the following error if I unncoment any of the calls to the event's functions: "attempt to index global "block" a nil value."

What is the best way to let my functions access the local block variable so that they work outside of push-blocks.lua and I can keep the code separated and clean?

I attached the demo with the files separated but all the calls to my functions are commented till I find a solution.

Thank you

Re: Help implementing game logic from fish fillets

Posted: Thu Jul 28, 2022 8:14 am
by darkfrei
glitchapp wrote: Thu Jul 28, 2022 7:43 am
What is the best way to let my functions access the local block variable so that they work outside of push-blocks.lua and I can keep the code separated and clean?

I attached the demo with the files separated but all the calls to my functions are commented till I find a solution.

Thank you
The simplest way is just send it by argument:

Code: Select all

push-blocks.lua/233. function pb:moveBlock (block, dx, dy)
	block.x = block.x + dx
	block.y = block.y + dy
	eventobbjectpushed(block) -- call function with block
end

Code: Select all

events.lua/125. function eventobbjectpushed(block) -- now we have block here!

Re: Help implementing game logic from fish fillets

Posted: Thu Jul 28, 2022 9:07 am
by glitchapp
Thanks darkfrei,
I did it and it works.
It is almost perfect now, I need to polish 2 things to make it perfect:
1. It seems that the size of the cells have changed / displaced a little and now the assets and background look a little displaced / out of place. I guess I need to tweak some values. It the retro color scheme everything is in place ( I attached a screenshot)
2. The pushevent is triggered all the time, I think I need to put the call to my function somewhere else.

The commit with all the changes is here: https://codeberg.org/glitchapp/fish-fil ... 2e9a225473

Don't worry I'll find out, I uploaded to the server now, finally the horrible spaghetti code is clean and it works! Thank you!

Re: Help implementing game logic from fish fillets

Posted: Thu Jul 28, 2022 9:40 am
by darkfrei
push-blocks.lua/71. (in the function pb:load)
Earlier was:

Code: Select all

self.gridSize = (math.min(width/(level.w), height/(level.h)))
now

Code: Select all

self.gridSize = math.floor(math.min(width/(level.w), height/(level.h)))
for pixel-perfect rendering.

You can access this value as

Code: Select all

local gridSize = pb.gridSize
OR set your custom gridSize to this value here.

Re: Help implementing game logic from fish fillets

Posted: Thu Jul 28, 2022 9:57 am
by glitchapp
Thank you, I will remove the new gridsize line and the pixel-perfect rendering because then I need to resize all the assets or change the size of the parameters and that seems like too much work for now, there will be time to work so that the pixel perfect proportions work again.

Thanks darkfrei, now everything works and is not displaced, I will upload this last update to the server

update: almost everything work, but the eventobbjectpushed(block) function that I call to trigger events when an object is pushed, is not working properly and it is being triggered on every movement. It should only be triggered when an object is pushed and if then it checks inside if the right object and conditions apply to trigger some voices and scripts.

Will see if I manage to get it work right, in the meanwhile in the first level the script that says that the pipe is pushed is triggered without reason and in level 56 the light is switched on without reason too...

Code: Select all

function pb:moveBlocks (blocks, dx, dy)
	for i, block in ipairs (blocks) do
		self:moveBlock (block, dx, dy)
		eventobbjectpushed(block)			--trigers events when objects are pushed
	end
end

Re: Help implementing game logic from fish fillets

Posted: Thu Jul 28, 2022 11:29 am
by darkfrei
from here: https://codeberg.org/glitchapp/fish-fil ... s.lua#L439
push-blocks.lua#L439

Code: Select all

function pb:mainMoving (dx, dy,dt)
	local agent = self.agent -- active agent
	if dx > 0 then 
		agent.direction = "right"
	elseif dx < 0 then
		agent.direction = "left"
	end
	local blocks, canMove = self:getBlocksToMove (agent, dx, dy)
	if canMove then
		self:moveBlock (agent, dx, dy) -- agent is block too
		self:moveBlocks (blocks, dx, dy)
--		self:fallBlocks (self.blocks, dx, dy) 
		self:fallBlocks ( ) 
			
	end
	eventagentposition(block,agent)				-- trigger events when on a specific position
end
You are run this event without condition, after the end of condition.

Make this part like this:

Code: Select all

if canMove then
	self:moveBlock (agent, dx, dy) -- agent is block too
	event_on_agent_moves (agent)
	self:moveBlocks (blocks, dx, dy)
	event_on_agent_move_blocks (agent, blocks)
	local fallingBlocks = self:fallBlocks () -- must be changed to return all blocks that falling
	event_on_falling_blocks (fallingBlocks)
else
	-- if agent has tried to move, but it was not possible
	event_on_agent_not_moves (agent)
	-- if agent has tried to move blocks, but it was not possible
	event_on_agent_not_move_blocks (agent, blocks)
end

Re: Help implementing game logic from fish fillets

Posted: Thu Jul 28, 2022 12:16 pm
by glitchapp
The only call that works like that is eventagentposition(block,agent)
All of the others send an error message: events-lua:612: attempt to index local 'block' (a nil value)

I can't solve it for now, as a temporary solution I will use a conditional to use old game logic when it is not needed to solve the level till I solve the problem with the events.

Code: Select all

		--temporary solution till the events are solved with the new game logic
		if nLevel==23 or nLevel==54 or nLevel==57 or nLevel==60 then
			pb = require ('game/logic/push-blocks')			-- load game logic
		else
			pb = require ('game/logic/push-blocksold')			-- load game logic
		end