~starkingdoms/starkingdoms

28eaeed194f4db9271d59cb92cd32816bd757340 — ghostlyzsh 2 years ago 72e0955
grab fix and proper bounding boxes
M client/src/gateway.ts => client/src/gateway.ts +1 -0
@@ 196,6 196,7 @@ export async function gateway_connect(gateway_url: string, username: string): Pr
                    children: pkt.module!.children,
                };
                global.tree.set(pkt.module!.id, module);
                global.clicked = null;
            } else if (pkt_id == MessageS2CModuleRemove_packetInfo.type) {
                let pkt = MessageS2CModuleRemove.decode(pkt_data);
                global.clicked = pkt.module!.id;

M client/src/index.ts => client/src/index.ts +17 -7
@@ 148,8 148,12 @@ async function client_main(server: string, username: string, texture_quality: st
                let rot = -global.modules[i].rotation;
                relativeX = relativeX*Math.cos(rot) - relativeY*Math.sin(rot);
                relativeY = relativeX*Math.sin(rot) + relativeY*Math.cos(rot);
                if (-25 < relativeX && relativeX < 25) {
                    if (-25 < relativeY && relativeY < 25) {
                let bound = [-25, 25, -25, 25];
                if (global.modules[i].moduleType == ModuleType.Cargo) {
                    bound = [-18.75, 18.75, -25, 21.875];
                }
                if (bound[0] < relativeX && relativeX < bound[1]) {
                    if (bound[2] < relativeY && relativeY < bound[3]) {
                        let msg = MessageC2SModuleGrabBegin.encode({
                            moduleId: global.modules[i].id,
                            worldposX: worldX,


@@ 164,10 168,16 @@ async function client_main(server: string, username: string, texture_quality: st
                let relativeX = value.x - worldX;
                let relativeY = value.y - worldY;
                let rot = -value.rotation;
                relativeX = relativeX*Math.cos(rot) - relativeY*Math.sin(rot);
                relativeY = relativeX*Math.sin(rot) + relativeY*Math.cos(rot);
                if (-25 < relativeX && relativeX < 25) {
                    if (-25 < relativeY && relativeY < 25) {
                let adjustedX = relativeX*Math.cos(rot) - relativeY*Math.sin(rot);
                let adjustedY = relativeX*Math.sin(rot) + relativeY*Math.cos(rot);
                let bound = [-25, 25, -25, 25];
                if (value.module_type == ModuleType.Cargo) {
                    bound = [-18.75, 18.75, -25, 21.875];
                }
                if (bound[0] < adjustedX && adjustedX < bound[1]) {
                    if (bound[2] < adjustedY && adjustedY < bound[3]) {
                        console.log("relative: " + relativeX + ", " + relativeY);
                        console.log("adjusted: " + adjustedX + ", " + adjustedY);
                        let msg = MessageC2SModuleDetach.encode({
                            moduleId: key,
                        }).finish();


@@ 211,7 221,6 @@ async function client_main(server: string, username: string, texture_quality: st
            
            for (let i = 0; i < global.modules.length; i++) {
                if(global.clicked === global.modules[i].id) {
                    global.clicked = null;
                    let msg = MessageC2SModuleGrabEnd.encode({
                        moduleId: global.modules[i].id,
                        worldposX: worldX,


@@ 220,6 229,7 @@ async function client_main(server: string, username: string, texture_quality: st
                    global.client?.socket.send(encode(MessageC2SModuleGrabEnd_packetInfo.type, msg))
                }
            }
            global.clicked = null;

            global.client?.socket.send(encode(MessageC2SMouseInput_packetInfo.type, msg))
        }

M server/src/handler.rs => server/src/handler.rs +3 -9
@@ 448,21 448,15 @@ pub async fn handle_client(
                        let mut entities = entities.write().await;
                        let mut data_handle = data.write().await;
                        let mut module: Option<AttachedModule> = None;
                        debug!("[{}] detach: {:?}", remote_addr, p);
                        debug!("[{}] {:?}", remote_addr, entities.entities);
                        //debug!("[{}] detach: {:?}", remote_addr, p);
                        //debug!("[{}] {:?}", remote_addr, entities.entities);



                        // START: MY CHANGES
                        if let Some(Entity::AttachedModule(p_module)) = entities.entities.get_mut(&p.module_id) {
                            module = Some(p_module.clone());
                        } else {
                            warn!("[{}] attempted to detach nonexistent module", remote_addr);
                            continue;
                        }
                        // END: MY CHANGES



                        let player_id = entities.get_player_id(remote_addr).unwrap();
                        let module_id = AttachedModule::detach(&mut data_handle, &mut entities,


@@ 498,7 492,7 @@ pub async fn handle_client(
                        let mut attached_id = None;
                        if let Entity::Module(p_module) = entities.entities.get_mut(&p.module_id).unwrap() {
                            module = Some(p_module.clone());
                            debug!("[{}] grab end: {:?}", remote_addr, p);
                            //debug!("[{}] grab end: {:?}", remote_addr, p);
                        }
                        let mut data_handle = data.write().await;
                        let player_id = entities.get_player_id(remote_addr).unwrap();

M server/src/manager.rs => server/src/manager.rs +0 -2
@@ 59,8 59,6 @@ impl Player {
                if let Entity::AttachedModule(child_module) =
                    entities.entities.get(&attachment.child).unwrap()
                {
                    debug!("player child: {:?}", *child_module);
                    debug!("player target: {:?}", module);
                    if *child_module == module {
                        return Some((slot as u8, entities.get_player_id(self.addr).unwrap()));
                    }

M server/src/module.rs => server/src/module.rs +50 -50
@@ 102,54 102,57 @@ impl AttachedModule {
            }
        };

        let relative_pos = 
            vector![anchor.x *  (parent_body.rotation().angle()).cos() + 
                    anchor.y * -(parent_body.rotation().angle()).sin(),
                    anchor.x *  (parent_body.rotation().angle()).sin() +
                    anchor.y *  (parent_body.rotation().angle()).cos()];
        let module_pos = parent_pos + relative_pos;
        let module_body = data.rigid_body_set.get_mut(module.handle).unwrap();
        module_body.set_translation(module_pos, true);
        module_body.set_rotation(Unit::from_angle(parent_angle + rotation), true);
        module_body.set_linvel(parent_linvel, true);
        module_body.set_angvel(parent_angvel, true);
        if let Some(id) = entities.get_from_module(&module) {
            let relative_pos = 
                vector![anchor.x *  (parent_body.rotation().angle()).cos() + 
                        anchor.y * -(parent_body.rotation().angle()).sin(),
                        anchor.x *  (parent_body.rotation().angle()).sin() +
                        anchor.y *  (parent_body.rotation().angle()).cos()];
            let module_pos = parent_pos + relative_pos;
            let module_body = data.rigid_body_set.get_mut(module.handle).unwrap();
            module_body.set_translation(module_pos, true);
            module_body.set_rotation(Unit::from_angle(parent_angle + rotation), true);
            module_body.set_linvel(parent_linvel, true);
            module_body.set_angvel(parent_angvel, true);

        let attach_joint = FixedJointBuilder::new()
            .local_anchor1(anchor)
            .local_anchor2(point![0.0, 0.0 / SCALE])
            .local_frame2(Isometry2::rotation(rotation))
            .build();
        let attach_joint_handle =
            data.impulse_joint_set
                .insert(parent_handle, module.handle, attach_joint, true);
        let attached_module = AttachedModule {
            handle: module.handle,
            module_type: module.module_type,
            player_id,
            children: [None, None, None, None],
        };
        let attached_id = get_entity_id();
        match parent_entity {
            Entity::Player(ref mut player) => {
                player.children[attachment_slot] = Some(Attachment {
                    child: attached_id,
                    connection: attach_joint_handle,
                });
            }
            Entity::AttachedModule(ref mut module) => {
                module.children[attachment_slot] = Some(Attachment {
                    child: attached_id,
                    connection: attach_joint_handle,
                });
            }
            _ => {
                panic!("unexpected parent");
            }
        };
        entity_map.remove(&entities.get_from_module(&module).unwrap());
        entity_map.insert(attached_id, Entity::AttachedModule(attached_module));
        entities.entities = entity_map;
        Ok(attached_id)
            let attach_joint = FixedJointBuilder::new()
                .local_anchor1(anchor)
                .local_anchor2(point![0.0, 0.0 / SCALE])
                .local_frame2(Isometry2::rotation(rotation))
                .build();
            let attach_joint_handle =
                data.impulse_joint_set
                    .insert(parent_handle, module.handle, attach_joint, true);
            let attached_module = AttachedModule {
                handle: module.handle,
                module_type: module.module_type,
                player_id,
                children: [None, None, None, None],
            };
            let attached_id = get_entity_id();
            match parent_entity {
                Entity::Player(ref mut player) => {
                    player.children[attachment_slot] = Some(Attachment {
                        child: attached_id,
                        connection: attach_joint_handle,
                    });
                }
                Entity::AttachedModule(ref mut module) => {
                    module.children[attachment_slot] = Some(Attachment {
                        child: attached_id,
                        connection: attach_joint_handle,
                    });
                }
                _ => {
                    panic!("unexpected parent");
                }
            };
            entity_map.remove(&id);
            entity_map.insert(attached_id, Entity::AttachedModule(attached_module));
            entities.entities = entity_map;
            return Ok(attached_id);
        }
        Err(())
    }
    pub fn detach(
        data: &mut PhysicsData,


@@ 198,7 201,6 @@ impl AttachedModule {
        for element in tree {
            for child in element.clone().children {
                if let Some(child) = child {
                    debug!("child: {:?}", child);
                    data.impulse_joint_set.remove(child.connection, true);
                    let child_body = entities.get_attached_from_id(child.child).unwrap();
                    let new_module = Module {


@@ 392,8 394,6 @@ impl AttachedModule {
                if let Entity::AttachedModule(child_module) =
                    entities.entities.get(&attachment.child).unwrap()
                {
                    debug!("child: {:?}", *child_module);
                    debug!("target: {:?}", module);
                    if *child_module == module {
                        return Some((slot as u8, entities.get_id_from_attached(self.clone()).unwrap()));
                    }