Help implementing game logic from fish fillets
Re: Help implementing game logic from fish fillets
Added map tiles and rough collision between agent and map; blocks and map.
- Attachments
-
- push-blocks-03.love
- (2.83 KiB) Downloaded 254 times
Last edited by darkfrei on Fri Apr 28, 2023 1:19 pm, edited 1 time in total.
Re: Help implementing game logic from fish fillets
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:
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!
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 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 244 times
-
- luasok.love
- (27.45 KiB) Downloaded 249 times
Last edited by glitchapp on Tue Feb 01, 2022 10:55 am, edited 1 time in total.
Re: Help implementing game logic from fish fillets
Very simple solution:
Code: Select all
if x < minX then x = minX end
if x+w > maxX then x = maxX-w end
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.
Re: Help implementing game logic from fish fillets
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.
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
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
Re: Help implementing game logic from fish fillets
if your field is 30x20 thenglitchapp 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.
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 variablesCode: 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
Code: Select all
local minX=1
local maxX=30
local minY=1
local maxY=20
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
- Attachments
-
- main.lua
- (620 Bytes) Downloaded 266 times
Last edited by darkfrei on Fri Apr 28, 2023 1:20 pm, edited 1 time in total.
Re: Help implementing game logic from fish fillets
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
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 257 times
Re: Help implementing game logic from fish fillets
We are need something like: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
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.
Re: Help implementing game logic from fish fillets
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:
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
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
integer: 1,2,3
not integer 1.2, 1.3, 2.4... etc
Re: Help implementing game logic from fish fillets
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
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
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
Re: Help implementing game logic from fish fillets
Exactly! that's what I need, perfect, I'm going to try to implement it now
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 0 guests