From 708f514582c868b294f83330bd93dce50d4d7a30 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Thu, 14 Aug 2025 14:32:16 -0500 Subject: [PATCH] Implement a proper gun fire-rate mechanism There is now a `Weapon` component which is just a timer for the gun's fire rate. It is ticked every frame, clamping at the "ready" state (0 time remaining). The ship spawns with this thing, and the `input_ship_shoot` system has been updated to use it. --- src/config.rs | 1 + src/lib.rs | 24 +++++++++++++++--------- src/objects.rs | 7 ++++++- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/config.rs b/src/config.rs index 51b3ac8..e80e187 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,7 @@ pub(crate) const BACKGROUND_COLOR: Color = Color::srgb(0.3, 0.3, 0.3); pub(crate) const PLAYER_SHIP_COLOR: Color = Color::srgb(1.0, 1.0, 1.0); pub(crate) const SHIP_THRUSTER_COLOR_ACTIVE: Color = Color::srgb(1.0, 0.2, 0.2); pub(crate) const SHIP_THRUSTER_COLOR_INACTIVE: Color = Color::srgb(0.5, 0.5, 0.5); +pub(crate) const SHIP_FIRE_RATE: f32 = 3.0; // in bullets-per-second pub(crate) const ASTEROID_SMALL_COLOR: Color = Color::srgb(1.0, 0., 0.); pub(crate) const BULLET_COLOR: Color = Color::srgb(0.0, 0.1, 0.9); // TODO: asteroid medium & large diff --git a/src/lib.rs b/src/lib.rs index c477ae4..395c615 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ use crate::config::{ SHIP_THRUSTER_COLOR_INACTIVE, }; use crate::machinery::AsteroidSpawner; -use crate::objects::{Bullet, Ship}; +use crate::objects::{Bullet, Ship, Weapon}; use crate::physics::AngularVelocity; use bevy::prelude::*; @@ -160,19 +160,25 @@ fn input_ship_rotation( /// tick those timers. Maybe this system? fn input_ship_shoot( keyboard_input: Res>, - ship: Single<(&Transform, &physics::Velocity), With>, + ship: Single<(&Transform, &physics::Velocity, &mut Weapon), With>, game_assets: Res, mut commands: Commands, + time: Res