mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-12-26 05:38:20 -06:00
pass velocity to server move evt
This commit is contained in:
parent
bc3db2c95e
commit
81decc6202
|
@ -1,35 +1,39 @@
|
||||||
use shipyard::{Component, View, ViewMut, EntitiesViewMut, IntoIter, track};
|
use shipyard::{Component, View, ViewMut, EntitiesViewMut, IntoIter, track};
|
||||||
use glam::{IVec3, Quat, Vec3};
|
use glam::{IVec3, Quat, Vec3};
|
||||||
use kubi_shared::block::Block;
|
use kubi_shared::block::Block;
|
||||||
use crate::{
|
use crate::{
|
||||||
player::MainPlayer,
|
client_physics::ClPhysicsActor, player::MainPlayer, transform::Transform
|
||||||
transform::Transform
|
};
|
||||||
};
|
use super::EventComponent;
|
||||||
use super::EventComponent;
|
|
||||||
|
#[derive(Component, Clone, Copy, Debug)]
|
||||||
#[derive(Component, Clone, Copy, Debug)]
|
pub enum PlayerActionEvent {
|
||||||
pub enum PlayerActionEvent {
|
PositionChanged {
|
||||||
PositionChanged {
|
position: Vec3,
|
||||||
position: Vec3,
|
//XXX: should this even be here?
|
||||||
direction: Quat
|
velocity: Vec3,
|
||||||
},
|
direction: Quat
|
||||||
UpdatedBlock {
|
},
|
||||||
position: IVec3,
|
UpdatedBlock {
|
||||||
block: Block,
|
position: IVec3,
|
||||||
},
|
block: Block,
|
||||||
}
|
},
|
||||||
|
}
|
||||||
pub fn generate_move_events(
|
|
||||||
transforms: View<Transform, track::All>,
|
pub fn generate_move_events(
|
||||||
player: View<MainPlayer>,
|
transforms: View<Transform, track::All>,
|
||||||
mut entities: EntitiesViewMut,
|
player: View<MainPlayer>,
|
||||||
mut events: ViewMut<EventComponent>,
|
actors: View<ClPhysicsActor>,
|
||||||
mut actions: ViewMut<PlayerActionEvent>,
|
mut entities: EntitiesViewMut,
|
||||||
) {
|
mut events: ViewMut<EventComponent>,
|
||||||
let Some((_, transform)) = (&player, transforms.inserted_or_modified()).iter().next() else { return };
|
mut actions: ViewMut<PlayerActionEvent>,
|
||||||
let (_, direction, position) = transform.0.to_scale_rotation_translation();
|
) {
|
||||||
entities.add_entity(
|
let Some((_, transform, actor)) = (&player, transforms.inserted_or_modified(), &actors).iter().next() else { return };
|
||||||
(&mut events, &mut actions),
|
let (_, direction, position) = transform.0.to_scale_rotation_translation();
|
||||||
(EventComponent, PlayerActionEvent::PositionChanged { position, direction })
|
//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 })
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
@ -1,101 +1,101 @@
|
||||||
use glam::{Vec3, Mat4};
|
use glam::{Vec3, Mat4};
|
||||||
use shipyard::{UniqueViewMut, View, IntoIter, AllStoragesView, AllStoragesViewMut, UniqueView, ViewMut, Get};
|
use shipyard::{UniqueViewMut, View, IntoIter, AllStoragesView, AllStoragesViewMut, UniqueView, ViewMut, Get};
|
||||||
use uflow::{SendMode, client::Event as ClientEvent};
|
use uflow::{SendMode, client::Event as ClientEvent};
|
||||||
use kubi_shared::{
|
use kubi_shared::{
|
||||||
transform::Transform,
|
transform::Transform,
|
||||||
networking::{
|
networking::{
|
||||||
messages::{ClientToServerMessage, ServerToClientMessage, ServerToClientMessageType},
|
messages::{ClientToServerMessage, ServerToClientMessage, ServerToClientMessageType},
|
||||||
channels::Channel,
|
channels::Channel,
|
||||||
client::ClientIdMap,
|
client::ClientIdMap,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
events::player_actions::PlayerActionEvent,
|
events::player_actions::PlayerActionEvent,
|
||||||
player::spawn_remote_player_multiplayer,
|
player::spawn_remote_player_multiplayer,
|
||||||
};
|
};
|
||||||
use super::{UdpClient, NetworkEvent};
|
use super::{UdpClient, NetworkEvent};
|
||||||
|
|
||||||
pub fn init_client_map(
|
pub fn init_client_map(
|
||||||
storages: AllStoragesView,
|
storages: AllStoragesView,
|
||||||
) {
|
) {
|
||||||
storages.add_unique(ClientIdMap::new());
|
storages.add_unique(ClientIdMap::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_player_movement_events(
|
pub fn send_player_movement_events(
|
||||||
actions: View<PlayerActionEvent>,
|
actions: View<PlayerActionEvent>,
|
||||||
mut client: UniqueViewMut<UdpClient>,
|
mut client: UniqueViewMut<UdpClient>,
|
||||||
) {
|
) {
|
||||||
for event in actions.iter() {
|
for event in actions.iter() {
|
||||||
let PlayerActionEvent::PositionChanged { position, direction } = event else {
|
let PlayerActionEvent::PositionChanged { position, velocity, direction } = event else {
|
||||||
continue
|
continue
|
||||||
};
|
};
|
||||||
client.0.send(
|
client.0.send(
|
||||||
postcard::to_allocvec(&ClientToServerMessage::PositionChanged {
|
postcard::to_allocvec(&ClientToServerMessage::PositionChanged {
|
||||||
position: *position,
|
position: *position,
|
||||||
velocity: Vec3::ZERO,
|
velocity: *velocity,
|
||||||
direction: *direction
|
direction: *direction
|
||||||
}).unwrap().into_boxed_slice(),
|
}).unwrap().into_boxed_slice(),
|
||||||
Channel::Move as usize,
|
Channel::Move as usize,
|
||||||
SendMode::TimeSensitive
|
SendMode::TimeSensitive
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn receive_player_movement_events(
|
pub fn receive_player_movement_events(
|
||||||
mut transforms: ViewMut<Transform>,
|
mut transforms: ViewMut<Transform>,
|
||||||
network_events: View<NetworkEvent>,
|
network_events: View<NetworkEvent>,
|
||||||
id_map: UniqueView<ClientIdMap>
|
id_map: UniqueView<ClientIdMap>
|
||||||
) {
|
) {
|
||||||
for event in network_events.iter() {
|
for event in network_events.iter() {
|
||||||
let ClientEvent::Receive(data) = &event.0 else {
|
let ClientEvent::Receive(data) = &event.0 else {
|
||||||
continue
|
continue
|
||||||
};
|
};
|
||||||
|
|
||||||
if !event.is_message_of_type::<{ServerToClientMessageType::PlayerPositionChanged as u8}>() {
|
if !event.is_message_of_type::<{ServerToClientMessageType::PlayerPositionChanged as u8}>() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
let Ok(parsed_message) = postcard::from_bytes(data) else {
|
let Ok(parsed_message) = postcard::from_bytes(data) else {
|
||||||
log::error!("Malformed message");
|
log::error!("Malformed message");
|
||||||
continue
|
continue
|
||||||
};
|
};
|
||||||
|
|
||||||
let ServerToClientMessage::PlayerPositionChanged {
|
let ServerToClientMessage::PlayerPositionChanged {
|
||||||
client_id, position, direction
|
client_id, position, direction
|
||||||
} = parsed_message else { unreachable!() };
|
} = parsed_message else { unreachable!() };
|
||||||
|
|
||||||
let Some(&ent_id) = id_map.0.get(&client_id) else {
|
let Some(&ent_id) = id_map.0.get(&client_id) else {
|
||||||
log::error!("Not in client-id map");
|
log::error!("Not in client-id map");
|
||||||
continue
|
continue
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut transform = (&mut transforms).get(ent_id)
|
let mut transform = (&mut transforms).get(ent_id)
|
||||||
.expect("invalid player entity id");
|
.expect("invalid player entity id");
|
||||||
|
|
||||||
transform.0 = Mat4::from_rotation_translation(direction, position);
|
transform.0 = Mat4::from_rotation_translation(direction, position);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn receive_player_connect_events(
|
pub fn receive_player_connect_events(
|
||||||
mut storages: AllStoragesViewMut,
|
mut storages: AllStoragesViewMut,
|
||||||
) {
|
) {
|
||||||
let messages: Vec<ServerToClientMessage> = storages.borrow::<View<NetworkEvent>>().unwrap().iter().filter_map(|event| {
|
let messages: Vec<ServerToClientMessage> = storages.borrow::<View<NetworkEvent>>().unwrap().iter().filter_map(|event| {
|
||||||
let ClientEvent::Receive(data) = &event.0 else {
|
let ClientEvent::Receive(data) = &event.0 else {
|
||||||
return None
|
return None
|
||||||
};
|
};
|
||||||
if !event.is_message_of_type::<{ServerToClientMessageType::PlayerConnected as u8}>() {
|
if !event.is_message_of_type::<{ServerToClientMessageType::PlayerConnected as u8}>() {
|
||||||
return None
|
return None
|
||||||
};
|
};
|
||||||
let Ok(parsed_message) = postcard::from_bytes(data) else {
|
let Ok(parsed_message) = postcard::from_bytes(data) else {
|
||||||
log::error!("Malformed message");
|
log::error!("Malformed message");
|
||||||
return None
|
return None
|
||||||
};
|
};
|
||||||
Some(parsed_message)
|
Some(parsed_message)
|
||||||
}).collect();
|
}).collect();
|
||||||
|
|
||||||
for message in messages {
|
for message in messages {
|
||||||
let ServerToClientMessage::PlayerConnected { init } = message else { unreachable!() };
|
let ServerToClientMessage::PlayerConnected { init } = message else { unreachable!() };
|
||||||
log::info!("player connected: {} (id {})", init.username, init.client_id);
|
log::info!("player connected: {} (id {})", init.username, init.client_id);
|
||||||
spawn_remote_player_multiplayer(&mut storages, init);
|
spawn_remote_player_multiplayer(&mut storages, init);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue