Re: Help implementing game logic from fish fillets
Posted: Mon Jan 31, 2022 9:06 pm
Added map tiles and rough collision between agent and map; blocks and map.
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
Very simple solution:
Code: Select all
if x < minX then x = minX end
if x+w > maxX then x = maxX-w end
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
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
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
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
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
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
For my opinion the right way is to remember the target tile and move it to there even on the "too high speed".
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