~starkingdoms/starkingdoms

7f9073f2bdbee2080a9b4be4b0316fda3547966e — ghostlyzsh 1 year, 11 months ago 12dc42a
module break off
1 files changed, 84 insertions(+), 6 deletions(-)

M server/src/main.rs
M server/src/main.rs => server/src/main.rs +84 -6
@@ 1,4 1,3 @@
use std::f32::consts::PI;
// StarKingdoms.IO, a browser game about drifting through space
//     Copyright (C) 2023 ghostly_zsh, TerraMaster85, core
//


@@ 27,6 26,7 @@ use component::Input;
use component::*;
use packet::*;
use rand::Rng;
use std::f32::consts::PI;

pub mod component;
pub mod macros;


@@ 76,7 76,7 @@ fn main() {
        .add_systems(Update, on_message)
        .add_systems(Update, on_close)
        .add_systems(FixedUpdate, on_position_change)
        .add_systems(FixedUpdate, (gravity_update, player_input_update).chain())
        .add_systems(FixedUpdate, (break_modules, gravity_update, player_input_update).chain())
        .add_systems(FixedUpdate, convert_modules)
        //.insert_resource(Time::<Fixed>::from_seconds(1.0/20.0))
        .run();


@@ 175,6 175,11 @@ fn module_spawn(
                            0.,
                        )));
                })
                .insert(AdditionalMassProperties::MassProperties(MassProperties {
                    local_center_of_mass: vec2(0.0, 0.0),
                    mass: 0.0001,
                    principal_inertia: 0.005,
                }))
                .insert(ExternalForce::default())
                .insert(ExternalImpulse::default())
                .insert(Velocity::default())


@@ 481,7 486,7 @@ fn on_message(
                                                commands
                                                    .entity(loose_attach.children[2].unwrap())
                                                    .insert(Attach {
                                                        associated_player: attach.associated_player,
                                                        associated_player: Some(entity),
                                                        parent: Some(entity),
                                                        children: [None, None, None, None],
                                                    });


@@ 534,7 539,7 @@ fn on_message(
                                                commands
                                                    .entity(loose_attach.children[2].unwrap())
                                                    .insert(Attach {
                                                        associated_player: attach.associated_player,
                                                        associated_player: Some(entity),
                                                        parent: Some(entity),
                                                        children: [None, None, None, None],
                                                    });


@@ 592,7 597,7 @@ fn on_message(
                                                commands
                                                    .entity(loose_attach.children[2].unwrap())
                                                    .insert(Attach {
                                                        associated_player: attach.associated_player,
                                                        associated_player: Some(entity),
                                                        parent: Some(entity),
                                                        children: [None, None, None, None],
                                                    });


@@ 654,7 659,7 @@ fn on_message(
                                                commands
                                                    .entity(loose_attach.children[2].unwrap())
                                                    .insert(Attach {
                                                        associated_player: attach.associated_player,
                                                        associated_player: Some(entity),
                                                        parent: Some(entity),
                                                        children: [None, None, None, None],
                                                    });


@@ 1232,6 1237,11 @@ fn convert_modules_recursive(
                            .insert(ExternalImpulse::default())
                            .insert(Velocity::default())
                            .insert(ReadMassProperties::default())
                            .insert(AdditionalMassProperties::MassProperties(MassProperties {
                                local_center_of_mass: vec2(0.0, 0.0),
                                mass: 0.00000000000001,
                                principal_inertia: 0.0000000000001,
                            }))
                            .insert(Attach {
                                associated_player: attach.associated_player,
                                parent: Some(module_entity),


@@ 1281,6 1291,74 @@ fn convert_modules_recursive(
    }
}

fn break_modules(
    mut commands: Commands,
    rapier_context: Res<RapierContext>,
    mut attached_query: Query<
        (
            Entity,
            &PartType,
            &mut Transform,
            &mut Attach,
            &Velocity,
            Option<&CanAttach>,
            Option<&LooseAttach>,
        ),
        (Without<PlanetType>, Without<Player>),
    >,
    mut player_query: Query<&mut Attach, With<Player>>,
) {
    let joints = rapier_context.entity2impulse_joint();
    let mut detach_list = Vec::new();
    for (entity, part_type, _, attach, _, _, _) in &mut attached_query {
        if *part_type == PartType::LandingThrusterSuspension { continue }
        let handle = joints.get(&entity).unwrap();
        let joint = rapier_context.impulse_joints.get(*handle).unwrap();
        if joint.impulses.magnitude() > 0.00001 {
            detach_list.push((entity, *part_type, attach.clone()));
        }
    }
    for (entity, part_type, attach) in detach_list {
        commands.entity(entity).remove::<ImpulseJoint>();
        commands.entity(entity).remove::<Attach>();
        if part_type == PartType::LandingThruster {
            commands.entity(entity).insert(LooseAttach {
                children: attach.children,
            });
            commands
                .entity(attach.children[2].unwrap())
                .remove::<Attach>();
        } else {
            detach_recursive(
                &mut commands,
                attach.clone(),
                &mut attached_query,
            );
        }
        let parent = attach.parent.unwrap();
        if attached_query.contains(parent) {
            let mut parent_attach = attached_query.get_mut(parent).unwrap().3;
            let children = parent_attach.children.clone();
            for (i, child) in children.iter().enumerate() {
                if let Some(child) = child {
                    if *child == entity {
                        parent_attach.children[i] = None;
                    }
                }
            }
        } else {
            let mut attach = player_query.get_mut(parent).unwrap();
            for (i, child) in attach.children.clone().iter().enumerate() {
                if let Some(child) = child {
                    if *child == entity {
                        attach.children[i] = None;
                    }
                }
            }
        }
    }
}

fn on_close(
    player_query: Query<(Entity, &Player, &Attach)>,
    attached_query: Query<&Attach, With<PartType>>,