From 8e102fc626dd77ec76b80425ef7f63e7be708406 Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Tue, 11 Apr 2023 19:09:50 -0500 Subject: [PATCH] added player (yay) does it work? idk, client broke --- client/src/lib.rs | 6 +++--- server/src/handler.rs | 6 ++++++ server/src/manager.rs | 4 +++- server/src/timer.rs | 16 +++++++++++++--- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index bb36c67995f2bfedc3a58c53e0bfc33fbee5458f..c5fee6cadc11f6f22980957532d25a0200232f10 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -6,7 +6,7 @@ use futures::StreamExt; use log::{debug, error, info, Level, trace, warn}; use wasm_bindgen::prelude::*; use ws_stream_wasm::{WsErr, WsMessage, WsMeta, WsStream}; -use starkingdoms_protocol::{Planet, State}; +use starkingdoms_protocol::{ProtocolPlanet, State}; use starkingdoms_protocol::PROTOCOL_VERSION; use starkingdoms_protocol::MessageS2C; use starkingdoms_protocol::MessageC2S; @@ -39,7 +39,7 @@ extern { #[derive(Debug)] pub struct Client { pub client_data: Option, - pub planets: Vec, + pub planets: Vec, pub x: f64, pub y: f64 } @@ -286,4 +286,4 @@ pub fn get_texture(texture_id: &str) -> Option { } else { None } -} \ No newline at end of file +} diff --git a/server/src/handler.rs b/server/src/handler.rs index 1ce4d54bc25bdf10ef98ff768ecd55f1fa7771d5..e8c7578e3852f3ad52ed022b10cfb6aa87dab5fb 100644 --- a/server/src/handler.rs +++ b/server/src/handler.rs @@ -32,6 +32,12 @@ pub async fn handle_client(mgr: ClientManager, data: Arc>, r from }).await?; } + ClientHandlerMessage::Position { x, y } => { + send!(client_tx, &MessageS2C::Position { + x, + y + }).await?; + } } } else { info!("channel closed, shutting down"); diff --git a/server/src/manager.rs b/server/src/manager.rs index eb877f75c5e80e15aa661d25fdfb643033374c57..f930dcdec39973da283ddbdc20af6f14dfcc6b7b 100644 --- a/server/src/manager.rs +++ b/server/src/manager.rs @@ -57,7 +57,9 @@ impl PhysicsData { } } +#[derive(Debug)] pub enum ClientHandlerMessage { Tick, - ChatMessage { from: String, message: String } + ChatMessage { from: String, message: String }, + Position { x: f64, y: f64 }, } diff --git a/server/src/timer.rs b/server/src/timer.rs index 7f40bd8a5c469d3e351bea5ceb1b5f3a43b5601f..c3bf81c418fdb2732021ddb11fa1c420a19a0237 100644 --- a/server/src/timer.rs +++ b/server/src/timer.rs @@ -4,7 +4,7 @@ use log::{error}; use rapier2d::prelude::{PhysicsPipeline}; use tokio::{time::sleep, sync::RwLock}; -use crate::{manager::{ClientHandlerMessage, ClientManager, PhysicsData}, SCALE}; +use crate::{manager::{ClientHandlerMessage, ClientManager, PhysicsData}, SCALE, send}; pub async fn timer_main(mgr: ClientManager, physics_data: Arc>) { let mut pipeline = PhysicsPipeline::new(); @@ -13,9 +13,19 @@ pub async fn timer_main(mgr: ClientManager, physics_data: Arc (), + Ok(_) => { + let player_handle = mgr.players.read().await.get(addr).unwrap().handle; + let data = physics_data.read().await; + let player_body = data.rigid_body_set.get(player_handle).unwrap(); + match client_thread.tx.send(ClientHandlerMessage::Position { x: player_body.translation().x as f64, y: player_body.translation().y as f64 }).await { + Ok(_) => (), + Err(e) => { + error!("unable to send position packet: {}", e); + } + }; + } Err(e) => { error!("unable to update a client thread: {}", e); }