From 4337b9af15b5108b37a1e89dbd0cee5be0e715df Mon Sep 17 00:00:00 2001 From: Franklin Date: Wed, 29 Nov 2023 07:14:11 -0400 Subject: [PATCH] Fixed terrible bug respawning all attachments on every shot of the gun --- Design.md | 6 ++- assets/attachments/aimpoint_t1.glb | Bin 1482124 -> 1489884 bytes src/comps/core/markers/proxy/plugin.rs | 6 ++- .../core/markers/proxy/weapons/firearm.rs | 35 ++++++++++++++---- src/comps/core/markers/proxy/weapons/mod.rs | 3 +- .../proxy/weapons/optic_sight_glass.rs | 23 ++++++++++++ src/main.rs | 4 +- 7 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 src/comps/core/markers/proxy/weapons/optic_sight_glass.rs diff --git a/Design.md b/Design.md index b48da22..28cd7ae 100644 --- a/Design.md +++ b/Design.md @@ -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 diff --git a/assets/attachments/aimpoint_t1.glb b/assets/attachments/aimpoint_t1.glb index 404dbf9af35f1171fb4632d83d6bcb643c159e32..4e3173e9179335546bb7d94beefa6b2e0f495c81 100644 GIT binary patch delta 7721 zcmai0X^>o175-)q0g{kqKp_wslo&c=_w+tw`b`obKvqK}EE!;ubTUIG3o`>GCX?66 zump?&?XaoH76C;h5fy=Cnv!7oL#wcg!YaioEUL7$T%h=&Q2y{c=f3ym_LA;*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>SUL_eh_TJbCAh@Jg*caO7U`#wT&Q1C(ub99REah!#Zq;S zi5oox|C%E2SIk;ny+rL#tHq)IGyDQ4IG(`kcmv~T;@HIcIgSRjvbkqqI@>uopY3AI zV~Zfkwg8LRuEGkoHMo(j5B+T0Fv9jV{;132UL0im79L~!9*(g65J%a5PWs~{7v}So zGpTGVl&4=A^6L9oSly1Q!*qo%QqYJlA5r@}wH(Cpv9Y%M#@ZU#8`+!Ko7r2~TiIo8 p68mZFli5#aKZE^D_9^UB*{89e#Xg;V2K!9*S+9<@wV(FL{{e-GjjaFx delta 291 zcmWm2y-LGS90l-v-F)0?(%P5`4vm3;REOJ`#v-E7K~SMMxY(LasbW#E2#REKaH-QD z3J$(O7Z)q|2o4TeeGlp8>F_&;bAJCi!>^9r>mTnak|Yh^Jn6!Cw=1$E(kxvMu5ZrH z2f(); app.register_type::(); + app.register_type::(); + // Parts app.register_type::(); app.register_type::(); @@ -54,10 +56,12 @@ impl Plugin for ProxyComponentsPlugin { app.register_type::(); app.register_type::(); app.register_type::(); + 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); } } \ 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 a55a478..9f5f9f9 100644 --- a/src/comps/core/markers/proxy/weapons/firearm.rs +++ b/src/comps/core/markers/proxy/weapons/firearm.rs @@ -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>, firearm_query: Query<'w, 's, (Entity, &'static Firearm, &'static Parent, Option<&'static mut FirearmState>), Or<(Without, Changed)>>, 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>>, //sight_placement_start_query: Query<'w, 's, (&'static Parent, &'static Transform), (With, Without)>, //sight_placement_end_query: Query<'w, 's, (&'static Parent, &'static Transform), (With, Without)>, in_player_hands_query: Query<'w, 's, &'static ItemId, With>, @@ -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::(); - 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>>, assets_gltf: &GltfAssets, loaded_gltf_assets: &Assets, ) { @@ -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(); + }, }; } } diff --git a/src/comps/core/markers/proxy/weapons/mod.rs b/src/comps/core/markers/proxy/weapons/mod.rs index 78da5ef..696b9ad 100644 --- a/src/comps/core/markers/proxy/weapons/mod.rs +++ b/src/comps/core/markers/proxy/weapons/mod.rs @@ -1,4 +1,5 @@ pub mod firearm; pub mod initial_attachments; pub mod gun_colliders; -pub mod sight_placement; \ No newline at end of file +pub mod sight_placement; +pub mod optic_sight_glass; \ No newline at end of file diff --git a/src/comps/core/markers/proxy/weapons/optic_sight_glass.rs b/src/comps/core/markers/proxy/weapons/optic_sight_glass.rs new file mode 100644 index 0000000..f942f49 --- /dev/null +++ b/src/comps/core/markers/proxy/weapons/optic_sight_glass.rs @@ -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>, + materials_query: Query<(&Parent, &Handle), With>>, + mut materials: ResMut>, +) { + 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"); + } + } + } + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1b5dc05..e945ae1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::::default()) //.add_plugins(RapierDebugRenderPlugin::default())