Added temp weapon switching system, finished glock17 firearmdata

This commit is contained in:
Franklin Blanco 2023-09-18 14:52:54 -07:00
parent af2e23b275
commit 2f0a056900
10 changed files with 108 additions and 42 deletions

View File

@ -29,6 +29,8 @@ pub struct FirearmData {
pub final_aimed_position: Vec3, pub final_aimed_position: Vec3,
/// Final position of hands when not aimed in /// Final position of hands when not aimed in
pub final_position: Vec3, pub final_position: Vec3,
pub scale_factor: f32,
} }
#[derive(Component)] #[derive(Component)]

View File

@ -2,4 +2,5 @@
#[derive(Clone)] #[derive(Clone)]
pub enum Caliber { pub enum Caliber {
NATO556, NATO556,
Parabellum9mm,
} }

View File

@ -3,19 +3,23 @@ use bevy::prelude::*;
use super::{caliber::Caliber, spray_pattern::FirearmSprayPattern}; use super::{caliber::Caliber, spray_pattern::FirearmSprayPattern};
#[allow(unused)]
#[derive(Component, PartialEq, Eq, PartialOrd, Ord, Clone)] #[derive(Component, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub enum Firearm { pub enum Firearm {
M4A1, M4A1,
Glock17,
} }
impl Firearm { impl Firearm {
pub fn firearm_data(&self) -> FirearmData { pub fn firearm_data(&self) -> FirearmData {
match self {
Firearm::M4A1 => {
FirearmData { FirearmData {
firing_point: Vec3 { x: -2.5, y: 0.0, z: 0.0 }, 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,
rebound_time_seconds: 0.1, rebound_time_seconds: 0.2,
vertical_recoil_modifier: 2.0, vertical_recoil_modifier: 2.0,
horizontal_recoil_modifier: 0.5, horizontal_recoil_modifier: 0.5,
recoil_pattern: FirearmSprayPattern { recoil_pattern: FirearmSprayPattern {
@ -42,13 +46,59 @@ impl Firearm {
y: -0.45, y: -0.45,
z: -2.7, z: -2.7,
}, },
scale_factor: 1.0,
asset_path: String::from("weapons/m4a1_rifle.glb"), asset_path: String::from("weapons/m4a1_rifle.glb"),
} }
},
Firearm::Glock17 => {
FirearmData {
firing_point: Vec3 { x: -2.5, y: 0.0, z: 0.0 },
caliber: Caliber::Parabellum9mm,
max_capacity: 17,
fire_rate: 600.0,
rebound_time_seconds: 0.1,
vertical_recoil_modifier: 5.0,
horizontal_recoil_modifier: 1.0,
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 // 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 // 10 for now
]),
},
final_aimed_rotation: Quat::from_rotation_x(0.026),
final_rotation: Quat::default(),
final_aimed_position: Vec3 {
// x: -0.003,
// y: -0.35,
// z: -10.6,
x: -0.003,
y: -0.5,
z: -1.6,
},
final_position: Vec3 {
x: 1.0,
y: -0.45,
z: -2.7,
},
scale_factor: 0.25,
asset_path: String::from("weapons/glock_17_pistol.glb"),
}
},
}
} }
pub fn holdable_object_data(&self) -> HoldableObjectData { pub fn holdable_object_data(&self) -> HoldableObjectData {
HoldableObjectData { match self {
Firearm::M4A1 => HoldableObjectData {
held_at: Vec3::ZERO, held_at: Vec3::ZERO,
y_rot: -90.0, y_rot: -90.0,
},
Firearm::Glock17 => HoldableObjectData {
held_at: Vec3::ZERO,
y_rot: 0.0,
},
} }
} }
} }

View File

@ -2,7 +2,7 @@ use bevy::prelude::*;
use crate::{ use crate::{
comps::core::markers::{firearm::{FirearmData, MagazineData}, holdable::InPlayerHands, player::{PlayerHand, Player}}, comps::core::markers::{firearm::{FirearmData, MagazineData}, holdable::InPlayerHands, player::{PlayerHand, Player}},
logic::core::guns::player_firing::PlayerFiringInfo, utils::rad_deg::radians_from_degrees, setup::{animations::AllFirearmAnimations, load_state::GameLoadState}, logic::core::guns::{player_firing::PlayerFiringInfo, firearm::Firearm}, utils::rad_deg::radians_from_degrees, setup::{animations::AllFirearmAnimations, load_state::GameLoadState, equipment::{EquipmentChangeEvent, Equipment}},
}; };
pub fn capture_hand_usage( pub fn capture_hand_usage(
@ -22,10 +22,15 @@ pub fn capture_hand_usage(
animation_clips: Res<Assets<AnimationClip>>, animation_clips: Res<Assets<AnimationClip>>,
game_load_state: Res<GameLoadState>, game_load_state: Res<GameLoadState>,
mut equipment_change_event_writer: EventWriter<EquipmentChangeEvent>,
) { ) {
if !game_load_state.player_loaded { if !game_load_state.player_loaded {
return; return;
} }
if player_query.iter().len() == 0 {
return;
}
if let Some(player_firearm) = player_query.single().0.equipment.primary_firearm.clone() { if let Some(player_firearm) = player_query.single().0.equipment.primary_firearm.clone() {
for mut player_firing_info in player_firing_info_query.iter_mut() { for mut player_firing_info in player_firing_info_query.iter_mut() {
player_firing_info.full_auto_timer.tick(time.delta()); player_firing_info.full_auto_timer.tick(time.delta());
@ -77,6 +82,11 @@ pub fn capture_hand_usage(
(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),
); );
} }
if keyboard_input.just_pressed(KeyCode::Key1) {
equipment_change_event_writer.send(EquipmentChangeEvent(Equipment{ primary_firearm: Some(Firearm::M4A1) }));
} else if keyboard_input.just_pressed(KeyCode::Key2) {
equipment_change_event_writer.send(EquipmentChangeEvent(Equipment{ primary_firearm: Some(Firearm::Glock17) }));
}
// 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() {

View File

@ -3,6 +3,6 @@ use bevy::prelude::*;
use crate::{setup::{spawn::SpawnPoint, equipment::Equipment}, comps::core::markers::player::{Player, PlayerData}, logic::core::guns::firearm::Firearm}; use crate::{setup::{spawn::SpawnPoint, equipment::Equipment}, comps::core::markers::player::{Player, PlayerData}, logic::core::guns::firearm::Firearm};
pub fn set_spawn_points(mut commands: Commands) { pub fn set_spawn_points(mut commands: Commands) {
commands.spawn(SpawnPoint { at: Transform::from_xyz(3.0, 5.0, 2.0), what: Player(PlayerData{ equipment: Equipment { primary_firearm: Some(Firearm::M4A1) }}) }); commands.spawn(SpawnPoint { at: Transform::from_xyz(3.0, 5.0, 2.0), what: Player(PlayerData{ equipment: Equipment { primary_firearm: Some(Firearm::Glock17) }}) });
} }

View File

@ -31,10 +31,11 @@ pub fn load_all_assets(
image_handle: skybox_handle, image_handle: skybox_handle,
}); });
let m4a1_gltf_asset = GltfAsset { asset_type: GltfAssetType::Firearm(Firearm::M4A1), asset: asset_server.load(Firearm::M4A1.firearm_data().asset_path) } ; let m4a1_gltf_asset = GltfAsset { asset_type: GltfAssetType::Firearm(Firearm::M4A1), asset: asset_server.load(Firearm::M4A1.firearm_data().asset_path) } ;
let glock17_gltf_asset = GltfAsset { asset_type: GltfAssetType::Firearm(Firearm::Glock17), asset: asset_server.load(Firearm::Glock17.firearm_data().asset_path) };
commands.insert_resource(GltfAssets { commands.insert_resource(GltfAssets {
assets: Vec::from([ assets: Vec::from([
m4a1_gltf_asset m4a1_gltf_asset,
glock17_gltf_asset
]) ])
}); });
game_load_state.assets_loaded = true; game_load_state.assets_loaded = true;

View File

@ -32,7 +32,7 @@ pub fn change_equipment(
let player_hands = player_hands_query.single_mut(); let player_hands = player_hands_query.single_mut();
commands.entity(player_hands).clear_children(); // Don't do this without keeping the state from the last mag commands.entity(player_hands).despawn_descendants(); // Don't do this without keeping the state from the last mag
if let Some(new_firearm) = equipment_change_event.0.primary_firearm.clone() { if let Some(new_firearm) = equipment_change_event.0.primary_firearm.clone() {
spawn_firearm_on_player_hands(&mut commands, player_firing_info, player_hands, &assets_gltf, &loaded_gltf_assets, new_firearm); spawn_firearm_on_player_hands(&mut commands, player_firing_info, player_hands, &assets_gltf, &loaded_gltf_assets, new_firearm);
} }
@ -54,13 +54,15 @@ fn spawn_firearm_on_player_hands(
) { ) {
if let Some(asset_handle) = assets_gltf.assets.iter().find(|asset| asset.asset_type == GltfAssetType::Firearm(firearm.clone())) { if let Some(asset_handle) = assets_gltf.assets.iter().find(|asset| asset.asset_type == GltfAssetType::Firearm(firearm.clone())) {
if let Some(gltf) = loaded_gltf_assets.get(&asset_handle.asset) { if let Some(gltf) = loaded_gltf_assets.get(&asset_handle.asset) {
let firearm_data: FirearmData = firearm.firearm_data();
let mut firearm_transform = Transform::from_xyz(0.0, 0.0, 0.0); let mut firearm_transform = Transform::from_xyz(0.0, 0.0, 0.0);
firearm_transform.rotate_y(utils::rad_deg::radians_from_degrees( firearm_transform.rotate_y(utils::rad_deg::radians_from_degrees(
firearm.holdable_object_data().y_rot, firearm.holdable_object_data().y_rot,
)); ));
firearm_transform.scale = firearm_transform.scale * firearm_data.scale_factor;
let scene = gltf.scenes[0].clone(); let scene = gltf.scenes[0].clone();
let firearm_data: FirearmData = firearm.firearm_data();
let firearm_entity = commands let firearm_entity = commands
.spawn(( .spawn((
SceneBundle { SceneBundle {

View File

@ -94,8 +94,8 @@ pub fn player_spawner(
.insert(PlayerFiringInfo::default()) .insert(PlayerFiringInfo::default())
.push_children(&[camera]); .push_children(&[camera]);
game_load_state.player_loaded = true;
equipment_change_event_writer.send(EquipmentChangeEvent(player_spawn_point.what.0.equipment.clone())); equipment_change_event_writer.send(EquipmentChangeEvent(player_spawn_point.what.0.equipment.clone()));
commands.entity(player_spawn_point_entity).despawn(); commands.entity(player_spawn_point_entity).despawn();
game_load_state.player_loaded = true;
} }
} }