From db4a4e62c2b97ef76cce6b80d9e2a68b1e861dc1 Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Fri, 5 Jan 2024 21:25:37 -0600 Subject: [PATCH] can now move loose landing thrusters properly --- server/src/component.rs | 5 ++ server/src/main.rs | 109 +++++++++++++++++++++++++++++++++------- 2 files changed, 95 insertions(+), 19 deletions(-) diff --git a/server/src/component.rs b/server/src/component.rs index 20aa2185f76798ce714ddee993680d6af6504ea4..b13286a1e791ed4d442e20ee932f8373790eb678 100644 --- a/server/src/component.rs +++ b/server/src/component.rs @@ -41,6 +41,11 @@ pub struct Attach { pub children: [Option; 4], } +#[derive(Component, Clone, Debug)] +pub struct LooseAttach { + pub children: [Option; 4], +} + #[derive(Component, Clone, Copy, PartialEq, Debug)] pub struct CanAttach(pub u8); // each bit means a slot able to attach to diff --git a/server/src/main.rs b/server/src/main.rs index 5a8fd6f1a1596ce48697077e38e1e885216c955c..88a0c1069b3bc156e5e2232f7151615a158d0a60 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -195,11 +195,19 @@ fn on_message( mut commands: Commands, planet_query: Query<(Entity, &PlanetType, &Transform)>, mut part_query: Query< - (Entity, &PartType, &mut Transform, &mut Velocity), + (Entity, &PartType, &mut Transform, &mut Velocity, Option<&LooseAttach>), (Without, Without, Without), >, mut attached_query: Query< - (Entity, &PartType, &mut Transform, &mut Attach, &Velocity, Option<&CanAttach>), + ( + Entity, + &PartType, + &mut Transform, + &mut Attach, + &Velocity, + Option<&CanAttach>, + Option<&LooseAttach>, + ), (Without, Without), >, mut player_query: Query< @@ -314,7 +322,7 @@ fn on_message( // tell the player where parts are let mut parts = Vec::new(); - for (entity, part_type, transform, _) in &part_query { + for (entity, part_type, transform, _, _) in &part_query { parts.push(( entity.index(), Part { @@ -325,7 +333,7 @@ fn on_message( }, )); } - for (entity, part_type, transform, _, _, _) in &attached_query { + for (entity, part_type, transform, _, _, _, _) in &attached_query { parts.push(( entity.index(), Part { @@ -561,6 +569,10 @@ fn on_message( let parent = module.3.parent.unwrap(); commands.entity(select).remove::(); commands.entity(select).remove::(); + if *module.1 == PartType::LandingThruster { + commands.entity(entity).insert(LooseAttach { children: attach.children }); + } + let children_attach = module.3.clone(); detach_recursive( &mut commands, module.3.clone(), @@ -591,6 +603,12 @@ fn on_message( attached_query.get_mut(select).unwrap(); module.2.translation = vec3(x / SCALE, y / SCALE, 0.); + if *module.1 == PartType::LandingThruster { + let sub_entity = children_attach.children[2].unwrap(); + let mut suspension = attached_query.get_mut(sub_entity).unwrap(); + suspension.2.translation = + vec3(x / SCALE, y / SCALE, 0.); + } } } } @@ -609,13 +627,24 @@ fn on_message( break; } // move module to cursor since no attach - part_query.get_mut(select).unwrap().2.translation = + let mut part = part_query.get_mut(select).unwrap(); + part.2.translation = vec3(x / SCALE, y / SCALE, 0.); + if *part.1 == PartType::LandingThruster { + if let Some(loose_attach) = part.4 { + let sub_entity = loose_attach.children[2].unwrap(); + let mut part = part_query.get_mut(sub_entity).unwrap(); + part.2.translation = vec3(x / SCALE, y / SCALE, 0.); + } + } break; } - for (entity, part_type, transform, _m_attach, _velocity, _) in + for (entity, part_type, transform, _m_attach, _velocity, _, _) in &attached_query { + if *part_type == PartType::LandingThrusterSuspension { + continue; + } let pos = transform.translation; let rel_x = pos.x - x / SCALE; let rel_y = pos.y - y / SCALE; @@ -640,7 +669,10 @@ fn on_message( } } } - for (entity, part_type, transform, _) in &part_query { + for (entity, part_type, transform, _, _) in &part_query { + if *part_type == PartType::LandingThrusterSuspension { + continue; + } let pos = transform.translation; let rel_x = pos.x - x / SCALE; let rel_y = pos.y - y / SCALE; @@ -681,21 +713,37 @@ fn detach_recursive( commands: &mut Commands, attach: Attach, attached_query: &mut Query< - (Entity, &PartType, &mut Transform, &mut Attach, &Velocity, Option<&CanAttach>), + ( + Entity, + &PartType, + &mut Transform, + &mut Attach, + &Velocity, + Option<&CanAttach>, + Option<&LooseAttach>, + ), (Without, Without), >, ) { for child in attach.children { if let Some(child) = child { { - let (entity, _part_type, _transform, attach, _velocity, _) = + let (entity, part_type, _transform, attach, _velocity, _, _) = attached_query.get(child).unwrap(); - commands.entity(entity).remove::(); commands.entity(entity).remove::(); - - detach_recursive(commands, attach.clone(), attached_query); + if *part_type == PartType::LandingThruster { + commands.entity(entity).remove::(); + commands.entity(entity).insert(LooseAttach { children: attach.children }); + } else if *part_type == PartType::LandingThrusterSuspension { + let parent = attach.parent.unwrap(); + let parent_attach = attached_query.get(parent).unwrap().3; + commands.entity(parent).insert(LooseAttach { children: parent_attach.children }); + } else { + commands.entity(entity).remove::(); + detach_recursive(commands, attach.clone(), attached_query); + } } - let (entity, _part_type, _transform, attach, _velocity, _) = + let (entity, _part_type, _transform, attach, _velocity, _, _) = attached_query.get_mut(child).unwrap(); let parent = attach.parent.unwrap(); if attached_query.contains(parent) { @@ -720,18 +768,26 @@ fn attach_on_module_tree( attach: Attach, select: Entity, attached_query: &mut Query< - (Entity, &PartType, &mut Transform, &mut Attach, &Velocity, Option<&CanAttach>), + ( + Entity, + &PartType, + &mut Transform, + &mut Attach, + &Velocity, + Option<&CanAttach>, + Option<&LooseAttach>, + ), (Without, Without), >, part_query: &mut Query< - (Entity, &PartType, &mut Transform, &mut Velocity), + (Entity, &PartType, &mut Transform, &mut Velocity, Option<&LooseAttach>), (Without, Without, Without), >, ) -> bool { let mut ret = false; for child in attach.children { if let Some(child) = child { - let (entity, _part_type, transform, mut attach, velocity, can_attach) = + let (entity, _part_type, transform, mut attach, velocity, can_attach, loose_attach) = attached_query.get_mut(child).unwrap(); let p_pos = transform.translation; @@ -758,12 +814,17 @@ fn attach_on_module_tree( module.2.rotation = Quat::from_euler(EulerRot::ZYX, angle, 0., 0.); module.3.linvel = velocity.linvel; let joint = FixedJointBuilder::new().local_anchor1(vec2(0. / SCALE, -53. / SCALE)); + let mut children = [None, None, None, None]; + if let Some(loose_attach) = loose_attach { + commands.entity(entity).remove::(); + children = loose_attach.children; + } let mut module_entity = commands.entity(module.0); module_entity.insert(ImpulseJoint::new(entity, joint)); module_entity.insert(Attach { associated_player: attach.associated_player, parent: Some(entity), - children: [None, None, None, None], + children, }); attach.children[2] = Some(module.0); return true; @@ -785,12 +846,17 @@ fn attach_on_module_tree( let joint = FixedJointBuilder::new() .local_anchor1(vec2(53. / SCALE, 0. / SCALE)) .local_basis2(std::f32::consts::PI / 2.); + let mut children = [None, None, None, None]; + if let Some(loose_attach) = loose_attach { + commands.entity(entity).remove::(); + children = loose_attach.children; + } let mut module_entity = commands.entity(module.0); module_entity.insert(ImpulseJoint::new(entity, joint)); module_entity.insert(Attach { associated_player: attach.associated_player, parent: Some(entity), - children: [None, None, None, None], + children, }); attach.children[1] = Some(module.0); return true; @@ -812,12 +878,17 @@ fn attach_on_module_tree( let joint = FixedJointBuilder::new() .local_anchor1(vec2(-53. / SCALE, 0. / SCALE)) .local_basis2(-std::f32::consts::PI / 2.); + let mut children = [None, None, None, None]; + if let Some(loose_attach) = loose_attach { + commands.entity(entity).remove::(); + children = loose_attach.children; + } let mut module_entity = commands.entity(module.0); module_entity.insert(ImpulseJoint::new(entity, joint)); module_entity.insert(Attach { associated_player: attach.associated_player, parent: Some(entity), - children: [None, None, None, None], + children, }); attach.children[3] = Some(module.0); return true;