From 784aca277268c72af9c23957f9fb5c3536c8b78e Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Sat, 13 Jan 2024 15:53:34 -0600 Subject: [PATCH] module refactor done? --- bevy_tungstenite_stk/src/lib.rs | 2 +- server/src/main.rs | 214 +++++++++++--------------------- 2 files changed, 74 insertions(+), 142 deletions(-) diff --git a/bevy_tungstenite_stk/src/lib.rs b/bevy_tungstenite_stk/src/lib.rs index 10932fa72db533f655597b2ae893967c1cf64baa..995f65b7b80898f17a289a38019bcad809f6d61c 100644 --- a/bevy_tungstenite_stk/src/lib.rs +++ b/bevy_tungstenite_stk/src/lib.rs @@ -1,6 +1,6 @@ use bevy::app::{App, Plugin, PostUpdate, Startup}; use bevy::ecs::event::ManualEventReader; -use bevy::log::{warn, error}; +use bevy::log::{error, warn}; use bevy::prelude::{Commands, Event, Events, Local, Res, ResMut, Resource}; use crossbeam_channel::{unbounded, Receiver, Sender}; use std::collections::HashMap; diff --git a/server/src/main.rs b/server/src/main.rs index c7923502ecf1765e9377234f34b705d289653436..80fe988b636b108ec4249e1ba6154fc5a77a73e9 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -297,7 +297,7 @@ fn on_message( (Without, Without), >, mut player_query: Query< - (Entity, &mut Player, &Transform, &Velocity, &mut Attach), + (Entity, &mut Player, &Transform, &Velocity, &mut Attach, &mut PartFlags), Without, >, mut packet_recv: Local>, @@ -457,7 +457,7 @@ fn on_message( // tell the player already existing users let mut players = Vec::new(); - for (entity, player, _, _, _) in &player_query { + for (entity, player, _, _, _, _) in &player_query { players.push((entity.index(), player.username.clone())); } let packet = Packet::PlayerList { players }; @@ -561,7 +561,7 @@ fn on_message( Packet::SendMessage { target, content } => { // find our player let mut player = None; - for (_, q_player, _, _, _) in &player_query { + for (_, q_player, _, _, _, _) in &player_query { if q_player.addr == *from { player = Some(q_player); } @@ -569,7 +569,7 @@ fn on_message( let player = player.unwrap(); if let Some(target_username) = target { let mut target_player = None; - for (_, q_player, _, _, _) in &player_query { + for (_, q_player, _, _, _, _) in &player_query { if q_player.username == target_username { target_player = Some(q_player); } @@ -607,7 +607,7 @@ fn on_message( left, right, } => { - for (_, mut q_player, _, _, _) in &mut player_query { + for (_, mut q_player, _, _, _, _) in &mut player_query { if q_player.addr == *from { q_player.input.up = up; q_player.input.down = down; @@ -624,7 +624,7 @@ fn on_message( } => { let x = x / CLIENT_SCALE; let y = y / CLIENT_SCALE; - for (entity, mut q_player, _transform, _velocity, mut attach) in + for (entity, mut q_player, _transform, _velocity, _attach, _) in &mut player_query { if q_player.addr == *from { @@ -636,62 +636,20 @@ fn on_message( }; q_player.selected = None; if attached_query.contains(select) { + let module = attached_query.get(select).unwrap(); + let attach = module.3.clone(); + detach_recursive( + &mut commands, + module.0, + &mut attached_query, + &mut player_query, + ); let mut module = attached_query.get_mut(select).unwrap(); - let parent = module.3.parent.unwrap(); - commands.entity(select).remove::(); - commands.entity(select).remove::(); - let children_attach = module.3.clone(); + module.2.translation = vec3(x, y, 0.); if *module.1 == PartType::LandingThruster { - module.7.attached = false; - commands.entity(select).insert(LooseAttach { - children: module.3.children, - }); - commands - .entity(module.3.children[2].unwrap()) - .remove::(); - } else { - module.7.attached = false; - 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; - for (i, child) in children.iter().enumerate() { - if let Some(child) = child { - if *child == select { - parent_attach.children[i] = None; - } - } - } - } - let mut module = attached_query.get_mut(select).unwrap(); - module.2.translation = vec3(x, y, 0.); - } 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, y, 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, y, 0.); - } - } - } - } + let sub_entity = attach.children[2].unwrap(); + let mut suspension = attached_query.get_mut(sub_entity).unwrap(); + suspension.2.translation = vec3(x, y, 0.); } break; } @@ -767,7 +725,7 @@ fn on_message( } } Packet::RequestSave {} => { - for (_, q_player, _, _, attach) in &mut player_query { + for (_, q_player, _, _, attach, _) in &mut player_query { if q_player.addr == *from { // HEY! GHOSTLY! PLEASE FILL THIS STRUCT WITH DATA! // THANKS! @@ -1044,7 +1002,7 @@ fn construct_save_data( fn detach_recursive( commands: &mut Commands, - attach: Attach, + this: Entity, attached_query: &mut Query< ( Entity, @@ -1058,47 +1016,55 @@ fn detach_recursive( ), (Without, Without), >, + player_query: &mut Query< + (Entity, &mut Player, &Transform, &Velocity, &mut Attach, &mut PartFlags), + Without, + >, ) { - for child in attach.children.iter().flatten() { - { - let (entity, part_type, _transform, attach, _velocity, _, _, mut flags) = - attached_query.get_mut(*child).unwrap(); - commands.entity(entity).remove::(); - if *part_type == PartType::LandingThruster { - commands.entity(entity).insert(LooseAttach { - children: attach.children, - }); - commands - .entity(attach.children[2].unwrap()) - .remove::(); - flags.attached = false; - continue; - } else if *part_type == PartType::LandingThrusterSuspension { - flags.attached = false; - let parent = attach.parent.unwrap(); - let parent_attach = attached_query.get(parent).unwrap().3; - println!("suspension {:?} {:?}", parent_attach.children, entity); - commands.entity(parent).insert(LooseAttach { - children: parent_attach.children, - }); - } else { - flags.attached = false; - commands.entity(entity).remove::(); - detach_recursive(commands, attach.clone(), attached_query); + let f_attach = if attached_query.contains(this) { + attached_query.get(this).unwrap().3.clone() + } else { + player_query.get(this).unwrap().4.clone() + }; + for child in f_attach.children.iter().flatten() { + detach_recursive(commands, *child, attached_query, player_query); + } + let (entity, part_type, attach, mut flags) = if attached_query.contains(this) { + let (entity, part_type, _, attach, _, _, _, flags) = + attached_query.get_mut(this).unwrap(); + (entity, part_type, attach, flags) + } else { + let (entity, _, _, _, attach, part_flags) = player_query.get_mut(this).unwrap(); + (entity, &PartType::Hearty, attach, part_flags) + }; + commands.entity(entity).remove::(); + flags.attached = false; + 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::(); + } + let parent = f_attach.parent.unwrap(); + if attached_query.contains(parent) { + let mut parent_attach = attached_query.get_mut(parent).unwrap().3; + let children = parent_attach.children; + for (i, child) in children.iter().enumerate() { + if let Some(child) = child { + if *child == entity { + parent_attach.children[i] = None; + } } } - let (entity, _part_type, _transform, attach, _velocity, _, _, mut flags) = - attached_query.get_mut(*child).unwrap(); - flags.attached = false; - 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; - for (i, child) in children.iter().enumerate() { - if let Some(child) = child { - if *child == entity { - parent_attach.children[i] = None; - } + } else if player_query.contains(parent) { + let mut parent_attach = player_query.get_mut(parent).unwrap().4; + for (i, child) in parent_attach.children.clone().iter().enumerate() { + if let Some(child) = child { + if *child == entity { + parent_attach.children[i] = None; } } } @@ -1136,7 +1102,7 @@ fn attach_on_module_tree( (Without, Without, Without), >, player_query: &mut Query< - (Entity, &mut Player, &Transform, &Velocity, &mut Attach), + (Entity, &mut Player, &Transform, &Velocity, &mut Attach, &mut PartFlags), Without, >, ) -> bool { @@ -1171,7 +1137,7 @@ fn attach_on_module_tree( attached_query.get_mut(this).unwrap(); (entity, *transform, attach, velocity, can_attach) } else { - let (entity, _, transform, velocity, attach) = player_query.get_mut(this).unwrap(); + let (entity, _, transform, velocity, attach, _) = player_query.get_mut(this).unwrap(); (entity, *transform, attach, velocity, Some(&CanAttach(15))) }; @@ -1254,17 +1220,17 @@ fn attach_on_module_tree( .local_basis2(angle_offset); let mut children = [None, None, None, None]; if let Some(loose_attach) = module.4 { - commands.entity(entity).remove::(); if *module.1 == PartType::LandingThruster { commands .entity(loose_attach.children[2].unwrap()) .insert(Attach { associated_player, - parent: Some(entity), + parent: Some(module.0), children: [None, None, None, None], }); } children = loose_attach.children; + commands.entity(entity).remove::(); } let mut module_entity = commands.entity(module.0); module_entity.insert(ImpulseJoint::new(entity, joint)); @@ -1542,7 +1508,7 @@ fn break_modules( ), (Without, Without), >, - mut player_query: Query<&mut Attach, With>, + mut player_query: Query<(Entity, &mut Player, &Transform, &Velocity, &mut Attach, &mut PartFlags), Without>, ) { let joints = rapier_context.entity2impulse_joint(); let mut detach_list = Vec::new(); @@ -1557,42 +1523,8 @@ fn break_modules( 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, - }); - if attach.children[2].is_some() { - 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; - 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; - } - } - } - } + for (entity, _part_type, _attach) in detach_list { + detach_recursive(&mut commands, entity, &mut attached_query, &mut player_query); } }