aborto collisorio

This commit is contained in:
MaddoScientisto 2024-04-17 00:08:46 +02:00
commit c48f26f32a
2 changed files with 120 additions and 84 deletions

View file

@ -1,9 +1,11 @@
--[[pod_format="raw",created="2024-04-14 14:05:11",modified="2024-04-15 22:01:12",revision=116]]
--[[pod_format="raw",created="2024-04-14 14:05:11",modified="2024-04-16 22:07:51",revision=191]]
--include("/cirnofarm/src/actor.lua")
last_coll=0
mouse_debug = true
w=480
h=300
tile_width = 16
tile_height = 16
asdf = {top=0, side=1}
function _init()
@ -13,33 +15,47 @@ function _init()
y=128,
w=16,
h=16,
speed=1.2,
speed=0.05,
hflip=false,
spriteIndex=64,
move_x=0,
move_y=0,
noclip=false,
cm=true, -- Collide with map tiles
cb=true, -- Collide with world bounds
draw=function(self)
spr(self.spriteIndex,self.x,self.y, self.hflip)
drawCollision(self)
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,
self.move_x,self.move_y,last_coll),0,0,1)
end,
update=function(self)
self.move_x = self.x
self.move_y = self.y
--self.move_x = self.x
--self.move_y = self.y
if (btn(0)) self.move_x -= self.speed self.hflip = true
if (btn(1)) self.move_x += self.speed self.hflip = false
if (btn(2)) 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 (can_move(self.move_x,self.move_y)) then
--if (cmap(self) == false) then
self.x = self.move_x
self.y = self.move_y
if (not self.noclip) then self.move_x = 0 end
--else
-- sfx(0)
--end
-- sfx(0)
end
if (not self.noclip and checkCollision(self.x,self.y+self.move_y,self.w-1,self.h-1)) then
if (not self.noclip) then self.move_y = 0 end
end
self.move_x *= 0.95
self.move_y *= 0.95
self.x += self.move_x
self.y += self.move_y
--if (checkCollision(col_x,col_y) == false) then
-- x = col_x
-- y = col_y
@ -47,8 +63,7 @@ function _init()
end
})
tile_width = 16
tile_height = 16
end
function _draw()
@ -76,45 +91,62 @@ function _update()
end
end
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
function drawCollision(x,y,w,h)
for i=x,x+w,w do
pset(i,y,8)
--pset(i,y+h,18)
end
rect(left,top,right,bottom,2)
--[[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(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
function checkCollision(x,y,w,h)
local collide = false
last_coll = 0
for i=x,x+w,w do
if (not is_tile(1,top,left)
and not is_tile(1,top,right)
)
local sprite = mget(i,y)
local flag = fget(sprite)
if (flag == 1) then
collide = true
last_coll = 1
end
-- check left
-- check top
-- check bottom
return is_tile(0,x,y)
--return fget(mget(x/tile_width,y/tile_height),0)
--if (is_tile_solid(i,y) --[[or is_tile_solid(i,y+h)]]) then
-- collide = true
-- last_coll = 1
--end
end
--for i=y,y+h do
-- if (is_tile_solid(x,i) or is_tile_solid(x+w,i)) then
-- collide = true
-- end
--end
return collide
end
function is_tile(tile_type,x,y)
local tile = mget(x/tile_width,y/tile_height)
local has_flag = fget(tile,tile_type)
--last_coll = fget(tile)
return has_flag
end
function is_tile_solid(x,y)
return is_tile(1,x,y)
end
function can_move(x,y)
return not is_tile(1,x,y)
return is_tile(0,x,y)
end
function debug_mouse()
@ -123,8 +155,8 @@ function debug_mouse()
local y_offset = 5
-- tile size
local tw=16
local th=16
local tw=tile_width
local th=tile_height
-- window width and height
local w=480