Restart game on ENTER

This commit is contained in:
Joseph Ferano 2024-08-25 21:16:16 +07:00
parent 45d10ea271
commit 2f00e14632
2 changed files with 20 additions and 7 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/asteroids-odin /asteroids-odin
/web/

View File

@ -59,6 +59,7 @@ PlayerState :: union #no_nil {
GameState :: struct { GameState :: struct {
dt: f32, dt: f32,
restart: bool,
player_state: PlayerState, player_state: PlayerState,
bullets: [dynamic]Bullet, bullets: [dynamic]Bullet,
asteroids: [dynamic]Asteroid, 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} start_pos := Vec2{BW / 2 - SHIP_W / 2, BH / 2 - SHIP_H / 2}
state := GameState { player_state := Player {
dt = rl.GetFrameTime(),
player_state = Player {
pos = start_pos, pos = start_pos,
angle = -math.PI / 2, angle = -math.PI / 2,
}, }
update_ship_shape(&player_state)
state := GameState {
dt = rl.GetFrameTime(),
player_state = player_state,
bullets = make([dynamic]Bullet, 0, 64), bullets = make([dynamic]Bullet, 0, 64),
asteroids = make([dynamic]Asteroid, 0, 64), asteroids = make([dynamic]Asteroid, 0, 64),
asteroid_pop_idxs = make([dynamic]int, 0, 64), asteroid_pop_idxs = make([dynamic]int, 0, 64),
@ -140,7 +143,7 @@ player_input :: proc(s: ^GameState) {
} }
case Death: case Death:
if rl.IsKeyDown(.ENTER) { if rl.IsKeyDown(.ENTER) {
// restart game s.restart = true
} }
} }
} }
@ -238,6 +241,10 @@ draw2d :: proc(s: ^GameState) {
part := &player.flying_parts[i] part := &player.flying_parts[i]
rl.DrawLineEx(part.points[0], part.points[1], 1, rl.WHITE) 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 { for i := 0; i < len(s.asteroids); i += 1 {
rl.DrawRectangleLinesEx(s.asteroids[i].rect, 1, rl.WHITE) rl.DrawRectangleLinesEx(s.asteroids[i].rect, 1, rl.WHITE)
@ -247,14 +254,19 @@ draw2d :: proc(s: ^GameState) {
} }
} }
// @(export, link_name="_main")
main :: proc() { main :: proc() {
rl.SetTraceLogLevel(.ERROR) rl.SetTraceLogLevel(.ERROR)
rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Asteroids") rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Asteroids")
rl.SetTargetFPS(60) rl.SetTargetFPS(60)
state := init_game() state := init_state()
for !rl.WindowShouldClose() { for !rl.WindowShouldClose() {
if state.restart {
free(state)
state = init_state()
}
state.dt = rl.GetFrameTime() state.dt = rl.GetFrameTime()
player_input(state) player_input(state)