Sight bug fixes and firearm state persistence

This commit is contained in:
Franklin 2023-11-29 08:49:54 -04:00
parent 837ea1ab40
commit afe34105e4
7 changed files with 71 additions and 15 deletions

View File

@ -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::<ItemState>();
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::<InitialAttachments>();
}
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<Vec3>,
optic_pos_opt: Option<f32>,
) {
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;
}
}
}

View File

@ -15,13 +15,13 @@ pub fn replace_optic_sight_material(
marker_query: Query<(Entity, &OpticSightGlass), Added<OpticSightGlass>>,
materials_query: Query<(&Parent, &Handle<StandardMaterial>), With<Handle<Mesh>>>,
mut materials: ResMut<Assets<StandardMaterial>>,
debug_mat: Res<DebugMaterialResource>
) {
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
}
}
}

View File

@ -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 => {

View File

@ -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<AttachmentSlot>,
pub magazine_data: Option<MagazineData>,
pub optic_pos: Option<Vec3>,
pub optic_data: Option<OpticData>,
}
impl FirearmState {
pub fn new(attachment_slots: Vec<AttachmentSlot>, magazine_data: Option<MagazineData>) -> Self {
Self { attachment_slots, magazine_data: None, optic_pos: None }
pub fn new(attachment_slots: Vec<AttachmentSlot>, magazine_data: Option<MagazineData>, optic_data: Option<OpticData>) -> 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<f32> {
match self.optic_data {
Some(ref optic_data) => optic_data.optic_pos_y,
None => None,
}
}
}

View File

@ -8,3 +8,4 @@ pub mod slot;
pub mod parts;
pub mod firearm_state;
pub mod attachment_slot;
pub mod optic_data;

View File

@ -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<f32>,
/// 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,
}
}
}

View File

@ -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);
}
}
},
};