~starkingdoms/starkingdoms

11598ed575e840986321baf0d52602ceda7025e4 — ghostly_zsh 5 months ago d2dc2f8
fix: attachment rotation & velocity
2 files changed, 11 insertions(+), 4 deletions(-)

M crates/unified/src/server/part.rs
M crates/unified/src/server/player.rs
M crates/unified/src/server/part.rs => crates/unified/src/server/part.rs +2 -1
@@ 4,7 4,7 @@ use crate::config::part::{JointConfig, PartConfig};
use crate::ecs::{Part, PartHandle};
use bevy::prelude::Component;
use bevy::prelude::*;
use bevy_rapier2d::prelude::{AdditionalMassProperties, Collider};
use bevy_rapier2d::prelude::{AdditionalMassProperties, Collider, Velocity};
use bevy_replicon::prelude::Replicated;

pub fn part_management_plugin(app: &mut App) {


@@ 113,6 113,7 @@ fn calculate_bundle(config: &PartConfig, handle: &Handle<PartConfig>) -> impl Bu
        part_handle,
        collider,
        additional_mass_properties,
        Velocity::default(),
        Replicated,
    )
}

M crates/unified/src/server/player.rs => crates/unified/src/server/player.rs +9 -3
@@ 5,7 5,7 @@ use crate::server::system_sets::PlayerInputSet;
use crate::server::world_config::WorldConfigResource;
use crate::server::{ConnectedGameEntity, ConnectedNetworkEntity};
use bevy::prelude::*;
use bevy_rapier2d::prelude::{ExternalForce, FixedJointBuilder, ImpulseJoint};
use bevy_rapier2d::prelude::{ExternalForce, FixedJointBuilder, ImpulseJoint, Velocity};
use bevy_replicon::prelude::FromClient;
use std::f32::consts::PI;
use crate::attachment::{Joint, JointOf, PartInShip, Peer, SnapOf, SnapOfJoint};


@@ 19,7 19,7 @@ pub fn player_management_plugin(app: &mut App) {

fn dragging(
    mut events: EventReader<FromClient<DragRequestEvent>>,
    mut parts: Query<(&mut Transform, Option<&PartInShip>, Entity), (With<Part>, Without<Joint>)>,
    mut parts: Query<(&mut Transform, Option<&PartInShip>, Entity, &mut Velocity), (With<Part>, Without<Joint>)>,
    snaps: Query<(&SnapOf, &SnapOfJoint)>,
    joints: Query<(&Joint, &JointOf, &Transform, Option<&Peer>, Entity)>,
    clients: Query<&ConnectedNetworkEntity>,


@@ 32,6 32,7 @@ fn dragging(

        let mut teleport_to_translation = Vec2::new(0.0, 0.0);
        let mut teleport_to_rotation = Quat::from_rotation_z(0.0);
        let mut new_vel = None;

        if let Some(snap_to) = event.snap_target && let Some(peer_snap) = event.peer_snap {
            let Ok(snap_on_target) = snaps.get(snap_to) else { continue };


@@ 107,13 108,14 @@ fn dragging(
            // create the joint...
            let joint = FixedJointBuilder::new()
                .local_anchor1(target_joint.2.translation.xy())
                .local_basis2(rotation.as_radians());
                .local_basis2(0.0);

            commands.entity(source_part.2)
                .insert(ImpulseJoint::new(target_part.2, joint));

            teleport_to_translation = target_position.translation.xy();
            teleport_to_rotation = event.set_rotation;
            new_vel = Some(*target_part.3);
            // and we're done!
        } else {
            warn!("blindly accepting non-attachment request, someone should change this eventually");


@@ 126,6 128,9 @@ fn dragging(
        part.0.translation.x = teleport_to_translation.x;
        part.0.translation.y = teleport_to_translation.y;
        part.0.rotation = teleport_to_rotation; // client calculates this; no reason to recalculate
        if let Some(new_vel) = new_vel {
            *part.3 = new_vel;
        }
        // ( the math sucks )
    }
}


@@ 174,6 179,7 @@ fn handle_new_players(
                power_capacity: 25.0,
                power: 25.0,
            })
            .insert(Velocity::default())
            .insert(Player {
                client: joined_player,
            });