collisions

This commit is contained in:
MaddoScientisto 2024-04-16 00:01:26 +02:00
commit 32240e598f
2 changed files with 63 additions and 27 deletions

View file

@ -1,4 +1,4 @@
--[[pod_format="raw",created="2024-04-14 14:05:11",modified="2024-04-15 17:58:22",revision=94]]
--[[pod_format="raw",created="2024-04-14 14:05:11",modified="2024-04-15 22:01:12",revision=116]]
--include("/cirnofarm/src/actor.lua")
mouse_debug = true
@ -11,22 +11,18 @@ function _init()
add(player, {
x=128,
y=128,
w=16,
h=16,
speed=1.2,
hflip=false,
spriteIndex=64,
move_x=0,
move_y=0,
cm=false, -- Collide with map tiles
cm=true, -- Collide with map tiles
cb=true, -- Collide with world bounds
draw=function(self)
spr(self.spriteIndex,self.x,self.y, self.hflip)
if (mouse_debug == true) then
local col = 8;
if (checkCollision(self.move_x,self.move_y) == true) then col = 7 end
--pset(self.move_x,self.move_y,col)
end
drawCollision(self)
end,
update=function(self)
self.move_x = self.x
@ -36,9 +32,13 @@ function _init()
if (btn(2)) self.move_y -= self.speed
if (btn(3)) self.move_y += self.speed
--if (can_move(self.move_x,self.move_y)) then
--if (cmap(self) == false) then
self.x = self.move_x
self.y = self.move_y
--else
-- sfx(0)
--end
--if (checkCollision(col_x,col_y) == false) then
-- x = col_x
@ -76,8 +76,45 @@ function _update()
end
end
function checkCollision(x,y)
return fget(mget(x/tile_width,y/tile_height),0)
function drawCollision(o)
local right = o.x+o.w+o.speed
local left = o.x-o.speed
local bottom = o.y+o.h+o.speed
local top = o.y-o.speed
rect(left,top,right,bottom,2)
end
function checkCollision(o)
-- check right
-- devo controllare la collisione prima di calcolare il dove, per ogni input direzionale
local right = o.x+o.w+o.speed
local left = o.x-o.speed
local bottom = o.y+o.h+o.speed
local top = o.y-o.speed
if (not is_tile(1,top,left)
and not is_tile(1,top,right)
)
-- check left
-- check top
-- check bottom
return is_tile(0,x,y)
--return fget(mget(x/tile_width,y/tile_height),0)
end
function is_tile(tile_type,x,y)
local tile = mget(x/tile_width,y/tile_height)
local has_flag = fget(tile,tile_type)
return has_flag
end
function can_move(x,y)
return not is_tile(1,x,y)
end
function debug_mouse()