Extra helper functions and collision checker.
This commit is contained in:
@@ -93,6 +93,49 @@ grid_to_world :: proc(col: int, row: int) -> rl.Vector2 {
|
|||||||
return rl.Vector2{world_x, world_y}
|
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) {
|
draw_map :: proc(current_map: Game_Map) {
|
||||||
for value, index in current_map.data {
|
for value, index in current_map.data {
|
||||||
row := index / current_map.width
|
row := index / current_map.width
|
||||||
|
|||||||
Reference in New Issue
Block a user