From bc52ad3e36ecb0918eca17ea68a0dc54852a3392 Mon Sep 17 00:00:00 2001 From: ne_mene Date: Fri, 13 Mar 2026 17:52:50 +0100 Subject: player is fat, ha ha --- src/phys.lua | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'src/phys.lua') diff --git a/src/phys.lua b/src/phys.lua index 8cf0613..2b1e101 100644 --- a/src/phys.lua +++ b/src/phys.lua @@ -56,6 +56,10 @@ function phys.Box.new( self.layers = opts.layers or { "hard" } self.mask = opts.mask or {} + local scene = get_current_scene() + assert(scene, "No scene set up.") + self.tilemap_mask = opts.tilemap_mask or { scene.tilemap } + self.touchx = 0 self.touchy = 0 @@ -100,10 +104,68 @@ function phys.Box:get_rect() self.height end +function box_in_tilemap(map, box) + local x, y, width, height = box:get_rect() + + local value + value = has_tile(map, to_tile_coords(x, y)) + if has_tile(map, to_tile_coords(x + width, y)) then + return true + end + if has_tile(map, to_tile_coords(x, y + height)) then + return true + end + if has_tile(map, to_tile_coords(x + width, y + height)) then + return true + end + + local extra_x = math.floor(width / (TILESIZE - 1)) + local step = width / (extra_x + 1) + + for i = 1, extra_x do + if has_tile( + map, to_tile_coords(x + step * i, y)) + then + return true + end + + if has_tile( + map, to_tile_coords(x + step * i, y + height)) + then + return true + end + end + + local extra_y = math.floor(height / (TILESIZE - 1)) + step = height / (extra_y + 1) + + for i = 1, extra_y do + if has_tile( + map, to_tile_coords(x, y + step * i)) + then + return true + end + + if has_tile( + map, to_tile_coords(x + width, y + step * i)) + then + return true + end + end + + return value +end + function collision_check(box) local box_x, box_y, box_width, box_height = box:get_rect() local other_x, other_y, other_width, other_height + for _, tilemap in ipairs(box.tilemap_mask) do + if box_in_tilemap(tilemap, box) then + return true + end + end + for _, layer in ipairs(box.mask) do for _, other in ipairs(world.layers[layer] or {}) do if other == box then -- cgit v1.3-2-g0d8e