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 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_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_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 ASTEROID_SMALL_COLOR: Color = Color::srgb(1.0, 0., 0.);
|
||||||
pub(crate) const BULLET_COLOR: Color = Color::srgb(0.0, 0.1, 0.9);
|
pub(crate) const BULLET_COLOR: Color = Color::srgb(0.0, 0.1, 0.9);
|
||||||
// TODO: asteroid medium & large
|
// TODO: asteroid medium & large
|
||||||
|
|||||||
24
src/lib.rs
24
src/lib.rs
@@ -16,7 +16,7 @@ use crate::config::{
|
|||||||
SHIP_THRUSTER_COLOR_INACTIVE,
|
SHIP_THRUSTER_COLOR_INACTIVE,
|
||||||
};
|
};
|
||||||
use crate::machinery::AsteroidSpawner;
|
use crate::machinery::AsteroidSpawner;
|
||||||
use crate::objects::{Bullet, Ship};
|
use crate::objects::{Bullet, Ship, Weapon};
|
||||||
use crate::physics::AngularVelocity;
|
use crate::physics::AngularVelocity;
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
@@ -160,19 +160,25 @@ fn input_ship_rotation(
|
|||||||
/// tick those timers. Maybe this system?
|
/// tick those timers. Maybe this system?
|
||||||
fn input_ship_shoot(
|
fn input_ship_shoot(
|
||||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
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>,
|
game_assets: Res<GameAssets>,
|
||||||
mut commands: Commands,
|
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
|
// Tick the timer so the cooldown eventually finishes. Once it does, the
|
||||||
let bullet_vel = (ship_pos.rotation * Vec3::X).xy() * BULLET_SPEED;
|
// value will clamp at 0. The weapon is now ready.
|
||||||
let bullet_vel = bullet_vel + ship_vel.0;
|
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((
|
commands.spawn((
|
||||||
Bullet,
|
Bullet,
|
||||||
Collider::ball(0.2),
|
Collider::ball(0.2),
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ use bevy_rapier2d::prelude::{ActiveCollisionTypes, ActiveEvents, Collider, Senso
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
AngularVelocity, GameAssets, GameState, Lives,
|
AngularVelocity, GameAssets, GameState, Lives,
|
||||||
config::ASTEROID_LIFETIME,
|
config::{ASTEROID_LIFETIME, SHIP_FIRE_RATE},
|
||||||
events::{AsteroidDestroy, BulletDestroy, ShipDestroy, SpawnAsteroid},
|
events::{AsteroidDestroy, BulletDestroy, ShipDestroy, SpawnAsteroid},
|
||||||
machinery::Lifetime,
|
machinery::Lifetime,
|
||||||
physics::{Velocity, Wrapping},
|
physics::{Velocity, Wrapping},
|
||||||
@@ -55,6 +55,10 @@ impl AsteroidSize {
|
|||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Ship;
|
pub struct Ship;
|
||||||
|
|
||||||
|
/// The ship's gun (is just a timer)
|
||||||
|
#[derive(Component, Deref, DerefMut)]
|
||||||
|
pub struct Weapon(Timer);
|
||||||
|
|
||||||
/// Marker component for bullets.
|
/// Marker component for bullets.
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Bullet;
|
pub struct Bullet;
|
||||||
@@ -144,6 +148,7 @@ pub fn spawn_player(mut commands: Commands, game_assets: Res<GameAssets>) {
|
|||||||
ActiveEvents::COLLISION_EVENTS,
|
ActiveEvents::COLLISION_EVENTS,
|
||||||
ActiveCollisionTypes::STATIC_STATIC,
|
ActiveCollisionTypes::STATIC_STATIC,
|
||||||
Ship,
|
Ship,
|
||||||
|
Weapon(Timer::from_seconds(1.0 / SHIP_FIRE_RATE, TimerMode::Once)),
|
||||||
Wrapping,
|
Wrapping,
|
||||||
Velocity(Vec2::ZERO),
|
Velocity(Vec2::ZERO),
|
||||||
AngularVelocity(0.0),
|
AngularVelocity(0.0),
|
||||||
|
|||||||
Reference in New Issue
Block a user