Asteroid random positioning and velocity

This commit is contained in:
Joseph Ferano 2024-08-25 11:15:20 +07:00
parent f175f08445
commit abb87c2449

View File

@ -5,7 +5,7 @@ import "core:math"
import "core:fmt" import "core:fmt"
Vec2 :: [2]f32 Vec2 :: [2]f32
Rect :: [4]f32 Rect :: rl.Rectangle
SCREEN_WIDTH : i32 = 1280 SCREEN_WIDTH : i32 = 1280
SCREEN_HEIGHT : i32 = 720 SCREEN_HEIGHT : i32 = 720
@ -25,9 +25,8 @@ Bullet :: struct {
} }
Asteroid :: struct { Asteroid :: struct {
pos: Vec2, rect: Rect,
vel: Vec2, vel: Vec2,
size: Rect,
rot: f32, rot: f32,
} }
@ -84,11 +83,19 @@ main :: proc() {
bullet_pop_idxs := make([dynamic]int, 0, 64) bullet_pop_idxs := make([dynamic]int, 0, 64)
get_rand_angle :: proc(min: i32, max: i32) -> f32 {
return f32(rl.GetRandomValue(min, max)) * rl.DEG2RAD
}
for i := 0; i < 5; i += 1 { 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_size := f32(rl.GetRandomValue(25, 60))
rand_vy := f32(rl.GetRandomValue(1, 2)) * 0.5
asteroid := Asteroid { asteroid := Asteroid {
pos = {f32(rl.GetRandomValue(0, i32(bw))),-10}, rect = {rand_pos.x,rand_pos.y,rand_size,rand_size},
vel = {f32(rl.GetRandomValue(0, i32(bw))), f32(rl.GetRandomValue(0, i32(bw)))}, vel = Vec2{rand_angle, rand_vy} * 0.005,
size = {0,0,0,0},
rot = 0, rot = 0,
} }
append(&s.asteroids, asteroid) append(&s.asteroids, asteroid)
@ -116,10 +123,28 @@ main :: proc() {
s.ship_pos += s.ship_vel * (5000 * s.dt) s.ship_pos += s.ship_vel * (5000 * s.dt)
for i := 0; i < len(s.asteroids); i += 1 {
s.asteroids[i].rect.x += s.asteroids[i].vel.x
s.asteroids[i].rect.y += s.asteroids[i].vel.y
}
for i := 0; i < len(s.bullets); i += 1 { 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(&bullet_pop_idxs, i)
}
s.bullets[i].pos += s.bullets[i].vel s.bullets[i].pos += s.bullets[i].vel
} }
bpi_len := len(bullet_pop_idxs)
for i := 0; i < bpi_len; i += 1 {
swap_n_pop(&s.bullets, bullet_pop_idxs[i])
}
if bpi_len > 0 {
clear(&bullet_pop_idxs)
}
if s.ship_pos.x + SHIP_W < 0 { s.ship_pos.x = bw + SHIP_W} if s.ship_pos.x + SHIP_W < 0 { s.ship_pos.x = bw + SHIP_W}
if s.ship_pos.x - SHIP_W > bw { s.ship_pos.x = -SHIP_W} if s.ship_pos.x - SHIP_W > bw { s.ship_pos.x = -SHIP_W}
if s.ship_pos.y + SHIP_H < 0 { s.ship_pos.y = bh + SHIP_H} if s.ship_pos.y + SHIP_H < 0 { s.ship_pos.y = bh + SHIP_H}
@ -134,20 +159,12 @@ main :: proc() {
rl.DrawLineEx(v3, v4, 1, rl.WHITE) rl.DrawLineEx(v3, v4, 1, rl.WHITE)
rl.DrawLineEx(v4, v1, 1, rl.WHITE) rl.DrawLineEx(v4, v1, 1, rl.WHITE)
for i := 0; i < len(s.bullets); i += 1 { for i := 0; i < len(s.asteroids); i += 1 {
if ( s.bullets[i].pos.x < 0 || s.bullets[i].pos.x > bw rl.DrawRectangleLinesEx(s.asteroids[i].rect, 1, rl.WHITE)
|| s.bullets[i].pos.y < 0 || s.bullets[i].pos.y > bh) {
append(&bullet_pop_idxs, i)
}
rl.DrawCircleLinesV(s.bullets[i].pos, 5, rl.WHITE)
} }
bpi_len := len(bullet_pop_idxs) for i := 0; i < len(s.bullets); i += 1 {
for i := 0; i < bpi_len; i += 1 { rl.DrawCircleLinesV(s.bullets[i].pos, 5, rl.WHITE)
swap_n_pop(&s.bullets, bullet_pop_idxs[i])
}
if bpi_len > 0 {
clear(&bullet_pop_idxs)
} }
rl.EndDrawing() rl.EndDrawing()