Initial map interface.
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package game
|
||||
|
||||
import "core:math"
|
||||
import rl "vendor:raylib"
|
||||
|
||||
Tile_Type :: enum {
|
||||
// TEMP light gray
|
||||
FLOOR,
|
||||
// TEMP DARK gray
|
||||
WALL,
|
||||
// TREMP GREEN
|
||||
PICKUP,
|
||||
}
|
||||
|
||||
Game_Map :: struct {
|
||||
width: int,
|
||||
height: int,
|
||||
tile_size: int,
|
||||
data: [400]int
|
||||
}
|
||||
|
||||
Grid_Pos :: [2]int
|
||||
|
||||
map_init :: proc() -> Game_Map {
|
||||
// @TODO: store maps and load them later
|
||||
// NOTE: flat, row-major — data[row * width + col]
|
||||
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, 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, 2, 0, 0, 1,
|
||||
1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 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, 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,
|
||||
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
|
||||
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
|
||||
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 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, 1,
|
||||
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
|
||||
1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1,
|
||||
1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1,
|
||||
1, 0, 0, 1, 0, 0, 0, 0, 0, 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, 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,
|
||||
}
|
||||
|
||||
return Game_Map{
|
||||
width = 20,
|
||||
height = 20,
|
||||
tile_size = TILE_SIZE,
|
||||
data = map_data,
|
||||
}
|
||||
}
|
||||
|
||||
world_to_grid :: proc(pixel_pos: rl.Vector2, camera: rl.Camera2D) -> Grid_Pos {
|
||||
grid_space := rl.GetScreenToWorld2D(pixel_pos, camera)
|
||||
|
||||
// @TODO: I wonder if this floor and casting is expensive, look for a smarter way later
|
||||
pos_x := int(math.floor(grid_space.x))
|
||||
pos_y := int(math.floor(grid_space.y))
|
||||
|
||||
return Grid_Pos{pos_x, pos_y}
|
||||
}
|
||||
|
||||
grid_to_world :: proc(col: int, row: int) -> rl.Vector2 {
|
||||
world_x := f32(col * TILE_SIZE)
|
||||
world_y := f32(row * TILE_SIZE)
|
||||
|
||||
return rl.Vector2{world_x, world_y}
|
||||
}
|
||||
|
||||
draw_map :: proc(current_map: Game_Map) {
|
||||
for value, index in current_map.data {
|
||||
row := index / current_map.width
|
||||
column := index % current_map.width
|
||||
|
||||
color := rl.LIGHTGRAY
|
||||
if value == 1 do color = rl.DARKGRAY
|
||||
if value == 2 do color = rl.GREEN
|
||||
|
||||
rect := grid_to_world(column, row)
|
||||
rl.DrawRectangle(i32(rect.x), i32(rect.y), TILE_SIZE, TILE_SIZE, color)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user