~starkingdoms/starkingdoms

c8da57eaa77610cac446e7cb1923853d82525d95 — core 5 months ago f75c2e1
feat: snapping only to valid snaps
1 files changed, 16 insertions(+), 7 deletions(-)

M crates/unified/src/client/parts.rs
M crates/unified/src/client/parts.rs => crates/unified/src/client/parts.rs +16 -7
@@ 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<DragGhost>, Without<SnapOf>, Without<JointOf>, Without<Part>)>,
    cursor: Res<CursorWorldCoordinates>,
    snaps: Query<(&Transform, &SnapOfJoint, &SnapOf)>,
    joints: Query<(&Transform, &JointOf)>,
    parts: Query<&GlobalTransform, With<Part>>,
    joints: Query<(&Transform, &JointOf), Without<Peer>>,
    parts: Query<(&GlobalTransform, Option<&Me>, Option<&PartInShip>), With<Part>>,
    me: Single<Entity, With<Me>>,
    debug: Res<AttachmentDebugRes>,
    drag: Res<DragResource>,
    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); }