From c8da57eaa77610cac446e7cb1923853d82525d95 Mon Sep 17 00:00:00 2001 From: core Date: Thu, 10 Jul 2025 11:53:27 -0400 Subject: [PATCH] feat: snapping only to valid snaps --- crates/unified/src/client/parts.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/crates/unified/src/client/parts.rs b/crates/unified/src/client/parts.rs index b20006682a1618a6387f301bfb08250c8b6cc30b..669be3136e50954233b96ca80041eb019d58f271 100644 --- a/crates/unified/src/client/parts.rs +++ b/crates/unified/src/client/parts.rs @@ -4,7 +4,7 @@ use crate::ecs::{CursorWorldCoordinates, DragRequestEvent, Part}; use bevy::prelude::*; use bevy_rapier2d::dynamics::MassProperties; use bevy_rapier2d::prelude::AdditionalMassProperties; -use crate::attachment::{JointOf, PartInShip, SnapOf, SnapOfJoint}; +use crate::attachment::{JointOf, PartInShip, Peer, SnapOf, SnapOfJoint}; use crate::client::colors::GREEN; use crate::client::key_input::AttachmentDebugRes; @@ -114,9 +114,11 @@ fn update_drag_ghosts( mut ghost: Single<&mut Transform, (With, Without, Without, Without)>, cursor: Res, snaps: Query<(&Transform, &SnapOfJoint, &SnapOf)>, - joints: Query<(&Transform, &JointOf)>, - parts: Query<&GlobalTransform, With>, + joints: Query<(&Transform, &JointOf), Without>, + parts: Query<(&GlobalTransform, Option<&Me>, Option<&PartInShip>), With>, + me: Single>, debug: Res, + drag: Res, mut gizmos: Gizmos, ) { let Some(cursor) = cursor.0 else { return }; @@ -127,7 +129,15 @@ fn update_drag_ghosts( let mut best_target = Transform::from_xyz(cursor.x, cursor.y, 0.0); for (snap_local_transform, snap_joint, snap_part) in &snaps { - let Ok(parent_position) = parts.get(snap_part.0) else { continue; }; + if Some(snap_part.0) == drag.0 { continue; } + + // only snap to ourselves + + let Ok((parent_position, maybe_me, maybe_parent_ship)) = parts.get(snap_part.0) else { continue; }; + + let allowed = maybe_me.is_some() || maybe_parent_ship.is_some_and(|u| u.0 == *me); + if !allowed { continue } + let snap_global_translation = parent_position.transform_point(snap_local_transform.translation).xy(); let distance_to_cursor = cursor.distance(snap_global_translation); @@ -143,10 +153,9 @@ fn update_drag_ghosts( if debug.0 { gizmos.circle_2d(snap_global_translation, 3.0, GREEN); } - let Ok((offset, parent)) = joints.get(snap_joint.0) else { continue; }; - let Ok(parent_pos) = parts.get(parent.0) else { continue; }; + let Ok((offset, _)) = joints.get(snap_joint.0) else { continue; }; - let joint_target = parent_pos.transform_point(offset.translation); + let joint_target = parent_position.transform_point(offset.translation); if debug.0 { gizmos.circle_2d(joint_target.xy(), 3.0, GREEN); }