Add logic for weapon firing/reloading
This commit is contained in:
parent
40805e9775
commit
27ee782aad
3 changed files with 59 additions and 1 deletions
|
@ -1,3 +1,4 @@
|
||||||
pub mod hud;
|
pub mod hud;
|
||||||
pub mod player;
|
pub mod player;
|
||||||
|
pub mod weapon;
|
||||||
pub mod world;
|
pub mod world;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use bevy::{input::system::exit_on_esc_system, prelude::*};
|
use bevy::{input::system::exit_on_esc_system, prelude::*};
|
||||||
use bevy_rapier3d::physics::RapierPhysicsPlugin;
|
use bevy_rapier3d::physics::RapierPhysicsPlugin;
|
||||||
use rake::{hud::HudPlugin, player::PlayerPlugin, world::WorldPlugin};
|
use rake::{hud::HudPlugin, player::PlayerPlugin, weapon::WeaponPlugin, world::WorldPlugin};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::build()
|
App::build()
|
||||||
|
@ -18,6 +18,7 @@ fn main() {
|
||||||
.add_plugin(PlayerPlugin)
|
.add_plugin(PlayerPlugin)
|
||||||
.add_plugin(HudPlugin)
|
.add_plugin(HudPlugin)
|
||||||
.add_plugin(WorldPlugin)
|
.add_plugin(WorldPlugin)
|
||||||
|
.add_plugin(WeaponPlugin)
|
||||||
.add_system(exit_on_esc_system.system())
|
.add_system(exit_on_esc_system.system())
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
56
src/weapon.rs
Normal file
56
src/weapon.rs
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
use bevy::{
|
||||||
|
input::{mouse::MouseButtonInput, ElementState},
|
||||||
|
prelude::*,
|
||||||
|
};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
pub struct WeaponPlugin;
|
||||||
|
|
||||||
|
impl Plugin for WeaponPlugin {
|
||||||
|
fn build(&self, app: &mut AppBuilder) {
|
||||||
|
app
|
||||||
|
.add_resource(WeaponData::default())
|
||||||
|
.add_system(shoot_system.system());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct WeaponData {
|
||||||
|
pub reload_timer: Timer,
|
||||||
|
pub reloading: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for WeaponData {
|
||||||
|
fn default() -> Self {
|
||||||
|
WeaponData {
|
||||||
|
reload_timer: Timer::new(Duration::from_secs(1), false),
|
||||||
|
reloading: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn shoot_system(
|
||||||
|
mut mouse_event_reader: Local<EventReader<MouseButtonInput>>,
|
||||||
|
mouse_button_events: Res<Events<MouseButtonInput>>,
|
||||||
|
time: Res<Time>,
|
||||||
|
mut weapon: ResMut<WeaponData>,
|
||||||
|
) {
|
||||||
|
if weapon.reloading {
|
||||||
|
weapon.reload_timer.tick(time.delta_seconds());
|
||||||
|
if weapon.reload_timer.finished() {
|
||||||
|
weapon.reload_timer.reset();
|
||||||
|
weapon.reloading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut fired = false;
|
||||||
|
for event in mouse_event_reader.iter(&mouse_button_events) {
|
||||||
|
if event.button == MouseButton::Left && event.state == ElementState::Pressed {
|
||||||
|
fired = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if fired && !weapon.reloading {
|
||||||
|
info!("weapon fired");
|
||||||
|
weapon.reloading = true;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue