From a232f72c0eb31fd489b8b1c713d21241fb6ee56d Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Thu, 22 Jun 2023 00:12:55 -0500 Subject: [PATCH] one time i heard that the base for module manipulation is good --- client/src/index.ts | 73 +++++++++++++++++++++++++++++++--- client/src/protocol/module.ts | 30 ++++++++++---- protocol/src/pbuf/module.proto | 3 +- server/src/entity.rs | 9 +++++ server/src/handler.rs | 14 ++++++- server/src/module.rs | 11 +++++ server/src/timer.rs | 15 ++++--- 7 files changed, 134 insertions(+), 21 deletions(-) diff --git a/client/src/index.ts b/client/src/index.ts index 6a9894e78aa7add4869b365a05de0554c3b609ac..46b34b6a569de730d43e171102d9f7fab8790928 100644 --- a/client/src/index.ts +++ b/client/src/index.ts @@ -7,7 +7,7 @@ import { MessageC2SAuthenticateAndBeamOut, MessageC2SAuthenticateAndBeamOut_packetInfo, MessageC2SInput, - MessageC2SInput_packetInfo, MessageC2SMouseInput, MessageC2SMouseInput_packetInfo + MessageC2SInput_packetInfo, MessageC2SModuleGrabBegin, MessageC2SModuleGrabBegin_packetInfo, MessageC2SModuleGrabEnd, MessageC2SModuleGrabEnd_packetInfo, MessageC2SMouseInput, MessageC2SMouseInput_packetInfo } from "./protocol/message_c2s"; import {encode} from "./serde"; import {InputType} from "./protocol/input"; @@ -30,7 +30,9 @@ export interface GlobalData { x_vel: number, y_vel: number can_beam_out: boolean, - direction_radians: number + direction_radians: number, + mouse_x: number, + mouse_y: number, } export interface Keys { @@ -60,7 +62,9 @@ export const global: GlobalData = { x_vel: 0, y_vel: 0, can_beam_out: false, - direction_radians: 0 + direction_radians: 0, + mouse_x: 0, + mouse_y: 0, } async function client_main(server: string, username: string, texture_quality: string) { @@ -128,8 +132,10 @@ async function client_main(server: string, username: string, texture_quality: st // convert screenspace to worldspace if (global.me !== null) { - let worldX = screenspaceX + global.me?.x; - let worldY = screenspaceY + global.me?.y; + //let worldX = screenspaceX + global.me?.x; + //let worldY = screenspaceY + global.me?.y; + let worldX = e.clientX - window.innerWidth / 2 + global.me?.x; + let worldY = e.clientY - window.innerHeight / 2 + global.me?.y; let button: InputType; if (e.button == 0) { @@ -140,6 +146,25 @@ async function client_main(server: string, username: string, texture_quality: st button = InputType.Right; } + for (let i = 0; i < global.modules.length; i++) { + let relativeX = global.modules[i].x - worldX; + let relativeY = global.modules[i].y - worldY; + 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 msg = MessageC2SModuleGrabBegin.encode({ + moduleId: global.modules[i].id, + worldposX: worldX, + worldposY: worldY, + }).finish(); + global.modules[i].flags |= 2; + global.client?.socket.send(encode(MessageC2SModuleGrabBegin_packetInfo.type, msg)) + } + } + } + let msg = MessageC2SMouseInput.encode({ worldposX: worldX, worldposY: worldY, @@ -178,11 +203,30 @@ async function client_main(server: string, username: string, texture_quality: st released: true, button: button! }).finish(); + + for (let i = 0; i < global.modules.length; i++) { + if((global.modules[i].flags & 2) != 0) { + global.modules[i].flags &= ~2; + let msg = MessageC2SModuleGrabEnd.encode({ + moduleId: global.modules[i].id, + parentId: 4294967295, + slot: 5, + }).finish(); + global.client?.socket.send(encode(MessageC2SModuleGrabEnd_packetInfo.type, msg)) + } + } global.client?.socket.send(encode(MessageC2SMouseInput_packetInfo.type, msg)) } } + document.onmousemove = (e) => { + let canvasLeft = canvas.offsetLeft + canvas.clientLeft; + let canvasTop = canvas.offsetTop + canvas.clientTop; + global.mouse_x = e.pageX - canvasLeft; + global.mouse_y = e.pageY - canvasTop; + } + document.onkeydown = (e) => { if (e.code == "ArrowLeft" || e.code == "KeyA") { // arrow-left global.keys.left = true; @@ -321,6 +365,25 @@ async function client_main(server: string, username: string, texture_quality: st -25, 50, 50); global.context.restore(); + + if ((module.flags & 2) != 0) { + global.context.save(); + global.context.translate(global.mouse_x - window.innerWidth/2, + global.mouse_y - window.innerHeight/2); + + global.context.rotate(module.rotation); + + global.context.globalAlpha = 0.6; + global.context.drawImage(global.spritesheet_img!, + tex.frame.x, + tex.frame.y, + tex.frame.w, + tex.frame.h, + -25, + -25, 50, 50); + global.context.globalAlpha = 1.0; + global.context.restore(); + } } } diff --git a/client/src/protocol/module.ts b/client/src/protocol/module.ts index 04a85bb4a35e1148be3cd2662c3dca6034410d0e..7adeeff00075065bff2487225bafa491254d53ba 100644 --- a/client/src/protocol/module.ts +++ b/client/src/protocol/module.ts @@ -53,7 +53,8 @@ export interface Module { rotation: number; x: number; y: number; - isOn: boolean; + id: number; + flags: number; } export interface AttachedModule { @@ -71,7 +72,7 @@ export interface Attachment { } function createBaseModule(): Module { - return { moduleType: 0, rotation: 0, x: 0, y: 0, isOn: false }; + return { moduleType: 0, rotation: 0, x: 0, y: 0, id: 0, flags: 0 }; } export const Module = { @@ -88,8 +89,11 @@ export const Module = { if (message.y !== 0) { writer.uint32(33).double(message.y); } - if (message.isOn === true) { - writer.uint32(40).bool(message.isOn); + if (message.id !== 0) { + writer.uint32(40).uint32(message.id); + } + if (message.flags !== 0) { + writer.uint32(48).uint32(message.flags); } return writer; }, @@ -134,7 +138,14 @@ export const Module = { break; } - message.isOn = reader.bool(); + message.id = reader.uint32(); + continue; + case 6: + if (tag != 48) { + break; + } + + message.flags = reader.uint32(); continue; } if ((tag & 7) == 4 || tag == 0) { @@ -151,7 +162,8 @@ export const Module = { rotation: isSet(object.rotation) ? Number(object.rotation) : 0, x: isSet(object.x) ? Number(object.x) : 0, y: isSet(object.y) ? Number(object.y) : 0, - isOn: isSet(object.isOn) ? Boolean(object.isOn) : false, + id: isSet(object.id) ? Number(object.id) : 0, + flags: isSet(object.flags) ? Number(object.flags) : 0, }; }, @@ -161,7 +173,8 @@ export const Module = { message.rotation !== undefined && (obj.rotation = message.rotation); message.x !== undefined && (obj.x = message.x); message.y !== undefined && (obj.y = message.y); - message.isOn !== undefined && (obj.isOn = message.isOn); + message.id !== undefined && (obj.id = Math.round(message.id)); + message.flags !== undefined && (obj.flags = Math.round(message.flags)); return obj; }, @@ -175,7 +188,8 @@ export const Module = { message.rotation = object.rotation ?? 0; message.x = object.x ?? 0; message.y = object.y ?? 0; - message.isOn = object.isOn ?? false; + message.id = object.id ?? 0; + message.flags = object.flags ?? 0; return message; }, }; diff --git a/protocol/src/pbuf/module.proto b/protocol/src/pbuf/module.proto index c5da5a8c6baa18bea6ca1d210cacd09c2e95f99d..7ab54de699a13710c587cba553103bae8c9b0e32 100644 --- a/protocol/src/pbuf/module.proto +++ b/protocol/src/pbuf/module.proto @@ -6,7 +6,8 @@ message Module { double rotation = 2; double x = 3; double y = 4; - bool is_on = 5; + uint32 id = 5; + uint32 flags = 6; } message AttachedModule { ModuleType module_type = 1; diff --git a/server/src/entity.rs b/server/src/entity.rs index 6de390c55c0ce45fedd8ddb5f78428833d240a33..917c6b0323d4bc98d9aba2e29d38830b5f21145c 100644 --- a/server/src/entity.rs +++ b/server/src/entity.rs @@ -101,6 +101,15 @@ impl EntityHandler { } modules } + pub fn get_modules_id(&self) -> Vec<(EntityId, Module)> { + let mut modules = Vec::new(); + for (id, entity) in self.entities.clone().into_iter() { + if let Entity::Module(module) = entity { + modules.push((id as u32, module.clone())); + } + } + modules + } pub fn get_module_count(&self) -> u32 { let mut module_count = 0; for entity in self.entities.values() { diff --git a/server/src/handler.rs b/server/src/handler.rs index e5a6a5fd79c248c531117ae5c85511f67fcae42b..81907a39f9ce8d5c6d997c6c420e10ccd5be4a8e 100644 --- a/server/src/handler.rs +++ b/server/src/handler.rs @@ -3,7 +3,7 @@ use crate::entity::{get_entity_id, Entity, EntityHandler}; use crate::manager::{ ClientHandlerMessage, ClientManager, Player, PhysicsData, }; -use crate::module::{AttachedModule, ModuleTemplate}; +use crate::module::{AttachedModule, ModuleTemplate, Module}; use crate::{recv, send, SCALE}; use async_std::net::TcpStream; use async_std::{channel::Receiver, sync::RwLock}; @@ -434,6 +434,18 @@ pub async fn handle_client( MessageC2S::MouseInput(p) => { debug!("[{}] player input: {:?}", remote_addr, p); } + MessageC2S::ModuleGrabBegin(p) => { + if let Entity::Module(module) = entities.write().await.entities.get_mut(&p.module_id).unwrap() { + module.flags |= 2; + debug!("[{}] grab begin: {:?}, flags: {}", remote_addr, p, module.flags); + } + } + MessageC2S::ModuleGrabEnd(p) => { + if let Entity::Module(module) = entities.write().await.entities.get_mut(&p.module_id).unwrap() { + module.flags &= !2; + debug!("[{}] grab end: {:?}", remote_addr, p); + } + } }, } } diff --git a/server/src/module.rs b/server/src/module.rs index e4fc292c8357489e606ac5631f12d58bfdd2fa90..afeaf8c7f22b4a670cb013c8a20c129193a11988 100644 --- a/server/src/module.rs +++ b/server/src/module.rs @@ -12,6 +12,7 @@ pub struct Module { pub handle: RigidBodyHandle, pub module_type: ModuleType, pub lifetime: f64, + pub flags: u32, } #[derive(Clone)] @@ -238,8 +239,18 @@ impl AttachedModule { handle: self.handle, module_type: self.module_type, lifetime: 10., + flags: 0, } } + // TODO: this one too + pub fn to_module_id(&self, entities: &EntityHandler) -> (EntityId, Module) { + (entities.get_id_from_attached(self.clone()).unwrap(), Module { + handle: self.handle, + module_type: self.module_type, + lifetime: 10., + flags: 0, + }) + } pub fn search_modules(&self, entities: &EntityHandler) -> Vec { let mut modules = vec![self.clone()]; diff --git a/server/src/timer.rs b/server/src/timer.rs index 55980b3da00bcff2a21f997ffe5eeda2bd9e513d..ecea7a59b999dd3ec4f883b324cddf7458c63225 100644 --- a/server/src/timer.rs +++ b/server/src/timer.rs @@ -128,6 +128,7 @@ pub async fn timer_main( handle: module_handler, module_type: ModuleType::Cargo, lifetime: 0.0, + flags: 0, }; entities .write() @@ -304,21 +305,22 @@ pub async fn timer_main( warn!("unable to send position packet: {}", e); } }; - let mut modules = entities.read().await.get_modules(); + let mut modules = entities.read().await.get_modules_id(); let attached_modules = entities.read().await.get_all_attached(); + let entities = entities.read().await; let attached_handles: Vec = attached_modules.iter().map(|m| m.handle).collect(); modules.append( &mut attached_modules .iter() .map(|m| { - let module = m.to_module(); + let module = m.to_module_id(&entities); //info!("{:?}", module); module }) .collect(), ); - modules.iter().for_each(|module| { + modules.iter().for_each(|(id, module)| { if attached_handles.contains(&module.handle) { /*info!( "{:?}", @@ -332,13 +334,15 @@ pub async fn timer_main( }); let protocol_modules: Vec = modules .iter() - .map(|module| { + .map(|(id, module)| { let body = physics_data.rigid_body_set.get(module.handle).unwrap(); return starkingdoms_protocol::module::Module { module_type: module.module_type.into(), rotation: body.rotation().angle(), x: body.translation().x * SCALE, y: body.translation().y * SCALE, + id: *id, + flags: module.flags, special_fields: Default::default(), }; }) @@ -356,8 +360,7 @@ pub async fn timer_main( } }; - let world = entities.read().await; - let planet_data = world.to_protocol(); + let planet_data = entities.to_protocol(); match client_thread.tx.send(planet_data).await { Ok(_) => (), Err(e) => {