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

@ -2,8 +2,7 @@ use shipyard::{Component, View, ViewMut, EntitiesViewMut, IntoIter, track};
use glam::{IVec3, Quat, Vec3};
use kubi_shared::block::Block;
use crate::{
player::MainPlayer,
transform::Transform
client_physics::ClPhysicsActor, player::MainPlayer, transform::Transform
};
use super::EventComponent;
@ -11,6 +10,8 @@ use super::EventComponent;
pub enum PlayerActionEvent {
PositionChanged {
position: Vec3,
//XXX: should this even be here?
velocity: Vec3,
direction: Quat
},
UpdatedBlock {
@ -22,14 +23,17 @@ pub enum PlayerActionEvent {
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)) = (&player, transforms.inserted_or_modified()).iter().next() else { return };
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, direction })
(EventComponent, PlayerActionEvent::PositionChanged { position, velocity, direction })
);
}

View file

@ -26,13 +26,13 @@ pub fn send_player_movement_events(
mut client: UniqueViewMut<UdpClient>,
) {
for event in actions.iter() {
let PlayerActionEvent::PositionChanged { position, direction } = event else {
let PlayerActionEvent::PositionChanged { position, velocity, direction } = event else {
continue
};
client.0.send(
postcard::to_allocvec(&ClientToServerMessage::PositionChanged {
position: *position,
velocity: Vec3::ZERO,
velocity: *velocity,
direction: *direction
}).unwrap().into_boxed_slice(),
Channel::Move as usize,