diff --git a/game/game b/game/game index dd65767..0faf6ac 100755 Binary files a/game/game and b/game/game differ diff --git a/game/game.odin b/game/game.odin index 2467d08..aa216c6 100644 --- a/game/game.odin +++ b/game/game.odin @@ -16,6 +16,10 @@ camera : rl.Camera2D main :: proc() { rl.InitWindow(1280, 720, "Odin game") + flags : rl.ConfigFlags = {.VSYNC_HINT} + rl.SetConfigFlags(flags) + + rl.SetTargetFPS(60) player.position.x = CELL_SIZE * 5 player.position.y = CELL_SIZE * 5 @@ -29,7 +33,7 @@ main :: proc() { fill_world_grid_with_nothing(&world) - random_trees(&world) + place_random_trees(&world) game_loop() } @@ -53,10 +57,12 @@ game_loop :: proc() { rl.DrawFPS(5,5) - pos_string = fmt.aprint("Player POS:", get_player_grid_position(&player)) - pos_cstring = strings.clone_to_cstring(pos_string) + player_grid_pos := get_player_grid_position(&player) + player_grid_pos_tile := get_grid_tile(&world, vec2_to_vec2i(player_grid_pos)) + pos_string := rl.TextFormat("POS: %v : %v", player_grid_pos, player_grid_pos_tile.type) + + rl.DrawText(pos_string, 5, 25, 20, rl.RED) - rl.DrawText(pos_cstring, 5, 25, 20, rl.RED) rl.EndDrawing() } @@ -67,9 +73,10 @@ game_loop :: proc() { update :: proc() { + handle_player_input(&player, &world) + handle_window_resize() camera.target = {player.position.x + (CELL_SIZE / 2), player.position.y + (CELL_SIZE / 2)} - handle_player_input(&player) } draw :: proc() { @@ -77,21 +84,16 @@ draw :: proc() { draw_player(&player) } -random_trees :: proc(world:^World) { - for x in 0.. [2]f32 { + return {f32(v2i.x), f32(v2i.y)} +} + +vec2_to_vec2i :: proc(v2:[2]f32) -> Vec2i { + return {u32(v2.x), u32(v2.y)} +} diff --git a/game/player.odin b/game/player.odin index 93d726d..9a5c1d2 100644 --- a/game/player.odin +++ b/game/player.odin @@ -7,22 +7,39 @@ Player :: struct { position : rl.Vector2, } -handle_player_input :: proc(p : ^Player) { +handle_player_input :: proc(p : ^Player, w: ^World) { + + target_pos := get_player_grid_position(p) + + // fmt.printfln("MOVING TO: %v : %v", target_pos, get_grid_tile(w, vec2_to_vec2i(target_pos)).type) + if rl.IsKeyPressed(.RIGHT) { - p.position.x += CELL_SIZE + target_pos.x += 1 + if !will_collide(target_pos, w) { + player.position.x += CELL_SIZE + } } if rl.IsKeyPressed(.LEFT) { - p.position.x -= CELL_SIZE + target_pos.x -= 1 + if !will_collide(target_pos, w) { + player.position.x -= CELL_SIZE + } } if rl.IsKeyPressed(.UP) { - p.position.y -= CELL_SIZE + target_pos.y -= 1 + if !will_collide(target_pos, w) { + player.position.y -= CELL_SIZE + } } if rl.IsKeyPressed(.DOWN) { - p.position.y += CELL_SIZE + target_pos.y += 1 + if !will_collide(target_pos, w) { + player.position.y += CELL_SIZE + } } } @@ -37,3 +54,18 @@ get_player_grid_position :: proc(player:^Player) -> rl.Vector2 { draw_player :: proc(player:^Player) { draw_tile({27,0}, player.position, rl.DARKBLUE) } + +will_collide :: proc(pos:rl.Vector2, w:^World) -> bool { + if pos.y > WORLD_SIZE * CELL_SIZE || pos.x > WORLD_SIZE * CELL_SIZE { + return false + } + + tile := get_grid_tile(w, vec2_to_vec2i(pos)) + + #partial switch tile.type { + case .WALL: + return true + } + + return false +} diff --git a/game/tiles.odin b/game/tiles.odin new file mode 100644 index 0000000..d096885 --- /dev/null +++ b/game/tiles.odin @@ -0,0 +1,23 @@ +package game + +import rl "vendor:raylib" +import "core:math/rand" + +tree_tile := Tile { + type = .WALL, + tilemap_pos = {0,1}, + color = rl.DARKGREEN +} + +place_random_trees :: proc(w:^World) { + for x in 0..< len(w.grid) { + for y in 0..< len(w.grid) { + + chance := rand.int_max(100) + + if chance <= 10 { + w.grid[x][y] = tree_tile + } + } + } +} diff --git a/game/world.odin b/game/world.odin index 5b87ff5..2fc67da 100644 --- a/game/world.odin +++ b/game/world.odin @@ -24,10 +24,19 @@ TileType :: enum { FLOOR, } + set_grid_tile :: proc(w:^World, pos:Vec2i, t:Tile) { w.grid[pos.x][pos.y] = t } +get_grid_tile :: proc(w: ^World, pos: Vec2i) -> Tile { + if pos.x < 0 || pos.x >= len(w.grid) || pos.y < 0 || pos.y >= len(w.grid[0]) { + // fmt.printfln("Target [%v] outside of world bounds", pos) + return w.grid[0][0] // Default or error tile + } + return w.grid[pos.x][pos.y] +} + fill_world_grid_with_nothing :: proc(w:^World) { for x in 0..< len(w.grid) { for y in 0..