Adding asteroids
This commit is contained in:
parent
11af775673
commit
f175f08445
93
game.odin
93
game.odin
@ -4,6 +4,9 @@ import rl "vendor:raylib"
|
||||
import "core:math"
|
||||
import "core:fmt"
|
||||
|
||||
Vec2 :: [2]f32
|
||||
Rect :: [4]f32
|
||||
|
||||
SCREEN_WIDTH : i32 = 1280
|
||||
SCREEN_HEIGHT : i32 = 720
|
||||
|
||||
@ -14,13 +17,27 @@ SHIP_H: f32 = 100 * SCALE
|
||||
THRUST_SPEED : f32 = 0.03
|
||||
ANGULAR_SPEED : f32 = 2.5
|
||||
|
||||
Vec2 :: [2]f32
|
||||
BULLET_SPEED : f32 = 0.05
|
||||
|
||||
Bullet :: struct {
|
||||
pos: Vec2,
|
||||
vel: Vec2,
|
||||
}
|
||||
|
||||
Asteroid :: struct {
|
||||
pos: Vec2,
|
||||
vel: Vec2,
|
||||
size: Rect,
|
||||
rot: f32,
|
||||
}
|
||||
|
||||
GameState :: struct {
|
||||
dt: f32,
|
||||
ship_pos: Vec2,
|
||||
ship_angle: f32,
|
||||
ship_vel: Vec2,
|
||||
dt: f32,
|
||||
bullets: [dynamic]Bullet,
|
||||
asteroids: [dynamic]Asteroid,
|
||||
}
|
||||
|
||||
rotate_vec2 :: proc(v : Vec2, angle: f32) -> Vec2 {
|
||||
@ -31,25 +48,57 @@ rotate_vec2 :: proc(v : Vec2, angle: f32) -> Vec2 {
|
||||
}
|
||||
|
||||
get_ship_shape :: proc(s : GameState) -> (Vec2, Vec2, Vec2, Vec2) {
|
||||
v1 := rotate_vec2({ SHIP_H*0.5, 0}, s.ship_angle) + s.ship_pos
|
||||
v2 := rotate_vec2({-SHIP_H*0.5, -SHIP_W*0.5}, s.ship_angle) + s.ship_pos
|
||||
v3 := rotate_vec2({-SHIP_H*0.5*0.5, 0}, s.ship_angle) + s.ship_pos
|
||||
v4 := rotate_vec2({-SHIP_H*0.5, SHIP_W*0.5}, s.ship_angle) + s.ship_pos
|
||||
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
|
||||
v3 := rl.Vector2Rotate({-SHIP_H*0.5*0.5, 0}, s.ship_angle) + s.ship_pos
|
||||
v4 := rl.Vector2Rotate({-SHIP_H*0.5, SHIP_W*0.5}, s.ship_angle) + s.ship_pos
|
||||
return v1, v2, v3, v4
|
||||
}
|
||||
|
||||
swap_n_pop :: proc(bullets: ^[dynamic]Bullet, idx: int) {
|
||||
last := len(bullets) - 1
|
||||
if last == 0 {
|
||||
clear(bullets)
|
||||
} else {
|
||||
bullets[last], bullets[idx] = bullets[idx], bullets[last]
|
||||
pop(bullets)
|
||||
}
|
||||
}
|
||||
|
||||
main :: proc() {
|
||||
rl.SetTraceLogLevel(.ERROR)
|
||||
rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Asteroids")
|
||||
|
||||
s := GameState{{0,0}, -math.PI / 2, {0,0}, rl.GetFrameTime()}
|
||||
s := GameState{
|
||||
rl.GetFrameTime(),
|
||||
{0,0},
|
||||
-math.PI / 2,
|
||||
{0,0},
|
||||
make([dynamic]Bullet, 0, 64),
|
||||
make([dynamic]Asteroid, 0, 64),
|
||||
}
|
||||
s.ship_pos = {600, 350}
|
||||
|
||||
bw := f32(SCREEN_WIDTH)
|
||||
bh := f32(SCREEN_HEIGHT)
|
||||
|
||||
bullet_pop_idxs := make([dynamic]int, 0, 64)
|
||||
|
||||
for i := 0; i < 5; i += 1 {
|
||||
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},
|
||||
rot = 0,
|
||||
}
|
||||
append(&s.asteroids, asteroid)
|
||||
}
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
s.dt = rl.GetFrameTime()
|
||||
|
||||
v1, v2, v3, v4 := get_ship_shape(s)
|
||||
|
||||
if rl.IsKeyDown(.D) {
|
||||
s.ship_angle += ANGULAR_SPEED * s.dt
|
||||
}
|
||||
@ -60,25 +109,47 @@ main :: proc() {
|
||||
s.ship_vel.x += math.cos(s.ship_angle) * THRUST_SPEED * s.dt
|
||||
s.ship_vel.y += math.sin(s.ship_angle) * THRUST_SPEED * s.dt
|
||||
}
|
||||
if rl.IsKeyPressed(.SPACE) {
|
||||
b_vel := Vec2{ math.cos(s.ship_angle) , math.sin(s.ship_angle) } * BULLET_SPEED
|
||||
append(&s.bullets, Bullet{v1,b_vel})
|
||||
}
|
||||
|
||||
s.ship_pos += s.ship_vel * (5000 * s.dt)
|
||||
|
||||
for i := 0; i < len(s.bullets); i += 1 {
|
||||
s.bullets[i].pos += s.bullets[i].vel
|
||||
}
|
||||
|
||||
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 + < 0 { s.ship_pos.y = bh}
|
||||
if s.ship_pos.y > bh { s.ship_pos.y = 0}
|
||||
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 > bh { s.ship_pos.y = -SHIP_H}
|
||||
|
||||
rl.BeginDrawing()
|
||||
rl.ClearBackground(rl.BLACK)
|
||||
|
||||
// Draw ship
|
||||
v1, v2, v3, v4 := get_ship_shape(s)
|
||||
rl.DrawLineEx(v1, v2, 1, rl.WHITE)
|
||||
rl.DrawLineEx(v2, v3, 1, rl.WHITE)
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user