use glam::Mat4; use shipyard::{Component, EntityId, Unique, AllStoragesView, UniqueView, NonSendSync, View, ViewMut, Get, IntoIter}; use hashbrown::HashMap; use uflow::SendMode; use std::net::SocketAddr; use kubi_shared::{ networking::{ client::{ClientIdMap, Client}, messages::{ClientToServerMessage, ServerToClientMessage, ClientToServerMessageType}, channels::Channel }, transform::Transform }; use crate::{ server::{ServerEvents, UdpServer}, util::check_message_auth }; #[derive(Component, Clone, Copy)] pub struct ClientAddress(pub SocketAddr); #[derive(Unique, Default)] pub struct ClientAddressMap(pub HashMap); impl ClientAddressMap { pub fn new() -> Self { Self::default() } } pub fn init_client_maps( storages: AllStoragesView ) { storages.add_unique(ClientIdMap::new()); storages.add_unique(ClientAddressMap::new()); } pub fn sync_client_positions( server: NonSendSync>, events: UniqueView, addr_map: UniqueView, clients: View, mut transforms: ViewMut, addrs: View, ) { for event in &events.0 { let Some(message) = check_message_auth ::<{ClientToServerMessageType::PositionChanged as u8}> (&server, event, &clients, &addr_map) else { continue }; let ClientToServerMessage::PositionChanged { position, velocity: _, direction } = message.message else { unreachable!() }; //log movement (annoying duh) log::debug!("dbg: player moved id: {} coords: {} quat: {}", message.client_id, position, direction); //Apply position to server-side client let mut trans = (&mut transforms).get(message.entity_id).unwrap(); trans.0 = Mat4::from_rotation_translation(direction, position); //Transmit the change to other players for (other_client, other_client_address) in (&clients, &addrs).iter() { if other_client.0 == message.client_id { continue } let Some(client) = server.0.client(&other_client_address.0) else { log::error!("Client with address not found"); continue }; client.borrow_mut().send( postcard::to_allocvec( &ServerToClientMessage::PlayerPositionChanged { client_id: message.client_id, position, direction } ).unwrap().into_boxed_slice(), Channel::Move as usize, SendMode::Reliable ); } } }