From 2f0a05690073a412b93de35d2248b5ba1c345e7a Mon Sep 17 00:00:00 2001 From: Franklin Blanco Date: Mon, 18 Sep 2023 14:52:54 -0700 Subject: [PATCH] Added temp weapon switching system, finished glock17 firearmdata --- .../{Glock17.glb => glock_17_pistol.glb} | Bin src/comps/core/markers/firearm.rs | 2 + src/constants/mod.rs | 2 +- src/logic/core/guns/caliber.rs | 1 + src/logic/core/guns/firearm.rs | 118 +++++++++++++----- src/logic/core/player/hands.rs | 12 +- src/scenes/scene1/spawn_points.rs | 2 +- src/setup/assets.rs | 5 +- src/setup/equipment.rs | 6 +- src/setup/spawners/player.rs | 2 +- 10 files changed, 108 insertions(+), 42 deletions(-) rename assets/weapons/{Glock17.glb => glock_17_pistol.glb} (100%) diff --git a/assets/weapons/Glock17.glb b/assets/weapons/glock_17_pistol.glb similarity index 100% rename from assets/weapons/Glock17.glb rename to assets/weapons/glock_17_pistol.glb diff --git a/src/comps/core/markers/firearm.rs b/src/comps/core/markers/firearm.rs index f0e72dd..a6c2378 100644 --- a/src/comps/core/markers/firearm.rs +++ b/src/comps/core/markers/firearm.rs @@ -29,6 +29,8 @@ pub struct FirearmData { pub final_aimed_position: Vec3, /// Final position of hands when not aimed in pub final_position: Vec3, + + pub scale_factor: f32, } #[derive(Component)] diff --git a/src/constants/mod.rs b/src/constants/mod.rs index e48f45e..44c5dfb 100644 --- a/src/constants/mod.rs +++ b/src/constants/mod.rs @@ -1 +1 @@ -pub mod player_values; +pub mod player_values; \ No newline at end of file diff --git a/src/logic/core/guns/caliber.rs b/src/logic/core/guns/caliber.rs index 01ce1d0..9dc6241 100644 --- a/src/logic/core/guns/caliber.rs +++ b/src/logic/core/guns/caliber.rs @@ -2,4 +2,5 @@ #[derive(Clone)] pub enum Caliber { NATO556, + Parabellum9mm, } diff --git a/src/logic/core/guns/firearm.rs b/src/logic/core/guns/firearm.rs index 0100ece..f8288f0 100644 --- a/src/logic/core/guns/firearm.rs +++ b/src/logic/core/guns/firearm.rs @@ -3,52 +3,102 @@ use bevy::prelude::*; use super::{caliber::Caliber, spray_pattern::FirearmSprayPattern}; +#[allow(unused)] #[derive(Component, PartialEq, Eq, PartialOrd, Ord, Clone)] pub enum Firearm { M4A1, + Glock17, } impl Firearm { pub fn firearm_data(&self) -> FirearmData { - FirearmData { - firing_point: Vec3 { x: -2.5, y: 0.0, z: 0.0 }, - caliber: Caliber::NATO556, - max_capacity: 30, - fire_rate: 800.0, - rebound_time_seconds: 0.1, - vertical_recoil_modifier: 2.0, - 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 // 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 - ]), + match self { + Firearm::M4A1 => { + FirearmData { + firing_point: Vec3 { x: -2.5, y: 0.0, z: 0.0 }, + caliber: Caliber::NATO556, + max_capacity: 30, + fire_rate: 800.0, + rebound_time_seconds: 0.2, + vertical_recoil_modifier: 2.0, + 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 // 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.35, + z: -1.6, + }, + final_position: Vec3 { + x: 0.6, + y: -0.45, + z: -2.7, + }, + scale_factor: 1.0, + asset_path: String::from("weapons/m4a1_rifle.glb"), + } }, - - 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.35, - z: -1.6, + 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"), + } }, - final_position: Vec3 { - x: 0.6, - y: -0.45, - z: -2.7, - }, - asset_path: String::from("weapons/m4a1_rifle.glb"), } } pub fn holdable_object_data(&self) -> HoldableObjectData { - HoldableObjectData { - held_at: Vec3::ZERO, - y_rot: -90.0, + match self { + Firearm::M4A1 => HoldableObjectData { + held_at: Vec3::ZERO, + y_rot: -90.0, + }, + Firearm::Glock17 => HoldableObjectData { + held_at: Vec3::ZERO, + y_rot: 0.0, + }, } } } diff --git a/src/logic/core/player/hands.rs b/src/logic/core/player/hands.rs index 75df0ba..9c23494 100644 --- a/src/logic/core/player/hands.rs +++ b/src/logic/core/player/hands.rs @@ -2,7 +2,7 @@ use bevy::prelude::*; use crate::{ 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( @@ -22,10 +22,15 @@ pub fn capture_hand_usage( animation_clips: Res>, game_load_state: Res, + + mut equipment_change_event_writer: EventWriter, ) { if !game_load_state.player_loaded { return; } + if player_query.iter().len() == 0 { + return; + } 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() { 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), ); } + 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 if mouse_buttons.pressed(MouseButton::Left) { if player_firing_info.full_auto_timer.finished() { diff --git a/src/scenes/scene1/spawn_points.rs b/src/scenes/scene1/spawn_points.rs index 3180a18..80d8f0d 100644 --- a/src/scenes/scene1/spawn_points.rs +++ b/src/scenes/scene1/spawn_points.rs @@ -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}; 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) }}) }); } \ No newline at end of file diff --git a/src/setup/assets.rs b/src/setup/assets.rs index ee4adce..f66399f 100644 --- a/src/setup/assets.rs +++ b/src/setup/assets.rs @@ -31,10 +31,11 @@ pub fn load_all_assets( 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 glock17_gltf_asset = GltfAsset { asset_type: GltfAssetType::Firearm(Firearm::Glock17), asset: asset_server.load(Firearm::Glock17.firearm_data().asset_path) }; commands.insert_resource(GltfAssets { assets: Vec::from([ - m4a1_gltf_asset + m4a1_gltf_asset, + glock17_gltf_asset ]) }); game_load_state.assets_loaded = true; diff --git a/src/setup/equipment.rs b/src/setup/equipment.rs index 1d15fe9..d295d8d 100644 --- a/src/setup/equipment.rs +++ b/src/setup/equipment.rs @@ -32,7 +32,7 @@ pub fn change_equipment( 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() { 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(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); firearm_transform.rotate_y(utils::rad_deg::radians_from_degrees( firearm.holdable_object_data().y_rot, )); + firearm_transform.scale = firearm_transform.scale * firearm_data.scale_factor; let scene = gltf.scenes[0].clone(); - let firearm_data: FirearmData = firearm.firearm_data(); + let firearm_entity = commands .spawn(( SceneBundle { diff --git a/src/setup/spawners/player.rs b/src/setup/spawners/player.rs index 99a2092..8369480 100644 --- a/src/setup/spawners/player.rs +++ b/src/setup/spawners/player.rs @@ -94,8 +94,8 @@ pub fn player_spawner( .insert(PlayerFiringInfo::default()) .push_children(&[camera]); + game_load_state.player_loaded = true; equipment_change_event_writer.send(EquipmentChangeEvent(player_spawn_point.what.0.equipment.clone())); commands.entity(player_spawn_point_entity).despawn(); - game_load_state.player_loaded = true; } } \ No newline at end of file