diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/init.lua | 2 | ||||
| -rw-r--r-- | src/objs/player.lua | 2 | ||||
| -rw-r--r-- | src/objs/tilemap.lua | 7 | ||||
| -rw-r--r-- | src/phys.lua | 62 |
4 files changed, 71 insertions, 2 deletions
diff --git a/src/init.lua b/src/init.lua index 637dafd..a8d106b 100644 --- a/src/init.lua +++ b/src/init.lua @@ -41,7 +41,7 @@ end function main_init() lg.setDefaultFilter("nearest", "nearest") viewport = lg.newCanvas(SCR_WIDTH, SCR_HEIGHT) - lw.setVSync(1) + lw.setVSync(0) load_textures_from() diff --git a/src/objs/player.lua b/src/objs/player.lua index c046c7c..82c5ed5 100644 --- a/src/objs/player.lua +++ b/src/objs/player.lua @@ -59,7 +59,7 @@ end function new_player(x, y) local ent = new_entity() - add_comp(ent, "Body", x, y, 16, 16, { + add_comp(ent, "Body", x, y, 64, 64, { offsetx = -8, offsety = -8, layers = {}, diff --git a/src/objs/tilemap.lua b/src/objs/tilemap.lua index 8b1ab76..7904a19 100644 --- a/src/objs/tilemap.lua +++ b/src/objs/tilemap.lua @@ -133,6 +133,13 @@ function get_tile(map, x, y) return map.tiledata[ID(map, x, y)] end +function has_tile(map, x, y) + if x < 0 or x >= map.width or y < 0 or y >= map.height then + return false + end + return map.tiledata[ID(map, x, y)] ~= 0 +end + function queue_tilemap_rebuild(tilemap) tilemap.needs_rebuild = true end 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 |
