From f185e5b890e0f16a8fa967f63ecf8985fc988752 Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Wed, 3 Jan 2024 00:01:25 -0600 Subject: [PATCH] detaching modules added --- server/src/component.rs | 2 +- server/src/main.rs | 102 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 94 insertions(+), 10 deletions(-) diff --git a/server/src/component.rs b/server/src/component.rs index a76180ff1afe96840363cacd1a13cb7f23753a3a..95cf9b2203e30387f8b0a0758993f20fc5e53ccd 100644 --- a/server/src/component.rs +++ b/server/src/component.rs @@ -31,7 +31,7 @@ pub enum PartType { #[derive(Component, Clone, Debug)] pub struct Attach { - pub associated_player: Option, + pub parent: Option, pub children: [Option; 4], } diff --git a/server/src/main.rs b/server/src/main.rs index 2dfb2fd8da1c448b2e925842d52eda2836f1cc76..38a21812fcc01dd6afde9af031069d83df3374b6 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -171,7 +171,7 @@ fn on_message( selected: None, }, attach: Attach { - associated_player: None, + parent: None, children: [None, None, None, None], }, }) @@ -358,7 +358,7 @@ fn on_message( break; }; q_player.selected = None; - { + if part_query.contains(select) { let mut module = part_query.get_mut(select).unwrap(); // attach module let p_pos = transform.translation; @@ -380,7 +380,7 @@ fn on_message( let mut module_entity = commands.entity(module.0); module_entity.insert(ImpulseJoint::new(entity, joint)); module_entity.insert(Attach { - associated_player: Some(entity), + parent: Some(entity), children: [None, None, None, None], }); attach.children[2] = Some(module.0); @@ -393,7 +393,7 @@ fn on_message( let mut module_entity = commands.entity(module.0); module_entity.insert(ImpulseJoint::new(entity, joint)); module_entity.insert(Attach { - associated_player: Some(entity), + parent: Some(entity), children: [None, None, None, None], }); attach.children[0] = Some(module.0); @@ -408,7 +408,7 @@ fn on_message( let mut module_entity = commands.entity(module.0); module_entity.insert(ImpulseJoint::new(entity, joint)); module_entity.insert(Attach { - associated_player: Some(entity), + parent: Some(entity), children: [None, None, None, None], }); attach.children[1] = Some(module.0); @@ -423,12 +423,40 @@ fn on_message( let mut module_entity = commands.entity(module.0); module_entity.insert(ImpulseJoint::new(entity, joint)); module_entity.insert(Attach { - associated_player: Some(entity), + parent: Some(entity), children: [None, None, None, None], }); attach.children[3] = Some(module.0); break; } + } else if attached_query.contains(select) { + let module = attached_query.get(select).unwrap(); + let parent = module.3.parent.unwrap(); + commands.entity(select).remove::(); + commands.entity(select).remove::(); + detach_recursive(&mut commands, module.3.clone(), &mut attached_query); + 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 { + for (i, child) in attach.children.clone().iter().enumerate() { + if let Some(child) = child { + if *child == select { + attach.children[i] = None; + let mut module = attached_query.get_mut(select).unwrap(); + module.2.translation = vec3(x / SCALE, y / SCALE, 0.); + } + } + } + } + break; } if attach_on_module_tree(x, y, &mut commands, attach.clone(), select, &mut attached_query, &mut part_query) { break; @@ -437,6 +465,31 @@ fn on_message( part_query.get_mut(select).unwrap().2.translation = vec3(x / SCALE, y / SCALE, 0.); break; } + for (entity, part_type, transform, _m_attach, _velocity) in &attached_query { + let pos = transform.translation; + let rel_x = pos.x - x / SCALE; + let rel_y = pos.y - y / SCALE; + let angle = -transform.rotation.z; + let x = rel_x * angle.cos() - rel_y * angle.sin(); + let y = rel_x * angle.sin() + rel_y * angle.cos(); + let mut bound = + [-25. / SCALE, 25. / SCALE, -25. / SCALE, 25. / SCALE]; // left, right, top, bottom + if let PartType::Cargo = part_type { + bound = [ + -18.75 / SCALE, + 18.75 / SCALE, + -25. / SCALE, + 21.875 / SCALE, + ]; + } + + if bound[0] < x && x < bound[1] { + if bound[2] < y && y < bound[3] { + q_player.selected = Some(entity); + break; + } + } + } for (entity, part_type, transform, _) in &part_query { let pos = transform.translation; let rel_x = pos.x - x / SCALE; @@ -474,6 +527,37 @@ fn on_message( } } +fn detach_recursive( + commands: &mut Commands, + attach: Attach, + attached_query: &mut Query<(Entity, &PartType, &mut Transform, &mut Attach, &Velocity), (Without, Without)>, +) { + for child in attach.children { + if let Some(child) = child { + { + 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); + } + let (entity, _part_type, _transform, attach, _velocity) = attached_query.get_mut(child).unwrap(); + 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; + } + } + } + } + } + } +} + fn attach_on_module_tree( x: f32, y: f32, @@ -507,7 +591,7 @@ fn attach_on_module_tree( let mut module_entity = commands.entity(module.0); module_entity.insert(ImpulseJoint::new(entity, joint)); module_entity.insert(Attach { - associated_player: Some(entity), + parent: Some(entity), children: [None, None, None, None], }); attach.children[2] = Some(module.0); @@ -522,7 +606,7 @@ fn attach_on_module_tree( let mut module_entity = commands.entity(module.0); module_entity.insert(ImpulseJoint::new(entity, joint)); module_entity.insert(Attach { - associated_player: Some(entity), + parent: Some(entity), children: [None, None, None, None], }); attach.children[1] = Some(module.0); @@ -537,7 +621,7 @@ fn attach_on_module_tree( let mut module_entity = commands.entity(module.0); module_entity.insert(ImpulseJoint::new(entity, joint)); module_entity.insert(Attach { - associated_player: Some(entity), + parent: Some(entity), children: [None, None, None, None], }); attach.children[3] = Some(module.0);