--[[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 w=480 h=300 asdf = {top=0, side=1} function _init() player={} add(player, { x=128, y=128, w=16, h=16, speed=1.2, hflip=false, spriteIndex=64, move_x=0, move_y=0, 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) end, update=function(self) 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 (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 -- y = col_y --end end }) tile_width = 16 tile_height = 16 end function _draw() cls(0) --camera(x - 240, y - 135) drawMap() for p in all(player) do p:draw() end debug_mouse() end function drawMap() for i=2,1,-1 do map(fetch"map/0.map"[i].bmp) end end function _update() for p in all(player) do p: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 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() local mx,my = mouse() local x_offset = 5 local y_offset = 5 -- tile size local tw=16 local th=16 -- window width and height local w=480 local h=270 -- offset if box leaves screen if mx>w-20 then x_offset=-15 end if my>h-29 then y_offset=-24 end -- draw debug text box local tile_x = mx\tw local tile_y = my\th local sprite = mget(tile_x,tile_y) local flag = fget(sprite) rect((tile_x*tw)+tw,(tile_y*th)+th,tile_x*tw,(tile_y*th),8) rectfill(mx+x_offset-1,my+y_offset-1,mx+x_offset+14,my+y_offset+23+8,1) print(string.format("%d (%d)",tile_x,mx),mx+x_offset,my+y_offset,8) print(string.format("%d (%d)",tile_y,my),mx+x_offset,my+y_offset+8,9) print(sprite,mx+x_offset,my+y_offset+8*2,10) print(flag,mx+x_offset,my+y_offset+8+8*2,10) end function cmap(o) local ct=false local cb=false -- if colliding with map tiles if(o.cm) then local x1=o.x/tile_width local y1=o.y/tile_height local x2=(o.x+tile_width-1)/tile_width local y2=(o.y+tile_height-1)/tile_height local a=fget(mget(x1,y1),1) local b=fget(mget(x1,y2),1) local c=fget(mget(x2,y2),1) local d=fget(mget(x2,y1),1) ct=a or b or c or d end -- if colliding world bounds if(o.cw) then cb=(o.x<0 or o.x+tile_width>w or o.y<0 or o.y+tile_height>h) end return ct or cb end