From b01c9aad39af97770a63fcd2380f51e411e92681 Mon Sep 17 00:00:00 2001 From: Franklin Blanco Date: Sat, 16 Sep 2023 13:37:24 -0700 Subject: [PATCH] Added magazines --- src/comps/core/markers/firearm.rs | 11 +++- src/logic/core/guns/firearm.rs | 6 +-- src/logic/core/guns/spawn_firearm.rs | 3 +- src/logic/core/player/hands.rs | 75 +++++++++++++--------------- 4 files changed, 48 insertions(+), 47 deletions(-) diff --git a/src/comps/core/markers/firearm.rs b/src/comps/core/markers/firearm.rs index 70302f6..2faf874 100644 --- a/src/comps/core/markers/firearm.rs +++ b/src/comps/core/markers/firearm.rs @@ -4,14 +4,15 @@ use crate::logic::core::guns::{caliber::Caliber, spray_pattern::FirearmSprayPatt #[derive(Component)] pub struct FirearmData<'a> { - /// Where the bullets will come out of + /// Where the bullets will come out of, and muzzle flash will be spawned out of. pub firing_point: Vec3, pub caliber: Caliber, /// Placeholder until mags get implemented - pub max_capacity: u32, + pub max_capacity: usize, /// Rounds per minute pub fire_rate: f32, /// Amount of seconds it takes for gun to come down from shooting + /// Also is the time it takes for the gun to aim in or aim out. pub rebound_time_seconds: f32, pub asset_path: &'a str, @@ -28,3 +29,9 @@ pub struct FirearmData<'a> { /// Final position of hands when not aimed in pub final_position: Vec3, } + +#[derive(Component)] +pub struct MagazineData { + pub rounds_shot: usize, + pub max_capacity: usize, +} \ No newline at end of file diff --git a/src/logic/core/guns/firearm.rs b/src/logic/core/guns/firearm.rs index 3e2c32a..f9eeb61 100644 --- a/src/logic/core/guns/firearm.rs +++ b/src/logic/core/guns/firearm.rs @@ -10,7 +10,7 @@ pub enum Firearm { impl Firearm { pub fn firearm_data(&self) -> FirearmData { FirearmData { - firing_point: Vec3::ZERO, + firing_point: Vec3 { x: -2.5, y: 0.0, z: 0.0 }, caliber: Caliber::NATO556, max_capacity: 30, fire_rate: 800.0, @@ -19,10 +19,10 @@ impl Firearm { horizontal_recoil_modifier: 0.5, recoil_pattern: FirearmSprayPattern { vertical: Vec::from([ - 1.0, 1.2, 1.3, 1.6, 1.5, 1.7, 1.5, 1.5, 1.5, 2.0 + 1.0, 1.2, 1.3, 1.6, 1.5, 1.7, 1.5, 1.5, 1.5, 2.0 // 10 for now ]), horizontal: Vec::from([ - 1.0, 1.2, 1.3, -1.6, 1.5, -1.7, -1.5, 1.5, -1.5, 2.0 + 1.0, 1.2, 1.3, -1.6, 1.5, -1.7, -1.5, 1.5, -1.5, 2.0 // 10 for now ]), }, asset_path: "weapons/m4a1_rifle.glb#Scene0", diff --git a/src/logic/core/guns/spawn_firearm.rs b/src/logic/core/guns/spawn_firearm.rs index fa978a0..a996a50 100644 --- a/src/logic/core/guns/spawn_firearm.rs +++ b/src/logic/core/guns/spawn_firearm.rs @@ -3,7 +3,7 @@ use std::time::Duration; use bevy::prelude::*; use crate::{ - comps::core::markers::{holdable::InPlayerHands, player::PlayerHand}, + comps::core::markers::{holdable::InPlayerHands, player::PlayerHand, firearm::MagazineData}, constants::player_values::DEFAULT_PLAYER_FIREARM, utils, }; @@ -31,6 +31,7 @@ pub fn spawn_firearm_on_player_hands( }, DEFAULT_PLAYER_FIREARM.firearm_data(), DEFAULT_PLAYER_FIREARM.holdable_object_data(), + MagazineData { rounds_shot: 0, max_capacity: DEFAULT_PLAYER_FIREARM.firearm_data().max_capacity }, InPlayerHands, )) .id(); diff --git a/src/logic/core/player/hands.rs b/src/logic/core/player/hands.rs index bc6a36c..52f9b92 100644 --- a/src/logic/core/player/hands.rs +++ b/src/logic/core/player/hands.rs @@ -1,7 +1,7 @@ use bevy::prelude::*; use crate::{ - comps::core::markers::{firearm::FirearmData, holdable::InPlayerHands, player::PlayerHand}, + comps::core::markers::{firearm::{FirearmData, MagazineData}, holdable::InPlayerHands, player::PlayerHand}, logic::core::guns::player_firing::PlayerFiringInfo, utils::rad_deg::radians_from_degrees, }; @@ -12,22 +12,16 @@ pub fn capture_hand_usage( mut player_firing_info: ResMut, mut firearm_query: Query< - (&mut Transform, &'static FirearmData), + (&mut Transform, &'static FirearmData, &mut MagazineData), (With, Without), >, ) { player_firing_info.full_auto_timer.tick(time.delta()); - for (mut _firearm_transform, firearm_data) in firearm_query.iter_mut() { + for (mut _firearm_transform, firearm_data, mut magazine_data) in firearm_query.iter_mut() { for mut hand_transform in hand_query.iter_mut() { // AIMING IN/OUT if mouse_buttons.pressed(MouseButton::Right) { - - - //firearm_transform.rotation = firearm_transform.rotation.lerp(Quat::default(), (time.delta_seconds() / //firearm_data.rebound_time_seconds).clamp(0.0, 1.0)); - - - let rotation_lerp_quat = hand_transform.rotation.lerp( firearm_data.final_aimed_rotation, (time.delta_seconds() / firearm_data.rebound_time_seconds).clamp(0.0, 1.0), @@ -38,10 +32,6 @@ pub fn capture_hand_usage( ); hand_transform.rotation = rotation_lerp_quat; hand_transform.translation = position_lerp_vec3; - - - - } else { hand_transform.rotation = hand_transform .rotation @@ -50,39 +40,42 @@ pub fn capture_hand_usage( firearm_data.final_position, (time.delta_seconds() / 0.5).clamp(0.0, 1.0), ); - } - - - - - // Shooting & Recoil + // SHOOTING & RECOIL if mouse_buttons.pressed(MouseButton::Left) { if player_firing_info.full_auto_timer.finished() { + if magazine_data.rounds_shot < magazine_data.max_capacity { + // Get recoil numbers from patterns + let vertical_recoil_number: f32 = match firearm_data.recoil_pattern.vertical.get(player_firing_info.current_round_index) { + Some(vert_recoil_number) => *vert_recoil_number, + None => { + *firearm_data.recoil_pattern.vertical.last().expect("FOUND A FIREARM_DATA WITHOUT ANY FIREARM_RECOIL_PATTERN.") + }, + }; + let horizontal_recoil_number: f32 = match firearm_data.recoil_pattern.horizontal.get(player_firing_info.current_round_index) { + Some(horizontal_recoil_number) => *horizontal_recoil_number, + None => { + *firearm_data.recoil_pattern.horizontal.last().expect("FOUND A FIREARM_DATA WITHOUT ANY FIREARM_RECOIL_PATTERN.") + }, + }; + // Increment indexes and timers + player_firing_info.current_round_index += 1; + player_firing_info.last_shot_timestamp = time.elapsed_seconds(); + player_firing_info.full_auto_timer.reset(); + magazine_data.rounds_shot += 1; - let vertical_recoil_number: f32 = match firearm_data.recoil_pattern.vertical.get(player_firing_info.current_round_index) { - Some(vert_recoil_number) => *vert_recoil_number, - None => { - *firearm_data.recoil_pattern.vertical.last().expect("FOUND A FIREARM_DATA WITHOUT ANY FIREARM_RECOIL_PATTERN.") - }, - }; - let horizontal_recoil_number: f32 = match firearm_data.recoil_pattern.horizontal.get(player_firing_info.current_round_index) { - Some(horizontal_recoil_number) => *horizontal_recoil_number, - None => { - *firearm_data.recoil_pattern.horizontal.last().expect("FOUND A FIREARM_DATA WITHOUT ANY FIREARM_RECOIL_PATTERN.") - }, - }; - + // Apply recoil + hand_transform + .rotate_x(radians_from_degrees(firearm_data.vertical_recoil_modifier * vertical_recoil_number)); + hand_transform.rotate_y(radians_from_degrees(firearm_data.horizontal_recoil_modifier * horizontal_recoil_number)); + } else { + //TODO: play magazine empty sound + } - - player_firing_info.current_round_index += 1; - player_firing_info.last_shot_timestamp = time.elapsed_seconds(); - player_firing_info.full_auto_timer.reset(); - - - hand_transform - .rotate_x(radians_from_degrees(firearm_data.vertical_recoil_modifier * vertical_recoil_number)); - hand_transform.rotate_y(radians_from_degrees(firearm_data.horizontal_recoil_modifier * horizontal_recoil_number)); + } + } else { + if player_firing_info.full_auto_timer.finished() { + player_firing_info.current_round_index = 0; } } }