From de4eacb0ed5175cd2b0c7cd0fc118385f757dd13 Mon Sep 17 00:00:00 2001 From: ghostly_zsh Date: Mon, 17 Nov 2025 23:23:52 -0600 Subject: [PATCH] feat: rewrite detachment --- crates/unified/src/server/player.rs | 89 +++++++++++++++------------- crates/unified/src/server_plugins.rs | 6 ++ crates/unified/src/shared_plugins.rs | 7 +-- 3 files changed, 54 insertions(+), 48 deletions(-) diff --git a/crates/unified/src/server/player.rs b/crates/unified/src/server/player.rs index 8fd013b6202ffdb30eb535adb08aa1cd9c53987f..5cbc3c50324e01cfc45e4a1cb13b2e050480d0ea 100644 --- a/crates/unified/src/server/player.rs +++ b/crates/unified/src/server/player.rs @@ -23,45 +23,49 @@ pub fn player_management_plugin(app: &mut App) { } fn disconnect_part( - entity: Entity, - recursed_entity: Entity, - joints: &Joints, - q_joints: Query<&Joints>, - q_peer: Query<(Entity, &Peer, &JointOf)>, + (entity, joints): (Entity, &Joints), + q_joints: &Query<(&Joint, &JointOf, &Transform, Option<&Peer>, Entity)>, + q_only_joints: &Query<&Joints>, processed_peers: &mut Vec, mut commands: Commands, ) { - trace!(?entity, ?joints, ?processed_peers, "recursive disconnect"); - // recursive disconnect part - for joint in &**joints { - let Ok((p_e, our_peer_object, _)) = q_peer.get(*joint) else { + //trace!(?entity, ?joints, ?processed_peers, "recursive disconnect"); + // loop the joints in the entity + for joint in joints.iter() { + // now we have the entity "joint" + let Ok((_, _, _, peer, _)) = q_joints.get(joint) else { continue; }; - if processed_peers.contains(&p_e) { continue } - let other_joint = our_peer_object.peer_joint_entity_id; - - commands.entity(*joint).remove::(); - commands.entity(our_peer_object.physics_joint).despawn(); - processed_peers.push(p_e); + // don't want to double process a joint + if processed_peers.contains(&joint) { continue; } - let Ok((p_e_2, _, other_joint_of)) = q_peer.get(other_joint) else { - continue; - }; + // we have the peer that holds the physics joint + let Some(peer) = peer else { continue; }; + commands.entity(joint).remove::(); + processed_peers.push(joint); - commands.entity(other_joint).remove::(); - processed_peers.push(p_e_2); + let mut physics_joint = commands.entity(peer.physics_joint); + // the physics joint shouldnt exist anymore + physics_joint.despawn(); - let Ok(joints) = q_joints.get(other_joint_of.0) else { + // delete the other side's peer that exists for some reason + let Ok((_, other_joint_of, _, other_peer, _)) = q_joints.get(peer.peer_joint_entity_id) else { continue; }; - if other_joint != recursed_entity { - disconnect_part(other_joint, entity, joints, q_joints, q_peer, processed_peers, commands.reborrow()); + let Some(other_peer) = other_peer else { continue; }; + commands.entity(peer.peer_joint_entity_id).remove::(); + processed_peers.push(peer.peer_joint_entity_id); + let Ok(other_joints) = q_only_joints.get(other_joint_of.0) else { + continue + }; + + if other_joint_of.0 != entity { + disconnect_part((peer.peer_joint_entity_id, joints), q_joints, + q_only_joints, processed_peers, commands.reborrow()); } + } - for ppeer in processed_peers { - commands.entity(*ppeer).remove::(); - commands.entity(*ppeer).remove::(); - } + // recursive disconnect part commands.entity(entity).remove::(); } @@ -75,8 +79,9 @@ fn dragging( &mut LinearVelocity, &Joints, &mut AngularVelocity, + &Part, ), - (With, Without), + (Without), >, snaps: Query<(&SnapOf, &SnapOfJoint)>, joints: Query<(&Joint, &JointOf, &Transform, Option<&Peer>, Entity)>, @@ -154,6 +159,7 @@ fn dragging( // great, the attachment appears to be valid // let's make sure this player is allowed to drag onto this part + // getting attached to (hearty) let target_part = { let Ok(target_part) = parts.get(target_joint.1.0) else { continue; @@ -161,6 +167,7 @@ fn dragging( target_part }; + // attached (housing) let source_part = { let Ok(source_part) = parts.get(source_joint.1.0) else { continue; @@ -180,14 +187,12 @@ fn dragging( // great, we have a valid peering request let did_disconnect = q_joint.get(source_part.2).is_ok(); - let mut dc_queue = vec![]; + let mut processed = vec![source_joint.4]; disconnect_part( - source_part.2, - Entity::PLACEHOLDER, - source_part.4, - q_joints, - peer, - &mut dc_queue, + (source_part.2, source_part.4), + &joints, + &q_joints, + &mut processed, commands.reborrow(), ); @@ -239,14 +244,12 @@ fn dragging( ); warn!("dragging already attached entities may cause inconsistent behavior!!"); let source_part = parts.get(event.drag_target).unwrap(); - let mut dc_queue = vec![]; + let mut processed = vec![]; disconnect_part( - source_part.2, - Entity::PLACEHOLDER, - source_part.4, - q_joints, - peer, - &mut dc_queue, + (source_part.2, source_part.4), + &joints, + &q_joints, + &mut processed, commands.reborrow(), ); teleport_to_translation = event.drag_to; @@ -254,8 +257,10 @@ fn dragging( } let mut part = parts.get_mut(event.drag_target).unwrap(); + debug!("{:?} {:?}", part.0.translation, teleport_to_translation); part.0.translation.x = teleport_to_translation.x; part.0.translation.y = teleport_to_translation.y; + debug!("{:?} {:?}", part.0.translation, teleport_to_translation); part.0.rotation = teleport_to_rotation; // client calculates this; no reason to recalculate if let Some(new_vel) = new_linvel { *part.3 = new_vel; diff --git a/crates/unified/src/server_plugins.rs b/crates/unified/src/server_plugins.rs index 38aeb5911bcf7a702aeab42481c20631008da9d9..e9d6236cf8da53ebe4ed08d940e53bd69b89fc50 100644 --- a/crates/unified/src/server_plugins.rs +++ b/crates/unified/src/server_plugins.rs @@ -3,6 +3,7 @@ use crate::config::planet::PlanetConfigCollection; use crate::config::world::GlobalWorldConfig; use aeronet_replicon::server::AeronetRepliconServerPlugin; use aeronet_websocket::server::WebSocketServerPlugin; +use avian2d::prelude::PhysicsInterpolationPlugin; use bevy::app::{PluginGroup, PluginGroupBuilder, ScheduleRunnerPlugin, TaskPoolPlugin}; use bevy::asset::AssetPlugin; use bevy::diagnostic::FrameCountPlugin; @@ -22,6 +23,11 @@ pub struct ServerPluginGroup { impl PluginGroup for ServerPluginGroup { fn build(self) -> PluginGroupBuilder { PluginGroupBuilder::start::() + .add_group( + PhysicsPlugins::default() + .with_length_unit(100.0) + .set(PhysicsInterpolationPlugin::interpolate_all()) + ) .add(StatesPlugin) .add(TaskPoolPlugin::default()) .add(FrameCountPlugin) diff --git a/crates/unified/src/shared_plugins.rs b/crates/unified/src/shared_plugins.rs index babd468211d6dd97f4f75b788acac04efe8d1af2..da7bd15ba2cedad54809f136b37adf2f8ff414c3 100644 --- a/crates/unified/src/shared_plugins.rs +++ b/crates/unified/src/shared_plugins.rs @@ -14,11 +14,6 @@ impl PluginGroup for SharedPluginGroup { fn build(self) -> PluginGroupBuilder { PluginGroupBuilder::start::() //.add(RapierPhysicsPlugin::::pixels_per_meter(100.0)) - .add_group( - PhysicsPlugins::default() - .with_length_unit(100.0) - .set(PhysicsInterpolationPlugin::interpolate_all()) - ) .add(physics_setup_plugin) .add(register_everything) .add(register_physics_components_for_replication) @@ -69,4 +64,4 @@ fn setup_physics( let mut params = ctx.integration_parameters; params.num_internal_stabilization_iterations = 16; } -*/ \ No newline at end of file +*/