From fecb8b0d7b8a02c546424981fce709f5fcf17b94 Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Sat, 24 Jun 2023 19:01:00 -0500 Subject: [PATCH] almost nothing changed but server now sends ModuleAdd packet --- server/src/handler.rs | 41 ++++++++++++++++++++++++++--------------- server/src/module.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/server/src/handler.rs b/server/src/handler.rs index 59d309a053d72d7014797d6cac33a6446a8204e7..f6714bafc3922c8a9c3177436795856486e38a91 100644 --- a/server/src/handler.rs +++ b/server/src/handler.rs @@ -19,7 +19,7 @@ use rapier2d_f64::prelude::{ use starkingdoms_protocol::goodbye_reason::GoodbyeReason; use starkingdoms_protocol::message_s2c::{ MessageS2CChat, MessageS2CGoodbye, MessageS2CHello, MessageS2CModulesUpdate, - MessageS2CPlanetData, MessageS2CPlayersUpdate, MessageS2CPong, + MessageS2CPlanetData, MessageS2CPlayersUpdate, MessageS2CPong, MessageS2CModuleAdd, }; use starkingdoms_protocol::module::ModuleType; use starkingdoms_protocol::state::State; @@ -444,6 +444,7 @@ pub async fn handle_client( let mut entities = entities.write().await; let mut module: Option = None; let mut did_attach = false; + let mut attached_id = None; if let Entity::Module(p_module) = entities.entities.get_mut(&p.module_id).unwrap() { p_module.flags &= !2; module = Some(p_module.clone()); @@ -457,24 +458,24 @@ pub async fn handle_client( let angle = -body.rotation().angle(); let (x, y) = (x*angle.cos() - y*angle.sin(), x*angle.sin() + y*angle.cos()); if 1.5 < y && y < 3. && -2. < x && x < 2. { - AttachedModule::attach(&mut data_handle, &mut entities, + attached_id = Some(AttachedModule::attach(&mut data_handle, &mut entities, player_id, - player_id, module.clone().unwrap(), 2); + player_id, module.clone().unwrap(), 2)); did_attach = true; } else if -3. < y && y < -1.5 && -2. < x && x < 2. { - AttachedModule::attach(&mut data_handle, &mut entities, + attached_id = Some(AttachedModule::attach(&mut data_handle, &mut entities, player_id, - player_id, module.clone().unwrap(), 0); + player_id, module.clone().unwrap(), 0)); did_attach = true; } else if -3. < x && x < -1.5 && -2. < y && y < 2. { - AttachedModule::attach(&mut data_handle, &mut entities, + attached_id = Some(AttachedModule::attach(&mut data_handle, &mut entities, player_id, - player_id, module.clone().unwrap(), 3); + player_id, module.clone().unwrap(), 3)); did_attach = true; } else if 1.5 < x && x < 3. && -2. < y && y < 2. { - AttachedModule::attach(&mut data_handle, &mut entities, + attached_id = Some(AttachedModule::attach(&mut data_handle, &mut entities, player_id, - player_id, module.clone().unwrap(), 1); + player_id, module.clone().unwrap(), 1)); did_attach = true; } let modules = player.search_modules(&entities); @@ -485,25 +486,35 @@ pub async fn handle_client( let (x, y) = (x*angle.cos() - y*angle.sin(), x*angle.sin() + y*angle.cos()); let parent_id = entities.get_id_from_attached(attached).unwrap(); if 1.5 < y && y < 3. && -2. < x && x < 2. { - AttachedModule::attach(&mut data_handle, &mut entities, + attached_id = Some(AttachedModule::attach(&mut data_handle, &mut entities, parent_id, - player_id, module.clone().unwrap(), 2); + player_id, module.clone().unwrap(), 2)); did_attach = true; } else if -3. < x && x < -1.5 && -2. < y && y < 2. { - AttachedModule::attach(&mut data_handle, &mut entities, + attached_id = Some(AttachedModule::attach(&mut data_handle, &mut entities, parent_id, - player_id, module.clone().unwrap(), 3); + player_id, module.clone().unwrap(), 3)); did_attach = true; } else if 1.5 < x && x < 3. && -2. < y && y < 2. { - AttachedModule::attach(&mut data_handle, &mut entities, + attached_id = Some(AttachedModule::attach(&mut data_handle, &mut entities, parent_id, - player_id, module.clone().unwrap(), 1); + player_id, module.clone().unwrap(), 1)); did_attach = true; } } if did_attach == false { let body = data_handle.rigid_body_set.get_mut(module.unwrap().handle).unwrap(); body.set_position(Isometry::new(Vector2::new(p.worldpos_x/SCALE, p.worldpos_y/SCALE),body.rotation().angle()), true); + } else { + if let Some(id) = attached_id { + let prot_module = entities.get_attached_from_id(id).unwrap().to_protocol(&entities, &mut data_handle); + let msg = MessageS2C::ModuleAdd(MessageS2CModuleAdd { + module: Some(prot_module).into(), + special_fields: Default::default(), + }) + .try_into()?; + send!(client_tx, msg).await?; + } } } }, diff --git a/server/src/module.rs b/server/src/module.rs index d01579e9af07467608af952e99f5b5da113e14f7..5d69aaa0c38cf0e078054d570c228ff5fa58da13 100644 --- a/server/src/module.rs +++ b/server/src/module.rs @@ -37,7 +37,7 @@ impl AttachedModule { player_id: EntityId, module: Module, attachment_slot: usize, - ) { + ) -> EntityId { let mut entity_map = entities.entities.clone(); let parent_entity = entity_map @@ -137,6 +137,7 @@ impl AttachedModule { entity_map.remove(&entities.get_from_module(&module).unwrap()); entity_map.insert(attached_id, Entity::AttachedModule(attached_module)); entities.entities = entity_map; + attached_id } pub fn attach_new( data: &mut PhysicsData, @@ -272,6 +273,30 @@ impl AttachedModule { }) } + pub fn to_protocol(&self, entities: &EntityHandler, data: &mut PhysicsData) -> starkingdoms_protocol::module::AttachedModule { + let body = data.rigid_body_set.get(self.handle).unwrap(); + let children = self.children.to_vec(); + let mut prot_children = Vec::new(); + for i in 1..children.len() { + if let Some(Some(child)) = children.get(i) { + prot_children.push(starkingdoms_protocol::module::Attachment { + id: child.child, + slot: i as u32, + special_fields: Default::default(), + }); + } + } + starkingdoms_protocol::module::AttachedModule { + module_type: self.module_type.into(), + rotation: body.rotation().angle(), + x: body.translation().x, + y: body.translation().y, + id: entities.get_id_from_attached(self.clone()).unwrap(), + children: prot_children, + special_fields: Default::default(), + } + } + pub fn search_modules(&self, entities: &EntityHandler) -> Vec { let mut modules = vec![self.clone()]; for attachment in self.children.iter().flatten() {