~starkingdoms/starkingdoms

fecb8b0d7b8a02c546424981fce709f5fcf17b94 — ghostlyzsh 2 years ago b494ad7
almost nothing changed but server now sends ModuleAdd packet
2 files changed, 52 insertions(+), 16 deletions(-)

M server/src/handler.rs
M server/src/module.rs
M server/src/handler.rs => server/src/handler.rs +26 -15
@@ 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<Module> = 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?;
                            }
                        }
                    }
                },

M server/src/module.rs => server/src/module.rs +26 -1
@@ 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<AttachedModule> {
        let mut modules = vec![self.clone()];
        for attachment in self.children.iter().flatten() {