From 14e9cb32f7331a795ad36c17b1c90b2a924c253d Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sat, 17 Feb 2024 14:45:30 +0100 Subject: [PATCH] pass velocity to server move evt --- kubi/src/events/player_actions.rs | 74 +++++------ kubi/src/networking/player.rs | 202 +++++++++++++++--------------- 2 files changed, 140 insertions(+), 136 deletions(-) diff --git a/kubi/src/events/player_actions.rs b/kubi/src/events/player_actions.rs index 564b6aa..6946962 100644 --- a/kubi/src/events/player_actions.rs +++ b/kubi/src/events/player_actions.rs @@ -1,35 +1,39 @@ -use shipyard::{Component, View, ViewMut, EntitiesViewMut, IntoIter, track}; -use glam::{IVec3, Quat, Vec3}; -use kubi_shared::block::Block; -use crate::{ - player::MainPlayer, - transform::Transform -}; -use super::EventComponent; - -#[derive(Component, Clone, Copy, Debug)] -pub enum PlayerActionEvent { - PositionChanged { - position: Vec3, - direction: Quat - }, - UpdatedBlock { - position: IVec3, - block: Block, - }, -} - -pub fn generate_move_events( - transforms: View, - player: View, - mut entities: EntitiesViewMut, - mut events: ViewMut, - mut actions: ViewMut, -) { - let Some((_, transform)) = (&player, transforms.inserted_or_modified()).iter().next() else { return }; - let (_, direction, position) = transform.0.to_scale_rotation_translation(); - entities.add_entity( - (&mut events, &mut actions), - (EventComponent, PlayerActionEvent::PositionChanged { position, direction }) - ); -} +use shipyard::{Component, View, ViewMut, EntitiesViewMut, IntoIter, track}; +use glam::{IVec3, Quat, Vec3}; +use kubi_shared::block::Block; +use crate::{ + client_physics::ClPhysicsActor, player::MainPlayer, transform::Transform +}; +use super::EventComponent; + +#[derive(Component, Clone, Copy, Debug)] +pub enum PlayerActionEvent { + PositionChanged { + position: Vec3, + //XXX: should this even be here? + velocity: Vec3, + direction: Quat + }, + UpdatedBlock { + position: IVec3, + block: Block, + }, +} + +pub fn generate_move_events( + transforms: View, + player: View, + actors: View, + mut entities: EntitiesViewMut, + mut events: ViewMut, + mut actions: ViewMut, +) { + let Some((_, transform, actor)) = (&player, transforms.inserted_or_modified(), &actors).iter().next() else { return }; + let (_, direction, position) = transform.0.to_scale_rotation_translation(); + //HACK: if the actor is disabled, the velocity is irrelevant, so we just set it to zero. + let velocity = if actor.disable { Vec3::ZERO } else { actor.velocity }; + entities.add_entity( + (&mut events, &mut actions), + (EventComponent, PlayerActionEvent::PositionChanged { position, velocity, direction }) + ); +} diff --git a/kubi/src/networking/player.rs b/kubi/src/networking/player.rs index 8b0b470..a70f254 100644 --- a/kubi/src/networking/player.rs +++ b/kubi/src/networking/player.rs @@ -1,101 +1,101 @@ -use glam::{Vec3, Mat4}; -use shipyard::{UniqueViewMut, View, IntoIter, AllStoragesView, AllStoragesViewMut, UniqueView, ViewMut, Get}; -use uflow::{SendMode, client::Event as ClientEvent}; -use kubi_shared::{ - transform::Transform, - networking::{ - messages::{ClientToServerMessage, ServerToClientMessage, ServerToClientMessageType}, - channels::Channel, - client::ClientIdMap, - }, -}; -use crate::{ - events::player_actions::PlayerActionEvent, - player::spawn_remote_player_multiplayer, -}; -use super::{UdpClient, NetworkEvent}; - -pub fn init_client_map( - storages: AllStoragesView, -) { - storages.add_unique(ClientIdMap::new()); -} - -pub fn send_player_movement_events( - actions: View, - mut client: UniqueViewMut, -) { - for event in actions.iter() { - let PlayerActionEvent::PositionChanged { position, direction } = event else { - continue - }; - client.0.send( - postcard::to_allocvec(&ClientToServerMessage::PositionChanged { - position: *position, - velocity: Vec3::ZERO, - direction: *direction - }).unwrap().into_boxed_slice(), - Channel::Move as usize, - SendMode::TimeSensitive - ); - } -} - -pub fn receive_player_movement_events( - mut transforms: ViewMut, - network_events: View, - id_map: UniqueView -) { - for event in network_events.iter() { - let ClientEvent::Receive(data) = &event.0 else { - continue - }; - - if !event.is_message_of_type::<{ServerToClientMessageType::PlayerPositionChanged as u8}>() { - continue - } - - let Ok(parsed_message) = postcard::from_bytes(data) else { - log::error!("Malformed message"); - continue - }; - - let ServerToClientMessage::PlayerPositionChanged { - client_id, position, direction - } = parsed_message else { unreachable!() }; - - let Some(&ent_id) = id_map.0.get(&client_id) else { - log::error!("Not in client-id map"); - continue - }; - - let mut transform = (&mut transforms).get(ent_id) - .expect("invalid player entity id"); - - transform.0 = Mat4::from_rotation_translation(direction, position); - } -} - -pub fn receive_player_connect_events( - mut storages: AllStoragesViewMut, -) { - let messages: Vec = storages.borrow::>().unwrap().iter().filter_map(|event| { - let ClientEvent::Receive(data) = &event.0 else { - return None - }; - if !event.is_message_of_type::<{ServerToClientMessageType::PlayerConnected as u8}>() { - return None - }; - let Ok(parsed_message) = postcard::from_bytes(data) else { - log::error!("Malformed message"); - return None - }; - Some(parsed_message) - }).collect(); - - for message in messages { - let ServerToClientMessage::PlayerConnected { init } = message else { unreachable!() }; - log::info!("player connected: {} (id {})", init.username, init.client_id); - spawn_remote_player_multiplayer(&mut storages, init); - } -} +use glam::{Vec3, Mat4}; +use shipyard::{UniqueViewMut, View, IntoIter, AllStoragesView, AllStoragesViewMut, UniqueView, ViewMut, Get}; +use uflow::{SendMode, client::Event as ClientEvent}; +use kubi_shared::{ + transform::Transform, + networking::{ + messages::{ClientToServerMessage, ServerToClientMessage, ServerToClientMessageType}, + channels::Channel, + client::ClientIdMap, + }, +}; +use crate::{ + events::player_actions::PlayerActionEvent, + player::spawn_remote_player_multiplayer, +}; +use super::{UdpClient, NetworkEvent}; + +pub fn init_client_map( + storages: AllStoragesView, +) { + storages.add_unique(ClientIdMap::new()); +} + +pub fn send_player_movement_events( + actions: View, + mut client: UniqueViewMut, +) { + for event in actions.iter() { + let PlayerActionEvent::PositionChanged { position, velocity, direction } = event else { + continue + }; + client.0.send( + postcard::to_allocvec(&ClientToServerMessage::PositionChanged { + position: *position, + velocity: *velocity, + direction: *direction + }).unwrap().into_boxed_slice(), + Channel::Move as usize, + SendMode::TimeSensitive + ); + } +} + +pub fn receive_player_movement_events( + mut transforms: ViewMut, + network_events: View, + id_map: UniqueView +) { + for event in network_events.iter() { + let ClientEvent::Receive(data) = &event.0 else { + continue + }; + + if !event.is_message_of_type::<{ServerToClientMessageType::PlayerPositionChanged as u8}>() { + continue + } + + let Ok(parsed_message) = postcard::from_bytes(data) else { + log::error!("Malformed message"); + continue + }; + + let ServerToClientMessage::PlayerPositionChanged { + client_id, position, direction + } = parsed_message else { unreachable!() }; + + let Some(&ent_id) = id_map.0.get(&client_id) else { + log::error!("Not in client-id map"); + continue + }; + + let mut transform = (&mut transforms).get(ent_id) + .expect("invalid player entity id"); + + transform.0 = Mat4::from_rotation_translation(direction, position); + } +} + +pub fn receive_player_connect_events( + mut storages: AllStoragesViewMut, +) { + let messages: Vec = storages.borrow::>().unwrap().iter().filter_map(|event| { + let ClientEvent::Receive(data) = &event.0 else { + return None + }; + if !event.is_message_of_type::<{ServerToClientMessageType::PlayerConnected as u8}>() { + return None + }; + let Ok(parsed_message) = postcard::from_bytes(data) else { + log::error!("Malformed message"); + return None + }; + Some(parsed_message) + }).collect(); + + for message in messages { + let ServerToClientMessage::PlayerConnected { init } = message else { unreachable!() }; + log::info!("player connected: {} (id {})", init.username, init.client_id); + spawn_remote_player_multiplayer(&mut storages, init); + } +}