From abb87c2449146a40c445cbe9f09848a24f979bdb Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sun, 25 Aug 2024 11:15:20 +0700 Subject: [PATCH] Asteroid random positioning and velocity --- game.odin | 53 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/game.odin b/game.odin index 25f3b56..c7aa307 100644 --- a/game.odin +++ b/game.odin @@ -5,7 +5,7 @@ import "core:math" import "core:fmt" Vec2 :: [2]f32 -Rect :: [4]f32 +Rect :: rl.Rectangle SCREEN_WIDTH : i32 = 1280 SCREEN_HEIGHT : i32 = 720 @@ -25,9 +25,8 @@ Bullet :: struct { } Asteroid :: struct { - pos: Vec2, + rect: Rect, vel: Vec2, - size: Rect, rot: f32, } @@ -84,11 +83,19 @@ main :: proc() { 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 { + 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 { - pos = {f32(rl.GetRandomValue(0, i32(bw))),-10}, - vel = {f32(rl.GetRandomValue(0, i32(bw))), f32(rl.GetRandomValue(0, i32(bw)))}, - size = {0,0,0,0}, + rect = {rand_pos.x,rand_pos.y,rand_size,rand_size}, + vel = Vec2{rand_angle, rand_vy} * 0.005, rot = 0, } append(&s.asteroids, asteroid) @@ -116,10 +123,28 @@ main :: proc() { 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 { + 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 } + 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 > bw { s.ship_pos.x = -SHIP_W} 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(v4, v1, 1, rl.WHITE) - 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) - } - rl.DrawCircleLinesV(s.bullets[i].pos, 5, rl.WHITE) + for i := 0; i < len(s.asteroids); i += 1 { + rl.DrawRectangleLinesEx(s.asteroids[i].rect, 1, rl.WHITE) } - 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) + for i := 0; i < len(s.bullets); i += 1 { + rl.DrawCircleLinesV(s.bullets[i].pos, 5, rl.WHITE) } rl.EndDrawing()