From a4c336e337cff5ebb346420750660793f6908c9f Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sat, 7 Sep 2024 16:37:51 +0700 Subject: [PATCH] Fire bullets, move the character independent of where it's looking --- game.odin | 78 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/game.odin b/game.odin index 5c6c206..241ebb3 100644 --- a/game.odin +++ b/game.odin @@ -12,20 +12,21 @@ Tex :: rl.Texture Key :: rl.KeyboardKey Color :: rl.Color -SCREEN_WIDTH : i32 = 1000 -SCREEN_HEIGHT : i32 = 800 +SCREEN_WIDTH : i32 = 1600 +SCREEN_HEIGHT : i32 = 1000 BW := f32(SCREEN_WIDTH) BH := f32(SCREEN_HEIGHT) -BULLET_SPEED : f32 = 7.5 -BULLET_RADIUS : f32 = 5 +BULLET_SPEED : f32 = 30.5 +BULLET_RADIUS: f32 = 2 -ANGULAR_SPEED : f32 = 0.045 -THRUST_SPEED : f32 = 2.3 +WEAPON_ROF: f32 = 0.08 + +ANGULAR_SPEED: f32 = 0.045 +THRUST_SPEED: f32 = 2.3 Player :: struct { pos: Vec2, - points: [4]Vec2, angle: f32, vel: Vec2 } @@ -36,10 +37,11 @@ Bullet :: struct { } GameState :: struct { - dt: f32, - keymap: map[string]Key, - player: Player, - bullets: [dynamic]Bullet, + dt: f32, + keymap: map[string]Key, + player: Player, + bullets: [dynamic]Bullet, + weapon: f32, } init_state :: proc() -> ^GameState { @@ -59,37 +61,40 @@ init_state :: proc() -> ^GameState { return cloned } +get_rand_angle :: proc(min: i32, max: i32) -> f32 { return f32(rl.GetRandomValue(min, max)) * rl.DEG2RAD } + player_input :: proc(using s: ^GameState) { - speed :f32 = 1.5 - if rl.IsKeyDown(.D) { - theta := player.angle + math.PI / 2 - player.vel.x += math.cos(theta) * speed - player.vel.y += math.sin(theta) * speed - } - if rl.IsKeyDown(.A) { - theta := player.angle - math.PI / 2 - player.vel.x += math.cos(theta) * speed - player.vel.y += math.sin(theta) * speed - } - if rl.IsKeyDown(.W) { - player.vel.x += math.cos(player.angle) * speed - player.vel.y += math.sin(player.angle) * speed - } - if rl.IsKeyDown(.S) { - player.vel.x += math.cos(-player.angle) * speed - player.vel.y += math.sin(-player.angle) * speed - } - if rl.IsKeyPressed(.SPACE) { - b_vel := Vec2{ math.cos(player.angle) , math.sin(player.angle) } * BULLET_SPEED - append(&s.bullets, Bullet{player.points[0], b_vel}) + horizontal: f32 + vertical: f32 + if rl.IsKeyDown(.D) { horizontal = 1 } + if rl.IsKeyDown(.A) { horizontal = -1 } + if rl.IsKeyDown(.W) { vertical = -1 } + if rl.IsKeyDown(.S) { vertical = 1 } + speed :f32 = 600 + player.vel += rl.Vector2Normalize(Vec2{horizontal, vertical}) * (speed * dt) + if rl.IsMouseButtonDown(rl.MouseButton.LEFT) && weapon >= WEAPON_ROF { + angle := player.angle - get_rand_angle(0, 10) + (5 * rl.DEG2RAD) + b_vel := Vec2{ math.cos(angle) , math.sin(angle) } * BULLET_SPEED + append(&s.bullets, Bullet{player.pos, b_vel}) + weapon = 0 } } update :: proc(using s: ^GameState) { - player.vel *= 0.97 + player.vel *= 0.9 player.pos += player.vel * (THRUST_SPEED * s.dt) m_pos, p_pos := rl.GetMousePosition(), player.pos player.angle = math.atan2(m_pos.y - p_pos.y, m_pos.x - p_pos.x) + + 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 + } + weapon += dt } draw :: proc(using s: ^GameState) { @@ -103,6 +108,11 @@ draw :: proc(using s: ^GameState) { color := Color{255, 0, 0, 172} rl.DrawRectangleRoundedLines(reticle_rect, 0.2, 5, 5, color) rl.DrawCircleV(m_pos, 3, color) + + // Draw Bullets + for i := 0; i < len(s.bullets); i += 1 { + rl.DrawLineV(s.bullets[i].pos, s.bullets[i].pos - s.bullets[i].vel, rl.BLACK) + } } main :: proc() {