Added some definitions for each tile for better map loading.
This commit is contained in:
+42
-23
@@ -8,10 +8,27 @@ Tile_Type :: enum {
|
|||||||
FLOOR,
|
FLOOR,
|
||||||
// TEMP DARK gray
|
// TEMP DARK gray
|
||||||
WALL,
|
WALL,
|
||||||
|
// TEMP gray
|
||||||
|
LOW_WALL,
|
||||||
// TREMP GREEN
|
// TREMP GREEN
|
||||||
PICKUP,
|
PICKUP,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Tile_Def :: struct {
|
||||||
|
solid: bool,
|
||||||
|
collider_offset_x, collider_offset_y: f32,
|
||||||
|
collider_w, collider_h: f32
|
||||||
|
}
|
||||||
|
|
||||||
|
// awesome odin sets the fixed size to the ammount of enum options
|
||||||
|
tile_defs: [Tile_Type]Tile_Def = {
|
||||||
|
.FLOOR = { solid = false },
|
||||||
|
.WALL = { solid = true, collider_offset_x = 0, collider_offset_y = 0, collider_w = TILE_SIZE, collider_h = TILE_SIZE },
|
||||||
|
.LOW_WALL = { solid = true, collider_offset_x = 0, collider_offset_y = TILE_SIZE/2, collider_w = TILE_SIZE, collider_h = TILE_SIZE/2 },
|
||||||
|
.PICKUP = { solid = false },
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Game_Map :: struct {
|
Game_Map :: struct {
|
||||||
tile_size: int,
|
tile_size: int,
|
||||||
width: int,
|
width: int,
|
||||||
@@ -29,8 +46,8 @@ map_init :: proc() -> Game_Map {
|
|||||||
map_data := [400]int{
|
map_data := [400]int{
|
||||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||||
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
|
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
|
||||||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, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1,
|
||||||
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
|
1, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
|
||||||
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
|
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,11 +118,11 @@ grid_to_world :: proc(col: int, row: int) -> rl.Vector2 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// requires world x/y and then gives the rectangle in pixels that matches that
|
// 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 {
|
get_world_tile_rect :: proc(gx, gy, offset_x, offset_y, w, h: f32) -> rl.Rectangle {
|
||||||
return rl.Rectangle {
|
return rl.Rectangle {
|
||||||
grid_x * TILE_SIZE,
|
(gx * TILE_SIZE) + offset_x,
|
||||||
grid_y * TILE_SIZE,
|
(gy * TILE_SIZE) + offset_y,
|
||||||
TILE_SIZE, TILE_SIZE
|
w, h
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,19 +139,18 @@ check_map_collision :: proc(rect: rl.Rectangle, game_map: Game_Map) -> bool {
|
|||||||
// collider bot right point
|
// collider bot right point
|
||||||
max_grid := world_to_grid(rl.Vector2{rect.x + rect.width, rect.y + rect.height})
|
max_grid := world_to_grid(rl.Vector2{rect.x + rect.width, rect.y + rect.height})
|
||||||
|
|
||||||
// @TODO bounds check?
|
// check all points along the x axis per y axis 'row'
|
||||||
for gy := min_grid[1]; gy <= max_grid[1]; gy += 1 {
|
for gy := min_grid[1]; gy <= max_grid[1]; gy += 1 {
|
||||||
|
|
||||||
for gx := min_grid[0]; gx <= max_grid[0]; gx += 1 {
|
for gx := min_grid[0]; gx <= max_grid[0]; gx += 1 {
|
||||||
idx := (gy * game_map.width) + gx
|
idx := (gy * game_map.width) + gx
|
||||||
tile := Tile_Type(game_map.data[idx])
|
tile := Tile_Type(game_map.data[idx])
|
||||||
|
def := tile_defs[tile]
|
||||||
|
|
||||||
tile_rect := get_world_tile_rect(f32(gx), f32(gy));
|
if def.solid {
|
||||||
if is_tile_solid(tile) {
|
tile_rect := get_world_tile_rect( f32(gx), f32(gy), def.collider_offset_x, def.collider_offset_y, def.collider_w, def.collider_h);
|
||||||
if rl.CheckCollisionRecs(rect, tile_rect) {
|
if rl.CheckCollisionRecs(rect, tile_rect) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,16 +166,19 @@ draw_map :: proc(current_map: Game_Map) {
|
|||||||
|
|
||||||
tile := Tile_Type(value)
|
tile := Tile_Type(value)
|
||||||
color: rl.Color
|
color: rl.Color
|
||||||
switch tile {
|
|
||||||
case .FLOOR:
|
|
||||||
color = rl.LIGHTGRAY
|
|
||||||
case .WALL:
|
|
||||||
color = rl.DARKGRAY
|
|
||||||
case .PICKUP:
|
|
||||||
color = rl.GREEN
|
|
||||||
}
|
|
||||||
|
|
||||||
rect := grid_to_world(column, row)
|
rect := grid_to_world(column, row)
|
||||||
rl.DrawRectangle(i32(rect.x), i32(rect.y), TILE_SIZE, TILE_SIZE, color)
|
|
||||||
|
// base floor color, draw full tile first to avoid background bleeding
|
||||||
|
rl.DrawRectangle(i32(rect.x), i32(rect.y), TILE_SIZE, TILE_SIZE, rl.LIGHTGRAY)
|
||||||
|
|
||||||
|
// anything that isn't plain floor draws on top of the base
|
||||||
|
#partial switch tile {
|
||||||
|
case .WALL:
|
||||||
|
rl.DrawRectangle(i32(rect.x), i32(rect.y), TILE_SIZE, TILE_SIZE, rl.DARKGRAY)
|
||||||
|
case .LOW_WALL:
|
||||||
|
rl.DrawRectangle(i32(rect.x), i32(rect.y + TILE_SIZE / 2), TILE_SIZE, TILE_SIZE / 2, rl.GRAY)
|
||||||
|
case .PICKUP:
|
||||||
|
rl.DrawRectangle(i32(rect.x), i32(rect.y), TILE_SIZE, TILE_SIZE, rl.GREEN)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user