diff options
| author | ne_mene <[email protected]> | 2026-03-13 17:52:50 +0100 |
|---|---|---|
| committer | ne_mene <[email protected]> | 2026-03-13 17:52:50 +0100 |
| commit | bc52ad3e36ecb0918eca17ea68a0dc54852a3392 (patch) | |
| tree | bc3f47ab2619b08ba15fa593a625c9f4269e0b26 | |
| parent | c11b02ac27e68a218ea80826765cd1631e95e6d0 (diff) | |
player is fat, ha ha
| -rw-r--r-- | main.lua | 25 | ||||
| -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 |
5 files changed, 83 insertions, 15 deletions
@@ -16,15 +16,14 @@ function love.load() set_scene(scn) + local tilemap = new_tilemap(64, 64) + scn.tilemap = tilemap + set_tile(tilemap, 0, 0, 1) + player = new_player(100, 100) ground1 = phys.Box.new(5, 50, 64, 16, {}) ground2 = phys.Box.new(64 - 16, 50 + 16, 16, 64, {}) - - local tilemap = new_tilemap(64, 64) - scn.tilemap = tilemap - - set_tile(tilemap, 0, 0, 1) end function love.update(dt) @@ -64,16 +63,16 @@ function love.draw(dt) fire_event(scn.on_ui) im.draw() - im.end_step() lg.print(tostring(love.timer.getFPS())) - lg.print("left "..tostring(player.box:touching_left()), 0, 20) - lg.print("right "..tostring(player.box:touching_right()), 0, 40) - lg.print("horizontal "..tostring(player.box:touching_horizontal()), 0, 60) - lg.print("up "..tostring(player.box:touching_up()), 0, 80) - lg.print("down "..tostring(player.box:touching_down()), 0, 100) - lg.print("vertical "..tostring(player.box:touching_vertical()), 0, 120) - lg.print("any "..tostring(player.box:touching()), 0, 140) + -- lg.print("left "..tostring(player.box:touching_left()), 0, 20) + -- lg.print("right "..tostring(player.box:touching_right()), 0, 40) + -- lg.print("horizontal "..tostring(player.box:touching_horizontal()), 0, 60) + -- lg.print("up "..tostring(player.box:touching_up()), 0, 80) + -- lg.print("down "..tostring(player.box:touching_down()), 0, 100) + -- lg.print("vertical "..tostring(player.box:touching_vertical()), 0, 120) + -- lg.print("any "..tostring(player.box:touching()), 0, 140) + input_step() end 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 |
