diff --git a/src/comps/core/markers/proxy/weapons/firearm.rs b/src/comps/core/markers/proxy/weapons/firearm.rs index 9f5f9f9..38664b4 100644 --- a/src/comps/core/markers/proxy/weapons/firearm.rs +++ b/src/comps/core/markers/proxy/weapons/firearm.rs @@ -38,7 +38,7 @@ pub fn insert_firearm_state_to_firearms( if let ItemState::Weapon(mut firearm_state) = item_state.clone() { commands.entity(firearm_scene_entity).remove::(); spawn_attachments_in_firearm(&mut commands, firearm_entity, &mut firearm_state, &queries.slots_query, &queries.attachments_query, &queries.scenes_query, &assets_gltf, &loaded_gltf_assets); - adjust_sight_slot_position(&mut queries.slots_query, firearm_state.optic_pos); + adjust_sight_slot_position(&mut queries.slots_query, firearm_state.get_optic_pos_opt()); commands .entity(firearm_entity) .insert(firearm_state); @@ -50,7 +50,7 @@ pub fn insert_firearm_state_to_firearms( Some(mut firearm_state) => { // Change the Slots spawn_attachments_in_firearm(&mut commands, firearm_entity, &mut firearm_state, &queries.slots_query, &queries.attachments_query, &queries.scenes_query, &assets_gltf, &loaded_gltf_assets); - adjust_sight_slot_position(&mut queries.slots_query, firearm_state.optic_pos); + adjust_sight_slot_position(&mut queries.slots_query, firearm_state.get_optic_pos_opt()); for in_player_hands_item_id in queries.in_player_hands_query.iter() { for player_inventory in queries.player_inventories_query.iter() { if let Some(current_item) = player_inventory.get_current_slot_item() { @@ -64,7 +64,8 @@ pub fn insert_firearm_state_to_firearms( None => { // Create the firearm_state let mut firearm_slots = Vec::new(); - let magazine_data = None; + let mut magazine_data = None; + let mut optic_data = None; for (slot_entity, slot, parent_entity, _) in queries.slots_query.iter() { if firearm_entity == parent_entity.get() { let mut attachment = None; @@ -72,6 +73,7 @@ pub fn insert_firearm_state_to_firearms( if slot_entity == attachment_parent.get() { attachment = Some(weapon_attachment.clone()) } + println!("A"); } firearm_slots.push(AttachmentSlot { attachment, @@ -82,11 +84,18 @@ pub fn insert_firearm_state_to_firearms( if let Some(initial_attachments) = initial_attchments_opt { for initial_attachment in initial_attachments.attachments.iter() { - firearm_slots.iter_mut().for_each(|firearm_slot| if initial_attachment.fits_into_slot(&firearm_slot.slot_type) { firearm_slot.attachment = Some(initial_attachment.clone()) }); + firearm_slots.iter_mut().for_each(|firearm_slot| if initial_attachment.fits_into_slot(&firearm_slot.slot_type) { + firearm_slot.attachment = Some(initial_attachment.clone()); + match initial_attachment { + WeaponAttachment::Magazine(magazine) => magazine_data = Some(magazine.magazine_data()), + WeaponAttachment::Optic(optic) => optic_data = Some(optic.optic_data()), + _ => {} + }; + }); } commands.entity(firearm_scene_entity).remove::(); } - let firearm_state = FirearmState::new(firearm_slots, magazine_data); + let firearm_state = FirearmState::new(firearm_slots, magazine_data, optic_data); commands .entity(firearm_entity) .insert(firearm_state.clone()); @@ -150,6 +159,11 @@ fn spawn_attachments_in_firearm( firearm_state.magazine_data = Some(magazine.magazine_data()); } } + else if let None = &firearm_state.optic_data { + if let WeaponAttachment::Optic(optic) = attachment { + firearm_state.optic_data = Some(optic.optic_data()); + } + } }} }, None => { @@ -163,12 +177,12 @@ fn spawn_attachments_in_firearm( fn adjust_sight_slot_position( slots_query: &mut Query<(Entity, &'static WeaponSlot, &'static Parent, &'static mut Transform)>, - optic_pos_opt: Option, + optic_pos_opt: Option, ) { if let Some(optic_pos) = optic_pos_opt { for (_, slot, _, mut slot_transform) in slots_query.iter_mut() { if slot == &WeaponSlot::SightSlot { - slot_transform.translation = optic_pos; + slot_transform.translation.y = optic_pos; } } } diff --git a/src/comps/core/markers/proxy/weapons/optic_sight_glass.rs b/src/comps/core/markers/proxy/weapons/optic_sight_glass.rs index a504188..dad204e 100644 --- a/src/comps/core/markers/proxy/weapons/optic_sight_glass.rs +++ b/src/comps/core/markers/proxy/weapons/optic_sight_glass.rs @@ -15,13 +15,13 @@ pub fn replace_optic_sight_material( marker_query: Query<(Entity, &OpticSightGlass), Added>, materials_query: Query<(&Parent, &Handle), With>>, mut materials: ResMut>, - debug_mat: Res ) { for (marker, optic_sight_glass) in marker_query.iter() { for (material_parent, material_handle) in materials_query.iter() { if marker == material_parent.get() { if let Some(material) = materials.get_mut(material_handle) { *material = optic_sight_glass.0.sight_glass_material(); + //TODO: Reticle } } } diff --git a/src/comps/core/weapons/attachments/optic.rs b/src/comps/core/weapons/attachments/optic.rs index 718c59b..e652c88 100644 --- a/src/comps/core/weapons/attachments/optic.rs +++ b/src/comps/core/weapons/attachments/optic.rs @@ -1,6 +1,8 @@ use bevy::{reflect::{Reflect, std_traits::ReflectDefault}, ecs::{component::Component, reflect::ReflectComponent}, math::Vec3, pbr::StandardMaterial, render::color::Color}; use bevy_inspector_egui::egui::Rgba; +use crate::comps::core::weapons::optic_data::OpticData; + use super::attachment::Attachment; @@ -17,6 +19,15 @@ impl Optic { Optic::AimpointT1 => Vec3 { x: 0.0, y: -0.05, z: 0.0 }, } } + + pub fn default_reticle_brightness(&self) -> f32 { + 1.0 + } + + pub fn optic_data(&self) -> OpticData { + OpticData { optic_pos_y: None, reticle_brightness: self.default_reticle_brightness() } + } + pub fn sight_glass_material(&self) -> StandardMaterial { match self { Optic::AimpointT1 => { diff --git a/src/comps/core/weapons/firearm_state.rs b/src/comps/core/weapons/firearm_state.rs index 859f0d7..86115ad 100644 --- a/src/comps/core/weapons/firearm_state.rs +++ b/src/comps/core/weapons/firearm_state.rs @@ -1,17 +1,17 @@ use bevy::prelude::*; -use super::{attachment_slot::AttachmentSlot, slot::slot::WeaponSlot, magazine_data::MagazineData, attachments::{weapon_attachment::WeaponAttachment, attachment::Position}}; +use super::{attachment_slot::AttachmentSlot, slot::slot::WeaponSlot, magazine_data::MagazineData, attachments::{weapon_attachment::WeaponAttachment, attachment::Position}, optic_data::OpticData}; #[derive(Component, Reflect, PartialEq, Debug, Clone)] pub struct FirearmState { pub attachment_slots: Vec, pub magazine_data: Option, - pub optic_pos: Option, + pub optic_data: Option, } impl FirearmState { - pub fn new(attachment_slots: Vec, magazine_data: Option) -> Self { - Self { attachment_slots, magazine_data: None, optic_pos: None } + pub fn new(attachment_slots: Vec, magazine_data: Option, optic_data: Option) -> Self { + Self { attachment_slots, magazine_data, optic_data} } /// Call this function when a user requests to switch an attachment in a specific slot. @@ -48,4 +48,11 @@ impl FirearmState { } Vec3::ZERO } + + pub fn get_optic_pos_opt(&self) -> Option { + match self.optic_data { + Some(ref optic_data) => optic_data.optic_pos_y, + None => None, + } + } } \ No newline at end of file diff --git a/src/comps/core/weapons/mod.rs b/src/comps/core/weapons/mod.rs index dc00eef..c8e76f3 100644 --- a/src/comps/core/weapons/mod.rs +++ b/src/comps/core/weapons/mod.rs @@ -7,4 +7,5 @@ pub mod magazine_data; pub mod slot; pub mod parts; pub mod firearm_state; -pub mod attachment_slot; \ No newline at end of file +pub mod attachment_slot; +pub mod optic_data; \ No newline at end of file diff --git a/src/comps/core/weapons/optic_data.rs b/src/comps/core/weapons/optic_data.rs new file mode 100644 index 0000000..e908d78 --- /dev/null +++ b/src/comps/core/weapons/optic_data.rs @@ -0,0 +1,21 @@ +use bevy::prelude::*; + +#[derive(PartialEq, Clone, Debug, Reflect)] +pub struct OpticData { + /// The y position of the optic on the mounted gun + /// A value of None means use the y_pos that the gun came with. + /// In other words, the user hasn't changed the position of the optic. + pub optic_pos_y: Option, + /// Adjustable setting for each reticle in every optic. + /// 1.0 == baseline + pub reticle_brightness: f32, +} + +impl Default for OpticData { + fn default() -> Self { + Self { + optic_pos_y: None, + reticle_brightness: 1.0, + } + } +} \ No newline at end of file diff --git a/src/logic/core/guns/inspect.rs b/src/logic/core/guns/inspect.rs index c4ee6a1..d97bcbe 100644 --- a/src/logic/core/guns/inspect.rs +++ b/src/logic/core/guns/inspect.rs @@ -83,7 +83,7 @@ pub fn inspect_firearm( } firearm_state.switch_attachment_by_pos(slot, None); if slot == &WeaponSlot::SightSlot { - firearm_state.optic_pos = None; + firearm_state.optic_data = None; } } } @@ -111,7 +111,9 @@ pub fn inspect_firearm( if !find_child_in_parent_children(&mut commands, in_player_hands_entity, firearm_entity, &queries.children) { continue; } - firearm_state.optic_pos = Some(slot_transform.translation); + if let Some(optic_data) = firearm_state.optic_data.as_mut() { + optic_data.optic_pos_y = Some(slot_transform.translation.y); + } } }, };