|
|
|
|
@ -1,4 +1,5 @@ |
|
|
|
|
use bevy::prelude::*; |
|
|
|
|
use lightyear::prelude::*; |
|
|
|
|
|
|
|
|
|
use lightyear::prelude::input::native::ActionState; |
|
|
|
|
use serde::{Deserialize, Serialize}; |
|
|
|
|
@ -11,28 +12,29 @@ const MOVEMENT_SPEED: f32 = 5.0; |
|
|
|
|
#[require(Transform)] |
|
|
|
|
pub struct Player; |
|
|
|
|
|
|
|
|
|
pub fn server_movement( |
|
|
|
|
pub fn movement( |
|
|
|
|
time: Res<Time<Fixed>>, |
|
|
|
|
mut players: Query<(&mut Transform, &ActionState<Inputs>)>, |
|
|
|
|
mut players: Query< |
|
|
|
|
(&mut Transform, &ActionState<Inputs>), |
|
|
|
|
// Must be a `Player` which is either be `ControlledBy` a remote
|
|
|
|
|
// client (server-side) or its movement `Predicted` on the client.
|
|
|
|
|
(With<Player>, Or<(With<ControlledBy>, With<Predicted>)>), |
|
|
|
|
>, |
|
|
|
|
) { |
|
|
|
|
for (mut transform, inputs) in players.iter_mut() { |
|
|
|
|
shared_movement(&mut transform, *time, inputs); |
|
|
|
|
for (mut transform, input) in players.iter_mut() { |
|
|
|
|
let dt = time.delta_secs(); |
|
|
|
|
let mut translation = Vec3::ZERO; |
|
|
|
|
if input.movement != Vec2::ZERO { |
|
|
|
|
let movement = input.movement.clamp_length_max(1.0); |
|
|
|
|
translation.x += movement.x; |
|
|
|
|
translation.z += movement.y; |
|
|
|
|
} |
|
|
|
|
if input.up { |
|
|
|
|
translation.y += 1.0; |
|
|
|
|
} |
|
|
|
|
if input.down { |
|
|
|
|
translation.y -= 1.0; |
|
|
|
|
} |
|
|
|
|
transform.translation += translation * MOVEMENT_SPEED * dt; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn shared_movement(transform: &mut Transform, time: Time<Fixed>, input: &Inputs) { |
|
|
|
|
let dt = time.delta_secs(); |
|
|
|
|
let mut translation = Vec3::ZERO; |
|
|
|
|
if input.movement != Vec2::ZERO { |
|
|
|
|
let movement = input.movement.clamp_length_max(1.0); |
|
|
|
|
translation.x += movement.x; |
|
|
|
|
translation.z += movement.y; |
|
|
|
|
} |
|
|
|
|
if input.up { |
|
|
|
|
translation.y += 1.0; |
|
|
|
|
} |
|
|
|
|
if input.down { |
|
|
|
|
translation.y -= 1.0; |
|
|
|
|
} |
|
|
|
|
transform.translation += translation * MOVEMENT_SPEED * dt; |
|
|
|
|
} |
|
|
|
|
|