From c1cd45192df45631c5d0d01d9e7378fff56e354d Mon Sep 17 00:00:00 2001 From: Franklin Date: Sat, 25 Nov 2023 14:49:57 -0400 Subject: [PATCH] All optics implementing a fn/trait that gives a specific gun offset to use the optic correctly --- Design.md | 6 ++--- src/comps/core/weapons/attachments/optic.rs | 13 +++++++++-- src/comps/core/weapons/firearm_state.rs | 26 +++++++++------------ src/logic/core/player/hands.rs | 9 +++---- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/Design.md b/Design.md index 2c29815..d5a87e7 100644 --- a/Design.md +++ b/Design.md @@ -34,9 +34,9 @@ Multiplayer - [x] EventCollision system that takes into account more than one type of collider. - [x] Auto Low ready when gun collider hits object OR when player starts sprinting - [ ] (Not a priority) Collision with wall and attempting to Aim can look very twitchy, maybe add a cooldown of 0.1-0.4s on each change from low ready to high ready -- [ ] Optics - - [ ] All optics implementing a fn/trait that gives a specific gun offset to use the optic correctly - - [ ] Find some way to implement a shader for the optics +- [x] Optics + - [x] All optics implementing a fn/trait that gives a specific gun offset to use the optic correctly + - [ ] TODO: Find some way to implement a shader for the optics - [ ] Bobbing - [ ] Gun Bob on run - [ ] Gun Bob on walk diff --git a/src/comps/core/weapons/attachments/optic.rs b/src/comps/core/weapons/attachments/optic.rs index 8e0d4f6..c9f2bf5 100644 --- a/src/comps/core/weapons/attachments/optic.rs +++ b/src/comps/core/weapons/attachments/optic.rs @@ -1,4 +1,4 @@ -use bevy::{reflect::{Reflect, std_traits::ReflectDefault}, ecs::{component::Component, reflect::ReflectComponent}}; +use bevy::{reflect::{Reflect, std_traits::ReflectDefault}, ecs::{component::Component, reflect::ReflectComponent}, math::Vec3}; use super::attachment::Attachment; @@ -10,6 +10,14 @@ pub enum Optic { AimpointT1, } +impl Optic { + pub fn aimed_in_gun_pos_offset(&self) -> Vec3 { + match self { + Optic::AimpointT1 => Vec3 { x: 0.0, y: -0.05, z: 0.0 }, + } + } +} + impl Attachment for Optic { fn current_attachment_index(&self) -> u32 { match self { @@ -20,4 +28,5 @@ impl Attachment for Optic { fn all_attachments() -> Vec where Self: Sized { Vec::from([Self::AimpointT1]) } -} \ No newline at end of file +} + diff --git a/src/comps/core/weapons/firearm_state.rs b/src/comps/core/weapons/firearm_state.rs index 24d3f6d..859f0d7 100644 --- a/src/comps/core/weapons/firearm_state.rs +++ b/src/comps/core/weapons/firearm_state.rs @@ -37,19 +37,15 @@ impl FirearmState { } } } -} -/* -Some(add) => { - let add_by = if add { 1 } else { -1 }; - match attachment_slot.attachment { - Some(attachment) => , - None => { // This means there was no attachment there. pass -1 to the from_index() fn. It will return the last one - - }, - } - }, - None => { - attachment_slot.attachment = None; - }, -*/ \ No newline at end of file + pub fn get_optic_offset(&self) -> Vec3 { + for attachment_slot in self.attachment_slots.iter() { + if attachment_slot.slot_type == WeaponSlot::SightSlot { + if let Some(WeaponAttachment::Optic(optic)) = &attachment_slot.attachment { + return optic.aimed_in_gun_pos_offset() + } + } + } + Vec3::ZERO + } +} \ No newline at end of file diff --git a/src/logic/core/player/hands.rs b/src/logic/core/player/hands.rs index 3ad69c6..7a7f46a 100644 --- a/src/logic/core/player/hands.rs +++ b/src/logic/core/player/hands.rs @@ -196,8 +196,9 @@ pub fn capture_hand_usage( firearm_data.rebound_time_seconds } }; - if resources.mouse_buttons.pressed(MouseButton::Right) - && !resources.game_ui_state.any_window() && !player_linear_xz_state.is_sprinting() && !player_firing_info.gun_colliding_with_object + if resources.mouse_buttons.pressed(MouseButton::Right) // Aiming in (RClick) + && !resources.game_ui_state.any_window() && !player_linear_xz_state.is_sprinting() + && !player_firing_info.gun_colliding_with_object { player_firing_info.gun_ready_pose = GunReadyPose::HighReady; let rotation_lerp_quat = in_hand_transform.rotation.lerp( @@ -207,7 +208,7 @@ pub fn capture_hand_usage( .clamp(0.0, 1.0), ); let position_lerp_vec3 = in_hand_transform.translation.lerp( - firearm_data.final_aimed_position + resources.player_settings.pos_aimed_offset, + firearm_data.final_aimed_position + resources.player_settings.pos_aimed_offset + firearm_state.get_optic_offset(), (resources.time.delta_seconds() / lerp_time) .clamp(0.0, 1.0), @@ -230,7 +231,7 @@ pub fn capture_hand_usage( } // SHOOTING & RECOIL - if resources.mouse_buttons.pressed(MouseButton::Left) + if resources.mouse_buttons.pressed(MouseButton::Left) // Shooting (LClick) && !resources.game_ui_state.any_window() && player_firing_info.gun_ready_pose == GunReadyPose::HighReady { if let Some(magazine_data) = &mut firearm_state.magazine_data {