Initial map interface.

This commit is contained in:
2026-07-16 07:50:01 +02:00
parent 99c32198ec
commit f2110236e3
2 changed files with 98 additions and 8 deletions
+5 -2
View File
@@ -6,6 +6,7 @@ Game :: struct {
assets: Asset_Manager, assets: Asset_Manager,
camera: rl.Camera2D, camera: rl.Camera2D,
world: rl.RenderTexture2D, world: rl.RenderTexture2D,
current_map: Game_Map,
player: Player, player: Player,
debug: Debug debug: Debug
} }
@@ -20,6 +21,7 @@ game_init :: proc() -> Game {
game.player = player_init(&game.assets.player) game.player = player_init(&game.assets.player)
game.camera = camera_init(game.player, 1) game.camera = camera_init(game.player, 1)
game.world = rl.LoadRenderTexture(GAME_WIDTH, GAME_HEIGHT) game.world = rl.LoadRenderTexture(GAME_WIDTH, GAME_HEIGHT)
game.current_map = map_init()
// DEBUG // DEBUG
debug_init(&game) debug_init(&game)
@@ -32,7 +34,7 @@ game_should_run :: proc(game: ^Game) -> bool {
} }
game_update :: proc(game: ^Game) { game_update :: proc(game: ^Game) {
update_player(&game.player, &game.assets.player) update_player(&game.player, &game.assets.player, game.camera)
update_camera(game.player.pos, &game.camera) update_camera(game.player.pos, &game.camera)
// DEBUG // DEBUG
@@ -48,7 +50,8 @@ game_draw :: proc(game: ^Game) {
rl.BeginTextureMode(game.world) rl.BeginTextureMode(game.world)
rl.ClearBackground(rl.BLACK) rl.ClearBackground(rl.BLACK)
rl.BeginMode2D(cam) rl.BeginMode2D(cam)
renderer_draw_player(&game.player) draw_map(game.current_map)
draw_player(&game.player)
// DEBUG // DEBUG
debug_camera_draw(&game.camera, game.debug.camera_active) debug_camera_draw(&game.camera, game.debug.camera_active)
+87
View File
@@ -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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 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)
}
}