aboutsummaryrefslogtreecommitdiff
path: root/src/phys.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/phys.lua')
-rw-r--r--src/phys.lua62
1 files changed, 62 insertions, 0 deletions
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