Proper collisions

This commit is contained in:
Marco Giacomelli 2024-04-17 11:29:08 +02:00
commit de176974be

View file

@ -1,4 +1,4 @@
--[[pod_format="raw",created="2024-04-14 14:05:11",modified="2024-04-16 22:07:51",revision=191]] --[[pod_format="raw",created="2024-04-14 14:05:11",modified="2024-04-17 09:28:52",revision=219]]
--include("/cirnofarm/src/actor.lua") --include("/cirnofarm/src/actor.lua")
last_coll=0 last_coll=0
mouse_debug = true mouse_debug = true
@ -25,8 +25,6 @@ function _init()
cb=true, -- Collide with world bounds cb=true, -- Collide with world bounds
draw=function(self) draw=function(self)
spr(self.spriteIndex,self.x,self.y, self.hflip) spr(self.spriteIndex,self.x,self.y, self.hflip)
drawCollision(self.x+self.move_x,self.y,self.w-1,self.h-1)
drawCollision(self.x,self.y+self.move_y,self.w-1,self.h-1)
print(string.format("x:%.2f y:%.2f mx:%.2f my:%.2f coll:%d",self.x,self.y, print(string.format("x:%.2f y:%.2f mx:%.2f my:%.2f coll:%d",self.x,self.y,
self.move_x,self.move_y,last_coll),0,0,1) self.move_x,self.move_y,last_coll),0,0,1)
end, end,
@ -38,16 +36,11 @@ function _init()
if (btn(2)) self.move_y -= self.speed if (btn(2)) self.move_y -= self.speed
if (btn(3)) self.move_y += self.speed if (btn(3)) self.move_y += self.speed
if (checkCollision(self.x+self.move_x,self.y,self.w-1,self.h-1)) then if (check_collision(self.x+self.move_x,self.y,self.w-1,self.h-1)) then
--if (can_move(self.move_x,self.move_y)) then
--if (cmap(self) == false) then
if (not self.noclip) then self.move_x = 0 end if (not self.noclip) then self.move_x = 0 end
--else
-- sfx(0)
end end
if (not self.noclip and checkCollision(self.x,self.y+self.move_y,self.w-1,self.h-1)) then if (check_collision(self.x,self.y+self.move_y,self.w-1,self.h-1)) then
if (not self.noclip) then self.move_y = 0 end if (not self.noclip) then self.move_y = 0 end
end end
@ -56,10 +49,6 @@ function _init()
self.x += self.move_x self.x += self.move_x
self.y += self.move_y self.y += self.move_y
--if (checkCollision(col_x,col_y) == false) then
-- x = col_x
-- y = col_y
--end
end end
}) })
@ -91,45 +80,21 @@ function _update()
end end
end end
function drawCollision(x,y,w,h) function check_collision(x,y,w,h)
for i=x,x+w,w do
pset(i,y,8)
--pset(i,y+h,18)
end
--[[for i=y,y+h,w do
pset(x,i,8)
pset(x+w,i,8)
end]]
--rect(left,top,right,bottom,2)
end
function checkCollision(x,y,w,h)
local collide = false local collide = false
last_coll = 0
for i=x,x+w,w do for i=x,x+w,w do
local sprite = mget(i,y) if (is_tile_solid(i,y) or is_tile_solid(i,y+h)) then
local flag = fget(sprite) collide = collide or true
if (flag == 1) then
collide = true
last_coll = 1
end end
--if (is_tile_solid(i,y) --[[or is_tile_solid(i,y+h)]]) then
-- collide = true
-- last_coll = 1
--end
end end
--for i=y,y+h do for i=y,y+h do
-- if (is_tile_solid(x,i) or is_tile_solid(x+w,i)) then if (is_tile_solid(x,i) or is_tile_solid(x+w,i)) then
-- collide = true collide = collide or true
-- end end
--end end
return collide return collide
end end
@ -142,7 +107,7 @@ function is_tile(tile_type,x,y)
end end
function is_tile_solid(x,y) function is_tile_solid(x,y)
return is_tile(1,x,y) return is_tile(1,x,y) == 1
end end
function can_move(x,y) function can_move(x,y)