From d21ccf3dc8019c3333e9ff3f62a712bf52146587 Mon Sep 17 00:00:00 2001 From: Stefan Stefanov Date: Sat, 10 Feb 2024 17:45:45 +0200 Subject: [PATCH] updated build script, added bugged alien shooting --- Makefile | 13 +++++++++---- game.odin | 47 ++++++++++++++++++++++++++++++++++++++--------- main.odin | 4 ++++ 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index b43c610..c32d29e 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,10 @@ DEB_FOLDER=debug_x64_win BIN_FOLDER=./bin build: clean - odin run . -out:bin/${EXE}.exe -debug + odin build . -out:${BIN_FOLDER}/${EXE}.exe -debug build_release: clean - odin build . -out:bin/${EXE}_rel.exe -o:speed -subsystem:windows + odin build . -out:${BIN_FOLDER}/${EXE}_rel.exe -o:speed -subsystem:windows clean: rm -rf ${BIN_FOLDER}/${EXE}.* @@ -18,6 +18,11 @@ package: build_release mkdir ${BIN_FOLDER}/${REL_FOLDER} cp -r ./assets/ ${BIN_FOLDER}/${REL_FOLDER}/ cp ${BIN_FOLDER}/${EXE}_rel.exe ${BIN_FOLDER}/${REL_FOLDER}/${EXE}.exe + tar.exe -a -c -f ${BIN_FOLDER}/release_x64_win.zip ${BIN_FOLDER}/release_x64_win + +run: build + ${BIN_FOLDER}/${EXE}.exe + +run_rel: build_release + ${BIN_FOLDER}/${EXE}_rel.exe -run: - space_invaders.exe diff --git a/game.odin b/game.odin index e1d2a5d..54891bb 100644 --- a/game.odin +++ b/game.odin @@ -3,6 +3,8 @@ package space_invaders import "core:c" import "core:log" import glm "core:math/linalg/glsl" +import "core:math/rand" +import "core:time" import rl "vendor:raylib" @@ -37,6 +39,8 @@ Bullet :: struct { position: glm.vec2, } +rng := rand.Rand{} + collideAABB :: proc(a_rect: glm.vec2, a_pos: glm.vec2, b_rect: glm.vec2, b_pos: glm.vec2) -> bool { return( a_pos.x < b_pos.x + f32(b_rect.x / 2) && @@ -84,6 +88,8 @@ setup_game :: proc(state: ^GameState) { update_game :: proc(state: ^GameState) { using state + rand.init(&rng, transmute(u64)time.time_to_unix_nano(time.now())) + // Poll for keyboard commands (input) { // If we're entering the game screen then we need to setup initial state of the game variables @@ -103,7 +109,7 @@ update_game :: proc(state: ^GameState) { // Press space to change to fire if (rl.IsKeyPressed(rl.KeyboardKey.SPACE)) { - fire_bullet(state, shot_from_player = true) + fire_bullet(&bullets, &bullet_index) } if (rl.IsKeyDown(rl.KeyboardKey.RIGHT)) { @@ -145,16 +151,28 @@ update_game :: proc(state: ^GameState) { } } - // for alien_idx in 0..< ALIENS_PER_ROW { - // - // } + // Go over the aliens on the bottom of the column & roll rng + // to shoot a bullet towards the player + for alien_idx in 0 ..< ALIENS_PER_ROW { + result := rand.uint32(&rng) + for row := ALIEN_ROWS-1; row >= 0; row -= 1 { + alien := aliens[(row * ALIENS_PER_ROW) + alien_idx] + if alien.alive { + if result % 3 == 0 { + fire_bullet(&bullets, &bullet_index, false, &alien) + } + break + } + } + } // Update bullets & aliens { for &bullet, bi in bullets { if !bullet.alive {continue} // Update bullet pos first - bullet.position.y -= f32(BULLET_SPEED * delta_time) + bullet_dir : f32 = bullet.player_bullet ? -1 : 1 + bullet.position.y += f32(BULLET_SPEED * delta_time) * bullet_dir } corner_alien_pos := shuffle_dir == .RIGHT ? aliens[ALIENS_PER_ROW - 1].position : aliens[0].position @@ -204,14 +222,25 @@ update_game :: proc(state: ^GameState) { } } -fire_bullet :: proc(state: ^GameState, shot_from_player := true) { +fire_bullet :: proc( + bullets: ^#soa[MAX_BULLETS]Bullet, + bullet_index: ^int, + shot_from_player := true, + alien: ^Alien = nil, +) { using state bullet := &bullets[bullet_index];bullet_index = (bullet_index + 1) % MAX_BULLETS bullet.alive = true - bullet.player_bullet = true - bullet.position = player_pos - bullet.position.y = bullet.position.y - ((PLAYER_RECT.y / 2) + BULLET_RECT.y / 2.0) + if shot_from_player { + bullet.player_bullet = true + bullet.position = player_pos + bullet.position.y = bullet.position.y - ((PLAYER_RECT.y / 2) + BULLET_RECT.y / 2) + } else if alien != nil { + bullet.player_bullet = false + bullet.position = alien.position + bullet.position.y = bullet.position.y + ((ALIEN_RECT.y / 2) + ALIEN_RECT.y / 2) + } log.info("Fired bullet: ", bullet) } diff --git a/main.odin b/main.odin index 0b5a717..fad77f9 100644 --- a/main.odin +++ b/main.odin @@ -101,6 +101,10 @@ main :: proc() { rl.InitWindow(state.screen_width, state.screen_height, state.title) defer rl.CloseWindow() + if !ODIN_DEBUG { + rl.SetExitKey(nil) + } + texture_atlas_image = rl.LoadImage(TEXTURE_ATLAS_PATH) texture_atlas = rl.LoadTextureFromImage(texture_atlas_image) rl.UnloadImage(texture_atlas_image)