From 28eaeed194f4db9271d59cb92cd32816bd757340 Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Wed, 28 Jun 2023 21:58:36 -0500 Subject: [PATCH] grab fix and proper bounding boxes --- client/src/gateway.ts | 1 + client/src/index.ts | 24 +++++++--- server/src/handler.rs | 12 ++--- server/src/manager.rs | 2 - server/src/module.rs | 100 +++++++++++++++++++++--------------------- 5 files changed, 71 insertions(+), 68 deletions(-) diff --git a/client/src/gateway.ts b/client/src/gateway.ts index 0c1becbd8ed40a71fa8b0a04f1c3e98cec83ab74..ef8fbc23d2f887ffd94f7a0418dfdc599154a0b0 100644 --- a/client/src/gateway.ts +++ b/client/src/gateway.ts @@ -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; diff --git a/client/src/index.ts b/client/src/index.ts index 44982eb2c29eefddbdf5809fcb2c61443fceda22..4710ade20e59e0ab5eab9c3c160d1bb135060c6c 100644 --- a/client/src/index.ts +++ b/client/src/index.ts @@ -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)) } diff --git a/server/src/handler.rs b/server/src/handler.rs index ecf58bf6e9d92ca5bf21af0bfd0a7638c659152f..a20a687ff2f074ff747a66cb9115e9a64aecf337 100644 --- a/server/src/handler.rs +++ b/server/src/handler.rs @@ -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 = 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(); diff --git a/server/src/manager.rs b/server/src/manager.rs index e7adb3704f205c574b75e320c15891d0e27fe478..c6463ba67b86d66190a5f4c3b80620464c91d6dd 100644 --- a/server/src/manager.rs +++ b/server/src/manager.rs @@ -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())); } diff --git a/server/src/module.rs b/server/src/module.rs index 5c81d79a3686d411dc17c0cd96f5d621f82a2f10..d52646386176e37d86e71f1e879011d5013e61bb 100644 --- a/server/src/module.rs +++ b/server/src/module.rs @@ -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())); }