## Help implementing game logic from fish fillets

General discussion about LÖVE, Lua, game development, puns, and unicorns.
glitchapp
Party member
Posts: 136
Joined: Tue Oct 05, 2021 10:34 am

### Help implementing game logic from fish fillets

Hello!,

First, I opened this post in the wrong section, to give background of what have been posted already here is the link to last post:

https://love2d.org/forums/viewtopic.php?f=14&t=92580

I'm trying to implement fish fillets game mechanics in lua. I used the new level map that the user "pgimeno"provided me and I started to divide the background and the objects into different tables, so that it's easier to move and detect them.

I changed a pair of characters for the fish heads because they were hard to see for me but that's not important and all is provisional.

I attach the game with changes just to show that the background look cleaner now, but nothing else has been implemented yet. It will take me time so don't expect new posts very soon, at least till I accomplish a pair of main tasks. I hope you find the challenge so interesting as I do! Ah I forgot, the code is also updated in github for everyone to follow: https://github.com/glitchapp/Luasok
Last edited by glitchapp on Wed Feb 02, 2022 8:05 am, edited 2 times in total.
glitchapp
Party member
Posts: 136
Joined: Tue Oct 05, 2021 10:34 am

### Re: Help implementing game logic from fish fillets

I accomplished already a few things:
1. All the objects and the characters are now out of the background and loaded and drawn independently from their own tables.
2. The characters now move properly with all the cells as they should and they switch direction too.
3. Collisions with the walls works partially, I still need to figure out what's not working but I'm close to make it work.

Once I solve the collisions with the walls I will start with the objects that should be able to be pushed and fall down.
If I accomplish that half the mechanics are done.. hopefully. I attached the new code and updated it in github.

I added some options for debug, they will not be in later releases or they will be hidden but for now they could be useful.
Attachments
luasok.love
glitchapp
Party member
Posts: 136
Joined: Tue Oct 05, 2021 10:34 am

### Re: Help implementing game logic from fish fillets

I think it's becoming exciting this challenge. I implemented the grid and push code from darkfrei (https://github.com/darkfrei/love2d-lua- ... ush-blocks), now I have a grid and the objects. I realize that there is a performance issue? I'm not sure why is becoming slow, maybe too much loops I need to fix that, and I need to move the grid one cell left and one up. I need to find the way to put all the pieces together but I feel I'm getting closer to the solution!

It looks like a mess right now but all the debug information and the grid help me visualize how to put the pieces together!

I use this chance to ask a question: I don't understand what all this math is doing:
self.agent.y = self.agent.y + tdy*pb.grigSize
self.agent.ty = math.floor(self.agent.y*4/self.grigSize+0.5)/2

I'm trying to figure out how to make the blocks move inside the grid, any advice on this?
Attachments
luasok.love
darkfrei
Party member
Posts: 690
Joined: Sat Feb 08, 2020 11:09 pm

### Re: Help implementing game logic from fish fillets

Here must be optimization:
1) you are need to check only "dynamic" objects, that can be moved, other "static" tiles must be on the map[y][x].
2) By the checking of "dynamic" blocks (that can be moved) check first the border: if the fish 3x1 tiles has any collision with 2x3 block at all:
https://love2d.org/wiki/BoundingBox.lua
If you _can_ have this collision, than you are need to check every single tile in this block.

Smooth movement is for animation and also used as buffer to respect the *dt* properly, all logic is tile-based.
glitchapp
Party member
Posts: 136
Joined: Tue Oct 05, 2021 10:34 am

### Re: Help implementing game logic from fish fillets

I see, I'm learning a lot! I found what causes the performance issue, it is just and exclusively the background, which is very expensive in terms of drawing cycles. If I comment draw_level(), all the performance problems disappear completely, so there's no need to do much more to improve performance than drawing the background as an Image. There are also not many objects in the game. I will attach an example showing how the issue with performance is completely solved. I will check that library and see if I can implement it to solve the problem with collisions. Thanks!
darkfrei
Party member
Posts: 690
Joined: Sat Feb 08, 2020 11:09 pm

### Re: Help implementing game logic from fish fillets

You can use canvas to draw the background just once.
https://love2d.org/wiki/Canvas
glitchapp
Party member
Posts: 136
Joined: Tue Oct 05, 2021 10:34 am

### Re: Help implementing game logic from fish fillets

I uploaded the demo with the performance issue solved. Now it moves too fast, but that's easier to solve. I just wrote a fake background with love.graphics.draw and you can't notice the difference. By fake I mean without writing the cells one by one which caused the performance issue.

Many things solved already! now I need to make sure that the movement stops inside the grid for the push-blocks library, I don't think that's hard, I just need to understand some of the math involved. I feel like I'm going to beat this challenge! thanks for the help!
Attachments
luasok.love
Last edited by glitchapp on Sun Jan 30, 2022 5:16 pm, edited 1 time in total.
glitchapp
Party member
Posts: 136
Joined: Tue Oct 05, 2021 10:34 am

### Re: Help implementing game logic from fish fillets

I implemented the canvas, it was easier than I thought, this way the app keeps small which is good, thanks! it was before 39.14 KiB and now is 19kb again. Cool!

The big fish can move the objects now, it just don't stop inside the grid as it should. The small fish can't move objects yet but it moves properly.

Summary of what have been done so far:

- Background separated from the objects and drawn to the canvas
- Objects can now be pushed (only by the big fish yet)
- Switch character works
- Objects can't be moved down (but they don't fall by themselves yet)
- Collision with the walls works (only implemented on the big fish)

What's left yet:
- Make the the big fish stop after every move inside the grid
- Optimization: Performance issue caused now by the objects, they need to be drawn to the canvas and draw them only when necessary.
- Implement gravity: makes objects fall down
- Objects beyond the pushed objects should be detected, when the object collide the player should not be able to push further.
- Objects falling over character kills them
- Steel bar can only be moved by the big fish

There is no doubt of what's causing the performance issue now, you can press the key "2" to hide objects and notice the difference.
Attachments
luasok.love
darkfrei
Party member
Posts: 690
Joined: Sat Feb 08, 2020 11:09 pm

### Re: Help implementing game logic from fish fillets

Updated tiled-objects with collisions:
Now the agent checks if the bounding boxes of agent and block is colliding,
if yes, then check if here is an actual contact between them, the block checks if it can be moved too.

Code: Select all

function pb:canMove (agent, dx, dy)
for i, block in ipairs (self.blocks) do
if isRoughAgentBlockCollision (agent, block, dx, dy) then
block.color = lightgreen
if isFineAgentBlockCollision (agent, block, dx, dy) then
if pb:canMoveBlock (block, dx, dy) then
block.color = green
return false, block
else
block.color = red
return false, nil
end
end
else
block.color = white
end
end
return true
end
push-blocks-02.love
glitchapp
Party member
Posts: 136
Joined: Tue Oct 05, 2021 10:34 am

### Re: Help implementing game logic from fish fillets

Hei this is really cool, without your library I would never finish the mechanics of this game!! I'm really grateful for your help! Now it is smooth and objects collide! It is almost perfect, I also found what was causing the performance issue, it wasn't the objects but the many colors and characters I was using for debug.

Sorry, I did not understand the variables, now all collisions works including walls! It was just that I did not set up the height and width of every object and the agent and now all edges are detected. Cool!

I think it's almost done, I'm going to try to implement the gravity and also put the second player inside your algorithm. I'm having a lot of fun now that it moves forward!

Ah there is still something else missing: objects could be transported when they are pilled, at least in the original game, but that's not so important now, the most dificult mechanics are working now! The character must stop inside the grid otherwise the puzzle do not work properly. I also need to put the second fish inside your algorithm as second agent.

Summarizing what needs to be done yet:

- Gravity: objects fall down and kills player if they are in the path
- Fish stop inside the grid (I notice it does every 4 cycles so I just need to write a condition that make it stop in time)
- Objects can be pilled and transported when pilled
- Steel objects can only be pushed by the big fish

I added a debug mode that can be activated with key "1". This way if someone wants to just play the game and test it you don't need to deal with all the debug graphics (debug mode activates the grid and additional options to show / hide the objects and players).

I also added a little document tool to keep track of the progress of the development in the debug mode, also by pressing key 1 and then 6 (you can press key "h" to check the help document with all keys and options)
Attachments
luasok.love