Collision detection and generic swap n' pop
This commit is contained in:
parent
abb87c2449
commit
6d9dbfc5f8
34
game.odin
34
game.odin
@ -7,8 +7,8 @@ import "core:fmt"
|
||||
Vec2 :: [2]f32
|
||||
Rect :: rl.Rectangle
|
||||
|
||||
SCREEN_WIDTH : i32 = 1280
|
||||
SCREEN_HEIGHT : i32 = 720
|
||||
SCREEN_WIDTH : i32 = 1000
|
||||
SCREEN_HEIGHT : i32 = 800
|
||||
|
||||
SCALE: f32 = 0.5
|
||||
SHIP_W: f32 = 70 * SCALE
|
||||
@ -18,6 +18,7 @@ THRUST_SPEED : f32 = 0.03
|
||||
ANGULAR_SPEED : f32 = 2.5
|
||||
|
||||
BULLET_SPEED : f32 = 0.05
|
||||
BULLET_RADIUS : f32 = 5
|
||||
|
||||
Bullet :: struct {
|
||||
pos: Vec2,
|
||||
@ -39,13 +40,6 @@ GameState :: struct {
|
||||
asteroids: [dynamic]Asteroid,
|
||||
}
|
||||
|
||||
rotate_vec2 :: proc(v : Vec2, angle: f32) -> Vec2 {
|
||||
return {
|
||||
v.x * math.cos(angle) - v.y * math.sin(angle),
|
||||
v.x * math.sin(angle) + v.y * math.cos(angle),
|
||||
}
|
||||
}
|
||||
|
||||
get_ship_shape :: proc(s : GameState) -> (Vec2, Vec2, Vec2, Vec2) {
|
||||
v1 := rl.Vector2Rotate({ SHIP_H*0.5, 0}, s.ship_angle) + s.ship_pos
|
||||
v2 := rl.Vector2Rotate({-SHIP_H*0.5, -SHIP_W*0.5}, s.ship_angle) + s.ship_pos
|
||||
@ -54,7 +48,7 @@ get_ship_shape :: proc(s : GameState) -> (Vec2, Vec2, Vec2, Vec2) {
|
||||
return v1, v2, v3, v4
|
||||
}
|
||||
|
||||
swap_n_pop :: proc(bullets: ^[dynamic]Bullet, idx: int) {
|
||||
swap_n_pop :: proc(bullets: ^[dynamic]$T, idx: int) {
|
||||
last := len(bullets) - 1
|
||||
if last == 0 {
|
||||
clear(bullets)
|
||||
@ -82,6 +76,7 @@ main :: proc() {
|
||||
bh := f32(SCREEN_HEIGHT)
|
||||
|
||||
bullet_pop_idxs := make([dynamic]int, 0, 64)
|
||||
asteroid_pop_idxs := make([dynamic]int, 0, 64)
|
||||
|
||||
get_rand_angle :: proc(min: i32, max: i32) -> f32 {
|
||||
return f32(rl.GetRandomValue(min, max)) * rl.DEG2RAD
|
||||
@ -89,8 +84,7 @@ main :: proc() {
|
||||
|
||||
for i := 0; i < 5; i += 1 {
|
||||
rand_angle := get_rand_angle(-50, 50)
|
||||
fmt.println(rand_angle)
|
||||
rand_pos := Vec2{ f32(rl.GetRandomValue(0, i32(bw))), 50 }
|
||||
rand_pos := Vec2{ f32(rl.GetRandomValue(0, i32(bw))), -50 }
|
||||
rand_size := f32(rl.GetRandomValue(25, 60))
|
||||
rand_vy := f32(rl.GetRandomValue(1, 2)) * 0.5
|
||||
asteroid := Asteroid {
|
||||
@ -134,6 +128,13 @@ main :: proc() {
|
||||
append(&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) {
|
||||
fmt.println(s.bullets[i], s.asteroids[j])
|
||||
append(&bullet_pop_idxs, i)
|
||||
append(&asteroid_pop_idxs, j)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bpi_len := len(bullet_pop_idxs)
|
||||
@ -143,6 +144,13 @@ main :: proc() {
|
||||
if bpi_len > 0 {
|
||||
clear(&bullet_pop_idxs)
|
||||
}
|
||||
api_len := len(asteroid_pop_idxs)
|
||||
for i := 0; i < api_len; i += 1 {
|
||||
swap_n_pop(&s.asteroids, asteroid_pop_idxs[i])
|
||||
}
|
||||
if bpi_len > 0 {
|
||||
clear(&asteroid_pop_idxs)
|
||||
}
|
||||
|
||||
|
||||
if s.ship_pos.x + SHIP_W < 0 { s.ship_pos.x = bw + SHIP_W}
|
||||
@ -164,7 +172,7 @@ main :: proc() {
|
||||
}
|
||||
|
||||
for i := 0; i < len(s.bullets); i += 1 {
|
||||
rl.DrawCircleLinesV(s.bullets[i].pos, 5, rl.WHITE)
|
||||
rl.DrawCircleLinesV(s.bullets[i].pos, BULLET_RADIUS, rl.WHITE)
|
||||
}
|
||||
|
||||
rl.EndDrawing()
|
||||
|
Loading…
x
Reference in New Issue
Block a user