From 7f9073f2bdbee2080a9b4be4b0316fda3547966e Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Sat, 6 Jan 2024 16:47:30 -0600 Subject: [PATCH] module break off --- server/src/main.rs | 90 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 6 deletions(-) diff --git a/server/src/main.rs b/server/src/main.rs index d3cd7a071bb79f4d8f5f76598a86b7be90be8839..35dfa4c4b40887807d3849f460bf2b9ed3a1f4e5 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -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::::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, + mut attached_query: Query< + ( + Entity, + &PartType, + &mut Transform, + &mut Attach, + &Velocity, + Option<&CanAttach>, + Option<&LooseAttach>, + ), + (Without, Without), + >, + mut player_query: Query<&mut Attach, With>, +) { + 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::(); + commands.entity(entity).remove::(); + if part_type == PartType::LandingThruster { + commands.entity(entity).insert(LooseAttach { + children: attach.children, + }); + commands + .entity(attach.children[2].unwrap()) + .remove::(); + } 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>,