From 573891918b6d86693767a915b0831bfdbf6dd215 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Mon, 26 Aug 2024 18:04:41 +0700 Subject: [PATCH] Replace swap n pop for unordered_remove and check bullet/asteroid collisions even if dead --- game.odin | 69 +++++++++++++++---------------------------------------- 1 file changed, 18 insertions(+), 51 deletions(-) diff --git a/game.odin b/game.odin index 5959247..aa12f3c 100644 --- a/game.odin +++ b/game.odin @@ -13,7 +13,6 @@ SCREEN_HEIGHT : i32 = 800 BW := f32(SCREEN_WIDTH) BH := f32(SCREEN_HEIGHT) - SCALE: f32 = 0.37 SHIP_W: f32 = 70 * SCALE SHIP_H: f32 = 100 * SCALE @@ -63,8 +62,6 @@ GameState :: struct { player_state: PlayerState, bullets: [dynamic]Bullet, asteroids: [dynamic]Asteroid, - bullet_pop_idxs: [dynamic]int, - asteroid_pop_idxs: [dynamic]int, } update_ship_shape :: proc(p : ^Player) { @@ -79,16 +76,6 @@ random_direction :: proc() -> Vec2 { return {math.cos(angle), math.sin(angle)} } -swap_n_pop :: proc(bullets: ^[dynamic]$T, idx: int) { - last := len(bullets) - 1 - if last == 0 { - clear(bullets) - } else { - bullets[last], bullets[idx] = bullets[idx], bullets[last] - pop(bullets) - } -} - init_state :: proc() -> ^GameState { start_pos := Vec2{BW / 2 - SHIP_W / 2, BH / 2 - SHIP_H / 2} player_state := Player { @@ -101,8 +88,6 @@ init_state :: proc() -> ^GameState { player_state = player_state, bullets = make([dynamic]Bullet, 0, 64), asteroids = make([dynamic]Asteroid, 0, 64), - asteroid_pop_idxs = make([dynamic]int, 0, 64), - bullet_pop_idxs = make([dynamic]int, 0, 64), } get_rand_angle :: proc(min: i32, max: i32) -> f32 { @@ -163,35 +148,6 @@ update :: proc(s: ^GameState) { } } } - for i := 0; i < len(s.bullets); i += 1 { - if ( s.bullets[i].pos.x < 0 || s.bullets[i].pos.x > BW - || s.bullets[i].pos.y < 0 || s.bullets[i].pos.y > BH) { - append(&s.bullet_pop_idxs, i) - } - s.bullets[i].pos += s.bullets[i].vel - for j := 0; j < len(s.asteroids); j += 1 { - if rl.CheckCollisionCircleRec(s.bullets[i].pos, BULLET_RADIUS, s.asteroids[j].rect) { - append(&s.bullet_pop_idxs, i) - append(&s.asteroid_pop_idxs, j) - } - } - } - - bpi_len := len(s.bullet_pop_idxs) - for i := 0; i < bpi_len; i += 1 { - swap_n_pop(&s.bullets, s.bullet_pop_idxs[i]) - } - if bpi_len > 0 { - clear(&s.bullet_pop_idxs) - } - api_len := len(s.asteroid_pop_idxs) - for i := 0; i < api_len; i += 1 { - swap_n_pop(&s.asteroids, s.asteroid_pop_idxs[i]) - } - if bpi_len > 0 { - clear(&s.asteroid_pop_idxs) - } - if ship_collision { // Randomize everything s.player_state = Death { @@ -214,18 +170,29 @@ update :: proc(s: ^GameState) { part.points[0] += part.vel part.points[1] += part.vel } - for i := 0; i < len(s.bullets); i += 1 { - if ( s.bullets[i].pos.x < 0 || s.bullets[i].pos.x > BW - || s.bullets[i].pos.y < 0 || s.bullets[i].pos.y > BH) { - append(&s.bullet_pop_idxs, i) - } - s.bullets[i].pos += s.bullets[i].vel - } for i := 0; i < len(s.asteroids); i += 1 { s.asteroids[i].rect.x += s.asteroids[i].vel.x * s.dt s.asteroids[i].rect.y += s.asteroids[i].vel.y * s.dt } } + for i := len(s.bullets) - 1; i >= 0; i -= 1 { + if ( s.bullets[i].pos.x < 0 || s.bullets[i].pos.x > BW + || s.bullets[i].pos.y < 0 || s.bullets[i].pos.y > BH) { + unordered_remove(&s.bullets, i) + continue + } + s.bullets[i].pos += s.bullets[i].vel + collided := false + for j := len(s.asteroids) - 1; j >= 0; j -= 1 { + if rl.CheckCollisionCircleRec(s.bullets[i].pos, BULLET_RADIUS, s.asteroids[j].rect) { + collided = true + unordered_remove(&s.asteroids, j) + } + } + if collided { + unordered_remove(&s.bullets, i) + } + } } draw2d :: proc(s: ^GameState) {