Fixed terrible bug respawning all attachments on every shot of the gun

This commit is contained in:
Franklin 2023-11-29 07:14:11 -04:00
parent dde59da879
commit 4337b9af15
7 changed files with 66 additions and 11 deletions

View File

@ -38,8 +38,11 @@ Multiplayer
- [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 (AFTER IMPLEMENTING NEW BULLET SYSTEM)
- [ ] Optic glass material
- [ ] Reticle render
- [ ] Parallax effect on reticle
- [ ] Weapon Clipping
- [ ] Make the player's collider bigger towards the front
- [x] Make the player's collider bigger towards the front
- [ ] Make the weapon's collider cover the firing point
- [ ] If possible, create a new gun pose, like pushed back
- [ ] Bobbing
@ -53,6 +56,7 @@ Multiplayer
- [x] Gun dropping does not apply impulse for some reason...
- [x] Gun colliding with ground and going into low ready
- [x] With introduction of floor collision groups now bullets go through floors
- [x] TODO: Huge bug all attachments get despawned and spawned when gun shoots
# Design

Binary file not shown.

View File

@ -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, 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, gun_colliders::{GunFirearmCollider, update_gun_collider}, sight_placement::{SightPlacementStart, SightPlacementEnd}}};
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}, sight_placement::{SightPlacementStart, SightPlacementEnd}, optic_sight_glass::{OpticSightGlass, replace_optic_sight_material}}};
@ -36,6 +36,8 @@ impl Plugin for ProxyComponentsPlugin {
app.register_type::<Magazine>();
app.register_type::<ForeGrip>();
app.register_type::<OpticSightGlass>();
// Parts
app.register_type::<ChargingHandle>();
app.register_type::<FireSelector>();
@ -54,10 +56,12 @@ impl Plugin for ProxyComponentsPlugin {
app.register_type::<physics::rapier::Collider>();
app.register_type::<physics::rapier::RigidBodyBlender>();
app.register_type::<physics::rapier::LinkToPlayer>();
app.add_systems(Update, (physics_replace_proxies, update_game_load_state));
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);
app.add_systems(Update, replace_optic_sight_material);
}
}

View File

@ -11,7 +11,8 @@ pub struct InsertFirearmStateIntoFirearmsParams<'w, 's> {
firearm_scene_bundle: Query<'w, 's, (Entity, Option<&'static ItemState>, Option<&'static InitialAttachments>, &'static Children), With<SceneInstance>>,
firearm_query: Query<'w, 's, (Entity, &'static Firearm, &'static Parent, Option<&'static mut FirearmState>), Or<(Without<FirearmState>, Changed<FirearmState>)>>,
slots_query: Query<'w, 's, (Entity, &'static WeaponSlot, &'static Parent, &'static mut Transform)>,
attachments_query: Query<'w, 's, (Entity, &'static WeaponAttachment, &'static Parent)>,
attachments_query: Query<'w, 's, (&'static WeaponAttachment, &'static Parent)>,
scenes_query: Query<'w, 's, (&'static Children, &'static Parent), With<Handle<Scene>>>,
//sight_placement_start_query: Query<'w, 's, (&'static Parent, &'static Transform), (With<SightPlacementStart>, Without<SightPlacementEnd>)>,
//sight_placement_end_query: Query<'w, 's, (&'static Parent, &'static Transform), (With<SightPlacementEnd>, Without<SightPlacementStart>)>,
in_player_hands_query: Query<'w, 's, &'static ItemId, With<InPlayerHands>>,
@ -36,7 +37,7 @@ pub fn insert_firearm_state_to_firearms(
#[allow(irrefutable_let_patterns)]
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, &assets_gltf, &loaded_gltf_assets);
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);
commands
.entity(firearm_entity)
@ -48,7 +49,7 @@ pub fn insert_firearm_state_to_firearms(
match firearm_state_opt {
Some(mut firearm_state) => {
// Change the Slots
spawn_attachments_in_firearm(&mut commands, firearm_entity, &mut firearm_state, &queries.slots_query, &assets_gltf, &loaded_gltf_assets);
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);
for in_player_hands_item_id in queries.in_player_hands_query.iter() {
for player_inventory in queries.player_inventories_query.iter() {
@ -67,7 +68,7 @@ pub fn insert_firearm_state_to_firearms(
for (slot_entity, slot, parent_entity, _) in queries.slots_query.iter() {
if firearm_entity == parent_entity.get() {
let mut attachment = None;
for (_, weapon_attachment, attachment_parent) in queries.attachments_query.iter() {
for (weapon_attachment, attachment_parent) in queries.attachments_query.iter() {
if slot_entity == attachment_parent.get() {
attachment = Some(weapon_attachment.clone())
}
@ -102,6 +103,8 @@ fn spawn_attachments_in_firearm(
firearm_entity: Entity,
firearm_state: &mut FirearmState,
slots_query: &Query<(Entity, &WeaponSlot, &Parent, &mut Transform)>,
attachments_query: &Query<(&'static WeaponAttachment, &'static Parent)>,
scenes_query: &Query<(&'static Children, &'static Parent), With<Handle<Scene>>>,
assets_gltf: &GltfAssets,
loaded_gltf_assets: &Assets<Gltf>,
) {
@ -111,9 +114,24 @@ fn spawn_attachments_in_firearm(
}
for attachment_slot in firearm_state.attachment_slots.iter() {
if discriminant(&attachment_slot.slot_type) == discriminant(weapon_slot) {
commands.entity(slot_entity).despawn_descendants();
match &attachment_slot.attachment {
Some(attachment) =>
{
// Check for existing attachment in slot, if it matches then don't despawn it.
let mut skip = false;
for (scene_children, scene_parent) in scenes_query.iter() {
for (weapon_attachment, attachment_parent) in attachments_query.iter() {
if scene_parent.get() == slot_entity {
for scene_child in scene_children {
if scene_child == &attachment_parent.get() && weapon_attachment == attachment{
skip = true;
}
}
}
}
}
if skip { continue; }
commands.entity(slot_entity).despawn_descendants();
if let Some(asset_handle) = assets_gltf.assets.iter().find(|asset| {
asset.asset_type == GltfAssetType::Attachment(attachment.clone())
}) {
@ -132,8 +150,11 @@ fn spawn_attachments_in_firearm(
firearm_state.magazine_data = Some(magazine.magazine_data());
}
}
}},
None => {},
}}
},
None => {
commands.entity(slot_entity).despawn_descendants();
},
};
}
}

View File

@ -1,4 +1,5 @@
pub mod firearm;
pub mod initial_attachments;
pub mod gun_colliders;
pub mod sight_placement;
pub mod sight_placement;
pub mod optic_sight_glass;

View File

@ -0,0 +1,23 @@
use bevy::prelude::*;
#[derive(Default, Component, Reflect, Debug)]
#[reflect(Component)]
pub struct OpticSightGlass;
pub fn replace_optic_sight_material(
//mut commands: Commands,
marker_query: Query<Entity, Added<OpticSightGlass>>,
materials_query: Query<(&Parent, &Handle<StandardMaterial>), With<Handle<Mesh>>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
for marker in marker_query.iter() {
println!("One marker once");
for (material_parent, material_handle) in materials_query.iter() {
if marker == material_parent.get() {
if let Some(mut material) = materials.get_mut(material_handle) {
println!("Changed material");
}
}
}
}
}

View File

@ -28,7 +28,9 @@ fn main() {
fn setup_plugins(application: &mut App) {
application
.add_plugins(DefaultPlugins.set(AssetPlugin::default()))
.add_plugins(DefaultPlugins.set(AssetPlugin {
..Default::default()
}))
//.add_plugins(DefaultInspectorConfigPlugin)
.add_plugins(RapierPhysicsPlugin::<NoUserData>::default())
//.add_plugins(RapierDebugRenderPlugin::default())