pass velocity to server move evt

This commit is contained in:
griffi-gh 2024-02-17 14:45:30 +01:00
parent f3a844bf55
commit 14e9cb32f7
2 changed files with 140 additions and 136 deletions

View file

@ -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<Transform, track::All>,
player: View<MainPlayer>,
mut entities: EntitiesViewMut,
mut events: ViewMut<EventComponent>,
mut actions: ViewMut<PlayerActionEvent>,
) {
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<Transform, track::All>,
player: View<MainPlayer>,
actors: View<ClPhysicsActor>,
mut entities: EntitiesViewMut,
mut events: ViewMut<EventComponent>,
mut actions: ViewMut<PlayerActionEvent>,
) {
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 })
);
}

View file

@ -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<PlayerActionEvent>,
mut client: UniqueViewMut<UdpClient>,
) {
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<Transform>,
network_events: View<NetworkEvent>,
id_map: UniqueView<ClientIdMap>
) {
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<ServerToClientMessage> = storages.borrow::<View<NetworkEvent>>().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<PlayerActionEvent>,
mut client: UniqueViewMut<UdpClient>,
) {
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<Transform>,
network_events: View<NetworkEvent>,
id_map: UniqueView<ClientIdMap>
) {
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<ServerToClientMessage> = storages.borrow::<View<NetworkEvent>>().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);
}
}