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.
This commit is contained in:
@@ -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
|
||||
|
||||
24
src/lib.rs
24
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<ButtonInput<KeyCode>>,
|
||||
ship: Single<(&Transform, &physics::Velocity), With<Ship>>,
|
||||
ship: Single<(&Transform, &physics::Velocity, &mut Weapon), With<Ship>>,
|
||||
game_assets: Res<GameAssets>,
|
||||
mut commands: Commands,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let (ship_pos, ship_vel) = *ship;
|
||||
let (ship_pos, ship_vel, mut weapon) = ship.into_inner();
|
||||
|
||||
// Derive bullet velocity, add to the ship's velocity
|
||||
let bullet_vel = (ship_pos.rotation * Vec3::X).xy() * BULLET_SPEED;
|
||||
let bullet_vel = bullet_vel + ship_vel.0;
|
||||
// Tick the timer so the cooldown eventually finishes. Once it does, the
|
||||
// value will clamp at 0. The weapon is now ready.
|
||||
weapon.tick(time.delta());
|
||||
|
||||
// If the weapon is ready and the player presses the trigger,
|
||||
// spawn a bullet & reset the timer.
|
||||
if weapon.finished() && keyboard_input.pressed(KeyCode::Space) {
|
||||
weapon.reset();
|
||||
// Derive bullet velocity, add to the ship's velocity
|
||||
let bullet_vel = (ship_pos.rotation * Vec3::X).xy() * BULLET_SPEED;
|
||||
let bullet_vel = bullet_vel + ship_vel.0;
|
||||
|
||||
// TODO: create a timer for the gun fire rate.
|
||||
// For now, spawn one for each press of the spacebar.
|
||||
if keyboard_input.just_pressed(KeyCode::Space) {
|
||||
commands.spawn((
|
||||
Bullet,
|
||||
Collider::ball(0.2),
|
||||
|
||||
@@ -22,7 +22,7 @@ use bevy_rapier2d::prelude::{ActiveCollisionTypes, ActiveEvents, Collider, Senso
|
||||
|
||||
use crate::{
|
||||
AngularVelocity, GameAssets, GameState, Lives,
|
||||
config::ASTEROID_LIFETIME,
|
||||
config::{ASTEROID_LIFETIME, SHIP_FIRE_RATE},
|
||||
events::{AsteroidDestroy, BulletDestroy, ShipDestroy, SpawnAsteroid},
|
||||
machinery::Lifetime,
|
||||
physics::{Velocity, Wrapping},
|
||||
@@ -55,6 +55,10 @@ impl AsteroidSize {
|
||||
#[derive(Component)]
|
||||
pub struct Ship;
|
||||
|
||||
/// The ship's gun (is just a timer)
|
||||
#[derive(Component, Deref, DerefMut)]
|
||||
pub struct Weapon(Timer);
|
||||
|
||||
/// Marker component for bullets.
|
||||
#[derive(Component)]
|
||||
pub struct Bullet;
|
||||
@@ -144,6 +148,7 @@ pub fn spawn_player(mut commands: Commands, game_assets: Res<GameAssets>) {
|
||||
ActiveEvents::COLLISION_EVENTS,
|
||||
ActiveCollisionTypes::STATIC_STATIC,
|
||||
Ship,
|
||||
Weapon(Timer::from_seconds(1.0 / SHIP_FIRE_RATE, TimerMode::Once)),
|
||||
Wrapping,
|
||||
Velocity(Vec2::ZERO),
|
||||
AngularVelocity(0.0),
|
||||
|
||||
Reference in New Issue
Block a user