From 1ddab20ca5f9d61c03390663abea164b2a0f3aca Mon Sep 17 00:00:00 2001 From: Jason Hilder Date: Mon, 20 Jul 2026 08:07:45 +0200 Subject: [PATCH] Extra helper functions and collision checker. --- src/map.odin | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/map.odin b/src/map.odin index 4004197..b2e6e5d 100644 --- a/src/map.odin +++ b/src/map.odin @@ -93,6 +93,49 @@ grid_to_world :: proc(col: int, row: int) -> rl.Vector2 { return rl.Vector2{world_x, world_y} } +// requires world x/y and then gives the rectangle in pixels that matches that +get_world_tile_rect :: proc(grid_x, grid_y: f32) -> rl.Rectangle { + return rl.Rectangle { + grid_x * TILE_SIZE, + grid_y * TILE_SIZE, + TILE_SIZE, TILE_SIZE + } +} + +// Simple check for now +is_tile_solid :: proc(tile_type: Tile_Type) -> bool { + return tile_type == .WALL +} + +// takes in the next position/rect for check compares it +check_map_collision :: proc(rect: rl.Rectangle, game_map: Game_Map) -> bool { + + // collider top left point + min_grid := world_to_grid(rl.Vector2{rect.x, rect.y}) + // collider bot right point + max_grid := world_to_grid(rl.Vector2{rect.x + rect.width, rect.y + rect.height}) + + // @TODO bounds check? + for gy := min_grid[1]; gy <= max_grid[1]; gy += 1 { + + for gx := min_grid[0]; gx <= max_grid[0]; gx += 1 { + idx := (gy * game_map.width) + gx + tile := Tile_Type(game_map.data[idx]) + + tile_rect := get_world_tile_rect(f32(gx), f32(gy)); + if is_tile_solid(tile) { + if rl.CheckCollisionRecs(rect, tile_rect) { + return true + } + + } + } + + } + + return false +} + draw_map :: proc(current_map: Game_Map) { for value, index in current_map.data { row := index / current_map.width