From 2f00e146329cf4f24ada4e0f8ead28a03bdc844f Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sun, 25 Aug 2024 21:16:16 +0700 Subject: [PATCH] Restart game on ENTER --- .gitignore | 1 + game.odin | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index d5a2b65..08e3e6a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /asteroids-odin +/web/ diff --git a/game.odin b/game.odin index 82ef83c..5959247 100644 --- a/game.odin +++ b/game.odin @@ -59,6 +59,7 @@ PlayerState :: union #no_nil { GameState :: struct { dt: f32, + restart: bool, player_state: PlayerState, bullets: [dynamic]Bullet, asteroids: [dynamic]Asteroid, @@ -88,14 +89,16 @@ swap_n_pop :: proc(bullets: ^[dynamic]$T, idx: int) { } } -init_game :: proc() -> ^GameState { +init_state :: proc() -> ^GameState { start_pos := Vec2{BW / 2 - SHIP_W / 2, BH / 2 - SHIP_H / 2} + player_state := Player { + pos = start_pos, + angle = -math.PI / 2, + } + update_ship_shape(&player_state) state := GameState { dt = rl.GetFrameTime(), - player_state = Player { - pos = start_pos, - angle = -math.PI / 2, - }, + player_state = player_state, bullets = make([dynamic]Bullet, 0, 64), asteroids = make([dynamic]Asteroid, 0, 64), asteroid_pop_idxs = make([dynamic]int, 0, 64), @@ -140,7 +143,7 @@ player_input :: proc(s: ^GameState) { } case Death: if rl.IsKeyDown(.ENTER) { - // restart game + s.restart = true } } } @@ -238,6 +241,10 @@ draw2d :: proc(s: ^GameState) { part := &player.flying_parts[i] rl.DrawLineEx(part.points[0], part.points[1], 1, rl.WHITE) } + game_over := cstring("GAME OVER") + size : i32 = 32 + width := f32(rl.MeasureText(game_over, size)) + rl.DrawText(game_over, i32(BW / 2 - width / 2), i32(BH / 2 - f32(size) / 2), size, rl.WHITE) } for i := 0; i < len(s.asteroids); i += 1 { rl.DrawRectangleLinesEx(s.asteroids[i].rect, 1, rl.WHITE) @@ -247,14 +254,19 @@ draw2d :: proc(s: ^GameState) { } } +// @(export, link_name="_main") main :: proc() { rl.SetTraceLogLevel(.ERROR) rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Asteroids") rl.SetTargetFPS(60) - state := init_game() + state := init_state() for !rl.WindowShouldClose() { + if state.restart { + free(state) + state = init_state() + } state.dt = rl.GetFrameTime() player_input(state)