Fire bullets, move the character independent of where it's looking

This commit is contained in:
Joseph Ferano 2024-09-07 16:37:51 +07:00
parent 8b9cbda9b6
commit a4c336e337

View File

@ -12,20 +12,21 @@ Tex :: rl.Texture
Key :: rl.KeyboardKey Key :: rl.KeyboardKey
Color :: rl.Color Color :: rl.Color
SCREEN_WIDTH : i32 = 1000 SCREEN_WIDTH : i32 = 1600
SCREEN_HEIGHT : i32 = 800 SCREEN_HEIGHT : i32 = 1000
BW := f32(SCREEN_WIDTH) BW := f32(SCREEN_WIDTH)
BH := f32(SCREEN_HEIGHT) BH := f32(SCREEN_HEIGHT)
BULLET_SPEED : f32 = 7.5 BULLET_SPEED : f32 = 30.5
BULLET_RADIUS : f32 = 5 BULLET_RADIUS: f32 = 2
ANGULAR_SPEED : f32 = 0.045 WEAPON_ROF: f32 = 0.08
THRUST_SPEED : f32 = 2.3
ANGULAR_SPEED: f32 = 0.045
THRUST_SPEED: f32 = 2.3
Player :: struct { Player :: struct {
pos: Vec2, pos: Vec2,
points: [4]Vec2,
angle: f32, angle: f32,
vel: Vec2 vel: Vec2
} }
@ -36,10 +37,11 @@ Bullet :: struct {
} }
GameState :: struct { GameState :: struct {
dt: f32, dt: f32,
keymap: map[string]Key, keymap: map[string]Key,
player: Player, player: Player,
bullets: [dynamic]Bullet, bullets: [dynamic]Bullet,
weapon: f32,
} }
init_state :: proc() -> ^GameState { init_state :: proc() -> ^GameState {
@ -59,37 +61,40 @@ init_state :: proc() -> ^GameState {
return cloned 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) { player_input :: proc(using s: ^GameState) {
speed :f32 = 1.5 horizontal: f32
if rl.IsKeyDown(.D) { vertical: f32
theta := player.angle + math.PI / 2 if rl.IsKeyDown(.D) { horizontal = 1 }
player.vel.x += math.cos(theta) * speed if rl.IsKeyDown(.A) { horizontal = -1 }
player.vel.y += math.sin(theta) * speed if rl.IsKeyDown(.W) { vertical = -1 }
} if rl.IsKeyDown(.S) { vertical = 1 }
if rl.IsKeyDown(.A) { speed :f32 = 600
theta := player.angle - math.PI / 2 player.vel += rl.Vector2Normalize(Vec2{horizontal, vertical}) * (speed * dt)
player.vel.x += math.cos(theta) * speed if rl.IsMouseButtonDown(rl.MouseButton.LEFT) && weapon >= WEAPON_ROF {
player.vel.y += math.sin(theta) * speed angle := player.angle - get_rand_angle(0, 10) + (5 * rl.DEG2RAD)
} b_vel := Vec2{ math.cos(angle) , math.sin(angle) } * BULLET_SPEED
if rl.IsKeyDown(.W) { append(&s.bullets, Bullet{player.pos, b_vel})
player.vel.x += math.cos(player.angle) * speed weapon = 0
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})
} }
} }
update :: proc(using s: ^GameState) { update :: proc(using s: ^GameState) {
player.vel *= 0.97 player.vel *= 0.9
player.pos += player.vel * (THRUST_SPEED * s.dt) player.pos += player.vel * (THRUST_SPEED * s.dt)
m_pos, p_pos := rl.GetMousePosition(), player.pos m_pos, p_pos := rl.GetMousePosition(), player.pos
player.angle = math.atan2(m_pos.y - p_pos.y, m_pos.x - p_pos.x) 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) { draw :: proc(using s: ^GameState) {
@ -103,6 +108,11 @@ draw :: proc(using s: ^GameState) {
color := Color{255, 0, 0, 172} color := Color{255, 0, 0, 172}
rl.DrawRectangleRoundedLines(reticle_rect, 0.2, 5, 5, color) rl.DrawRectangleRoundedLines(reticle_rect, 0.2, 5, 5, color)
rl.DrawCircleV(m_pos, 3, 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() { main :: proc() {