Added magazines

This commit is contained in:
Franklin Blanco 2023-09-16 13:37:24 -07:00
parent 56fc163b5c
commit b01c9aad39
4 changed files with 48 additions and 47 deletions

View File

@ -4,14 +4,15 @@ use crate::logic::core::guns::{caliber::Caliber, spray_pattern::FirearmSprayPatt
#[derive(Component)] #[derive(Component)]
pub struct FirearmData<'a> { 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 firing_point: Vec3,
pub caliber: Caliber, pub caliber: Caliber,
/// Placeholder until mags get implemented /// Placeholder until mags get implemented
pub max_capacity: u32, pub max_capacity: usize,
/// Rounds per minute /// Rounds per minute
pub fire_rate: f32, pub fire_rate: f32,
/// Amount of seconds it takes for gun to come down from shooting /// 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 rebound_time_seconds: f32,
pub asset_path: &'a str, pub asset_path: &'a str,
@ -28,3 +29,9 @@ pub struct FirearmData<'a> {
/// Final position of hands when not aimed in /// Final position of hands when not aimed in
pub final_position: Vec3, pub final_position: Vec3,
} }
#[derive(Component)]
pub struct MagazineData {
pub rounds_shot: usize,
pub max_capacity: usize,
}

View File

@ -10,7 +10,7 @@ pub enum Firearm {
impl Firearm { impl Firearm {
pub fn firearm_data(&self) -> FirearmData { pub fn firearm_data(&self) -> FirearmData {
FirearmData { FirearmData {
firing_point: Vec3::ZERO, firing_point: Vec3 { x: -2.5, y: 0.0, z: 0.0 },
caliber: Caliber::NATO556, caliber: Caliber::NATO556,
max_capacity: 30, max_capacity: 30,
fire_rate: 800.0, fire_rate: 800.0,
@ -19,10 +19,10 @@ impl Firearm {
horizontal_recoil_modifier: 0.5, horizontal_recoil_modifier: 0.5,
recoil_pattern: FirearmSprayPattern { recoil_pattern: FirearmSprayPattern {
vertical: Vec::from([ 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([ 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", asset_path: "weapons/m4a1_rifle.glb#Scene0",

View File

@ -3,7 +3,7 @@ use std::time::Duration;
use bevy::prelude::*; use bevy::prelude::*;
use crate::{ use crate::{
comps::core::markers::{holdable::InPlayerHands, player::PlayerHand}, comps::core::markers::{holdable::InPlayerHands, player::PlayerHand, firearm::MagazineData},
constants::player_values::DEFAULT_PLAYER_FIREARM, constants::player_values::DEFAULT_PLAYER_FIREARM,
utils, utils,
}; };
@ -31,6 +31,7 @@ pub fn spawn_firearm_on_player_hands(
}, },
DEFAULT_PLAYER_FIREARM.firearm_data(), DEFAULT_PLAYER_FIREARM.firearm_data(),
DEFAULT_PLAYER_FIREARM.holdable_object_data(), DEFAULT_PLAYER_FIREARM.holdable_object_data(),
MagazineData { rounds_shot: 0, max_capacity: DEFAULT_PLAYER_FIREARM.firearm_data().max_capacity },
InPlayerHands, InPlayerHands,
)) ))
.id(); .id();

View File

@ -1,7 +1,7 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::{ 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, 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<PlayerFiringInfo>, mut player_firing_info: ResMut<PlayerFiringInfo>,
mut firearm_query: Query< mut firearm_query: Query<
(&mut Transform, &'static FirearmData), (&mut Transform, &'static FirearmData, &mut MagazineData),
(With<InPlayerHands>, Without<PlayerHand>), (With<InPlayerHands>, Without<PlayerHand>),
>, >,
) { ) {
player_firing_info.full_auto_timer.tick(time.delta()); 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() { for mut hand_transform in hand_query.iter_mut() {
// AIMING IN/OUT // AIMING IN/OUT
if mouse_buttons.pressed(MouseButton::Right) { 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( let rotation_lerp_quat = hand_transform.rotation.lerp(
firearm_data.final_aimed_rotation, firearm_data.final_aimed_rotation,
(time.delta_seconds() / firearm_data.rebound_time_seconds).clamp(0.0, 1.0), (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.rotation = rotation_lerp_quat;
hand_transform.translation = position_lerp_vec3; hand_transform.translation = position_lerp_vec3;
} else { } else {
hand_transform.rotation = hand_transform hand_transform.rotation = hand_transform
.rotation .rotation
@ -50,39 +40,42 @@ pub fn capture_hand_usage(
firearm_data.final_position, firearm_data.final_position,
(time.delta_seconds() / 0.5).clamp(0.0, 1.0), (time.delta_seconds() / 0.5).clamp(0.0, 1.0),
); );
} }
// SHOOTING & RECOIL
// Shooting & Recoil
if mouse_buttons.pressed(MouseButton::Left) { if mouse_buttons.pressed(MouseButton::Left) {
if player_firing_info.full_auto_timer.finished() { 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) { // Apply recoil
Some(vert_recoil_number) => *vert_recoil_number, hand_transform
None => { .rotate_x(radians_from_degrees(firearm_data.vertical_recoil_modifier * vertical_recoil_number));
*firearm_data.recoil_pattern.vertical.last().expect("FOUND A FIREARM_DATA WITHOUT ANY FIREARM_RECOIL_PATTERN.") hand_transform.rotate_y(radians_from_degrees(firearm_data.horizontal_recoil_modifier * horizontal_recoil_number));
}, } else {
}; //TODO: play magazine empty sound
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.")
},
};
}
player_firing_info.current_round_index += 1; } else {
player_firing_info.last_shot_timestamp = time.elapsed_seconds(); if player_firing_info.full_auto_timer.finished() {
player_firing_info.full_auto_timer.reset(); player_firing_info.current_round_index = 0;
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));
} }
} }
} }