From 4ce5aa421969c4125c1f55afdb8eb841bd8a21e6 Mon Sep 17 00:00:00 2001 From: Franklin Date: Wed, 22 Nov 2023 17:56:41 -0400 Subject: [PATCH] Weapon system close to reality. All attachments except optics are good. Weapon collider now becomes a sensor when equipped and a non-sensor when dropped --- assets/weapons/ak105_rifle.glb | Bin 8226716 -> 8228460 bytes src/comps/core/inventory/player_inventory.rs | 3 +- src/comps/core/items/guns/ak105.rs | 8 +--- src/comps/core/markers/proxy/plugin.rs | 4 +- .../core/markers/proxy/weapons/firearm.rs | 1 - .../markers/proxy/weapons/gun_colliders.rs | 37 ++++++++++++++++++ src/comps/core/markers/proxy/weapons/mod.rs | 3 +- .../core/weapons/attachments/attachment.rs | 9 +++++ src/comps/core/weapons/attachments/mod.rs | 4 +- .../core/weapons/attachments/silencer.rs | 9 +++++ src/logic/core/guns/despawn_shots.rs | 13 ++++-- src/logic/core/player/camera_switching.rs | 7 +++- src/logic/core/player/hands.rs | 4 +- src/main.rs | 2 +- src/setup/equipment.rs | 5 ++- 15 files changed, 87 insertions(+), 22 deletions(-) create mode 100644 src/comps/core/markers/proxy/weapons/gun_colliders.rs create mode 100644 src/comps/core/weapons/attachments/attachment.rs create mode 100644 src/comps/core/weapons/attachments/silencer.rs diff --git a/assets/weapons/ak105_rifle.glb b/assets/weapons/ak105_rifle.glb index 2605cd1811e49949beaa14e608e8ca58abe5d315..bae8dfece685d84ff63691391b63173417a77a8f 100644 GIT binary patch delta 2440 zcmajfTW}Lq7zgm(+}c7Dk|vv^ZQ5)$i9oZ|-A(pZ1qwwJq*S@o3bs&az-XFkTU5|m zFF0Pt@xu3z7j={o#~JTa9eHq|jy^F1;-ii-`k;(6&N#lngEQ*?Y|@#ch-rU&&Uen) z?|gf<%_}cna{v0uC3oOpdB;W<$8qHsX1KAH9|eYZzA$%?|CzV@Cd%c~SaDM7k@|~M zcT2J~Seh#BE0s&rhcR|bX>6=m!RXD0DjQ3a#nH*}`W+;D<1POhouqS(){rbs?H?@{ zrJ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IBg90jOkcR^3V1Nl0*w6zvz>TmP*1%fmg>|qV`d|ax1RLRIxCJ)B zX1Eo$KtF7S0k{orhi$MOcEC;;ggame?u1>i8}5Q(7=b;o7e-+p?1wQZ!T~r4cS8y8 pfqS70<4}PKxDW1!Ntl9ZI0O&CgYXa>h9mGWJo4$~?97SLzX8ny@Vfv2 delta 1426 zcmXxhOH5o<7zc3fy)(n3pjx#v6IIrrB3 zuC%_oE6EGzjvp3VT3XgG)>>|4ugE6_!Ps07b_6UsJ2y`|ZVG~mA=d^Dx_sDmeIV#q zX+0$vxY0T;n45iV zjE-Oi8-WCl1V6{`0YBFK`!F4n@k7ibRIwe4V=9!w&6pptNC!R+^`H<;;(F*a++o}a zyX#+~%x`wWX)J{j$VYl`Eu4fJbz(8{30flxt9CF?iCVLy8g*ki>ftr^3V#{R0@3>M z9r!Qi!oSf=g303tSl-KL*p3a0y2apY^f408;a$KBZVT~PkBTR&m{ZteX3iuZA@lLaHSQ8RyI{k79to- z2PDfhkjh2zRodrN%Qjc$lYRBfq1t7&QR(lrBB~bVk|hjge2%hdnpn&Cp_U0qs%AAd z)G~gjX{wca#y)x!Ni&+`qdh)n)z-MOmM>wov0^|=#&C^eRc{_Yl=V4`3SY6_`cRe& zhTbT=ob@}bYPH$cw^>EhG_2I4f|8V?G-arVvXrAd6{wf`s7UpG`)Pm_tJedOd~W(hvP7gBlIXu(lL6Bj?)xP z(+N6BkJBl7f=<&6&C(f~qj`Fg&eBu#G(AHL^ejC`&(jNZj$Wjf=w(`@SLjuGjb5ko Wbb*%W4SJK_qPOXtt6Qbo#{B;P1nbxU diff --git a/src/comps/core/inventory/player_inventory.rs b/src/comps/core/inventory/player_inventory.rs index 5cf2cb0..b3237ed 100644 --- a/src/comps/core/inventory/player_inventory.rs +++ b/src/comps/core/inventory/player_inventory.rs @@ -92,8 +92,7 @@ pub fn drop_slot_in_game_world( ) { let drop_position = Transform::from_translation( player_transform.translation - + player_transform.up() * 3.0 - + player_transform.forward() * 2.0, + + player_transform.up() * 5.0 ); let drop_impulse = player_transform.translation + player_transform.up() * 100.0 diff --git a/src/comps/core/items/guns/ak105.rs b/src/comps/core/items/guns/ak105.rs index afebe0a..416fcb7 100644 --- a/src/comps/core/items/guns/ak105.rs +++ b/src/comps/core/items/guns/ak105.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use bevy::{prelude::*, gltf::Gltf}; use bevy_inspector_egui::egui::mutex::Mutex; -use bevy_rapier3d::{dynamics::{RigidBody, GravityScale, ExternalImpulse}, geometry::{ColliderMassProperties, Collider}}; +use bevy_rapier3d::{dynamics::{RigidBody, GravityScale, ExternalImpulse}, geometry::ColliderMassProperties}; use crate::{comps::core::{ grid::UGrid, @@ -105,7 +105,6 @@ impl Item for Ak105GunItem { if let Some(initial_attachments) = &self.initial_attachments { commands.entity(firearm_asset_entity).insert(initial_attachments.clone()); } - let firearm_size = firearm.get_size(); commands .spawn(( firearm.holdable_object_data(), @@ -121,11 +120,6 @@ impl Item for Ak105GunItem { RigidBody::Dynamic, ColliderMassProperties::Mass(5.0), GravityScale(4.0), - Collider::cuboid( - firearm_size.x, - firearm_size.y, - firearm_size.z, - ), ExternalImpulse { impulse: with_impulse, ..Default::default() diff --git a/src/comps/core/markers/proxy/plugin.rs b/src/comps/core/markers/proxy/plugin.rs index 69ada81..3e68354 100644 --- a/src/comps/core/markers/proxy/plugin.rs +++ b/src/comps/core/markers/proxy/plugin.rs @@ -2,7 +2,7 @@ use bevy::app::{Plugin, Update}; use crate::{setup::load_state::update_game_load_state, comps::core::weapons::{firearm::Firearm, attachments::{weapon_attachment::WeaponAttachment, optic::Optic, stock::Stock, compensator::Compensator, magazine::Magazine, foregrip::ForeGrip}, parts::{charging_handle::ChargingHandle, fire_selector::FireSelector, firing_point::FiringPoint, trigger::Trigger}, slot::{compensator_slot::CompensatorSlot, fore_grip_slot::ForeGripSlot, magazine_slot::MagazineSlot, sight_placement_start_slot::SightPlacementStartSlot, sight_placement_end_slot::SightPlacementEndSlot, stock_slot::StockSlot, utility_slot::UtilitySlot, slot::WeaponSlot}}}; -use super::{character::{player_hitbox::PlayerHitBox, player_character::PlayerCharacter, player_eye::{PlayerEye, insert_components_into_spawned_player}, in_player_hands_parent::{InPlayerHandsParent, insert_components_into_player_hand}, third_person_camera::ThirdPersonCameraProxy}, physics::{rapier::{AutoAABBCollider, physics_replace_proxies}, self}, weapons::firearm::insert_firearm_state_to_firearms}; +use super::{character::{player_hitbox::PlayerHitBox, player_character::PlayerCharacter, player_eye::{PlayerEye, insert_components_into_spawned_player}, in_player_hands_parent::{InPlayerHandsParent, insert_components_into_player_hand}, third_person_camera::ThirdPersonCameraProxy}, physics::{rapier::{AutoAABBCollider, physics_replace_proxies}, self}, weapons::{firearm::insert_firearm_state_to_firearms, gun_colliders::{GunFirearmCollider, update_gun_collider}}}; @@ -22,6 +22,7 @@ impl Plugin for ProxyComponentsPlugin { // Firearms app.register_type::(); + app.register_type::(); // Attachments app.register_type::(); @@ -56,5 +57,6 @@ impl Plugin for ProxyComponentsPlugin { app.add_systems(Update, insert_components_into_spawned_player); app.add_systems(Update, insert_components_into_player_hand); app.add_systems(Update, insert_firearm_state_to_firearms); + app.add_systems(Update, update_gun_collider); } } \ No newline at end of file diff --git a/src/comps/core/markers/proxy/weapons/firearm.rs b/src/comps/core/markers/proxy/weapons/firearm.rs index e548a2a..6acada6 100644 --- a/src/comps/core/markers/proxy/weapons/firearm.rs +++ b/src/comps/core/markers/proxy/weapons/firearm.rs @@ -12,7 +12,6 @@ pub struct InsertFirearmStateIntoFirearmsParams<'w, 's> { attachments_query: Query<'w, 's, (Entity, &'static WeaponAttachment, &'static Parent)>, in_player_hands_query: Query<'w, 's, &'static ItemId, With>, player_inventories_query: Query<'w, 's, &'static PlayerInventory>, - } /// This query inserts FirearmState into firearms, updates firearm assets that already have a FirearmState, diff --git a/src/comps/core/markers/proxy/weapons/gun_colliders.rs b/src/comps/core/markers/proxy/weapons/gun_colliders.rs new file mode 100644 index 0000000..4d281ca --- /dev/null +++ b/src/comps/core/markers/proxy/weapons/gun_colliders.rs @@ -0,0 +1,37 @@ +use bevy::prelude::*; +use bevy_rapier3d::geometry::Sensor; + +use crate::{comps::core::markers::holdable::InPlayerHands, utils::hierarchy::find_child_in_parent_children}; + + +/// This marker is to find the gun's collider that should be a sensor when equipped and a non-sensor when on the ground +#[derive(Component, Clone, Reflect, Default, Debug)] +#[reflect(Component)] +pub struct GunFirearmCollider; + + +/*for (gun_firearm_collider_entity, _) in gun_firearm_collider_query.iter() { + println!("a"); + if find_child_in_parent_children(commands, firearm_asset_entity, gun_firearm_collider_entity, &children) { + println!("Sensor"); + commands.entity(gun_firearm_collider_entity).insert(Sensor); + } + } */ +pub fn update_gun_collider( + mut commands: Commands, + gun_firearm_collider_query: Query>, + in_player_hands_query: Query>, + children: Query<&Children>, +) { + for gun_firearm_collider_entity in gun_firearm_collider_query.iter() { + let mut found = false; + for in_player_hands_entity in in_player_hands_query.iter() { + if find_child_in_parent_children(&mut commands, in_player_hands_entity, gun_firearm_collider_entity, &children) { + commands.entity(gun_firearm_collider_entity).insert(Sensor); + found = true; + } + } + if found { continue; } + commands.entity(gun_firearm_collider_entity).remove::(); + } +} \ No newline at end of file diff --git a/src/comps/core/markers/proxy/weapons/mod.rs b/src/comps/core/markers/proxy/weapons/mod.rs index 77a0ef8..0658ba3 100644 --- a/src/comps/core/markers/proxy/weapons/mod.rs +++ b/src/comps/core/markers/proxy/weapons/mod.rs @@ -1,2 +1,3 @@ pub mod firearm; -pub mod initial_attachments; \ No newline at end of file +pub mod initial_attachments; +pub mod gun_colliders; \ No newline at end of file diff --git a/src/comps/core/weapons/attachments/attachment.rs b/src/comps/core/weapons/attachments/attachment.rs new file mode 100644 index 0000000..1094c61 --- /dev/null +++ b/src/comps/core/weapons/attachments/attachment.rs @@ -0,0 +1,9 @@ + + +/// Trait that will contain all the SHARED properties in between all attachments +/// - Recoil reduction Vert and Horizontal +/// - Aim Speed +/// - +pub trait Attachment { + +} \ No newline at end of file diff --git a/src/comps/core/weapons/attachments/mod.rs b/src/comps/core/weapons/attachments/mod.rs index bb061dc..411b921 100644 --- a/src/comps/core/weapons/attachments/mod.rs +++ b/src/comps/core/weapons/attachments/mod.rs @@ -3,4 +3,6 @@ pub mod optic; pub mod magazine; pub mod compensator; pub mod stock; -pub mod foregrip; \ No newline at end of file +pub mod foregrip; +pub mod silencer; +pub mod attachment; \ No newline at end of file diff --git a/src/comps/core/weapons/attachments/silencer.rs b/src/comps/core/weapons/attachments/silencer.rs new file mode 100644 index 0000000..6a46a4a --- /dev/null +++ b/src/comps/core/weapons/attachments/silencer.rs @@ -0,0 +1,9 @@ +use bevy::{reflect::{Reflect, std_traits::ReflectDefault}, ecs::{component::Component, reflect::ReflectComponent}}; + + +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Reflect, Component, Debug, Default)] +#[reflect(Component, Default)] +pub enum Silencer { + #[default] + FirstSilencer +} \ No newline at end of file diff --git a/src/logic/core/guns/despawn_shots.rs b/src/logic/core/guns/despawn_shots.rs index 1d82b04..f4b5a19 100644 --- a/src/logic/core/guns/despawn_shots.rs +++ b/src/logic/core/guns/despawn_shots.rs @@ -1,6 +1,6 @@ use crate::comps::core::markers::{bullet::BulletMarker, muzzle_flash::MuzzleFlashMarker}; use bevy::prelude::*; -use bevy_rapier3d::prelude::*; +use bevy_rapier3d::{prelude::*, rapier::geometry::CollisionEventFlags}; pub fn despawn_muzzle_flashes( mut commands: Commands, @@ -20,6 +20,7 @@ pub fn despawn_stray_bullets( mut meshes: ResMut>, mut materials: ResMut>, mut query: Query<(&mut BulletMarker, Entity, &Transform)>, + mut collisions: EventReader, //res: Res, time: Res