- Camera is added to player on spawn - Input is being processed server-sidemain
parent
04c63abee2
commit
90b255ed3c
8 changed files with 148 additions and 97 deletions
@ -0,0 +1,41 @@ |
|||||||
|
use bevy::prelude::*; |
||||||
|
use lightyear::prelude::input::client::*; |
||||||
|
use lightyear::prelude::input::native::*; |
||||||
|
|
||||||
|
use bevy::window::{CursorGrabMode, CursorOptions}; |
||||||
|
|
||||||
|
use crate::camera::CameraFreeLook; |
||||||
|
use common::network::Inputs; |
||||||
|
|
||||||
|
pub fn plugin(app: &mut App) { |
||||||
|
app.add_systems( |
||||||
|
FixedPreUpdate, |
||||||
|
buffer_input.in_set(InputSystems::WriteClientInputs), |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
fn buffer_input( |
||||||
|
key_input: Res<ButtonInput<KeyCode>>, |
||||||
|
mut action_state: Single<&mut ActionState<Inputs>, With<InputMarker<Inputs>>>, |
||||||
|
cursor: Single<&CursorOptions>, |
||||||
|
camera: Single<&CameraFreeLook>, |
||||||
|
) { |
||||||
|
action_state.0 = if cursor.grab_mode == CursorGrabMode::None { |
||||||
|
// If cursor is not grabbed, reset movement input.
|
||||||
|
Default::default() |
||||||
|
} else { |
||||||
|
Inputs { |
||||||
|
#[rustfmt::skip] |
||||||
|
movement: { |
||||||
|
let mut movement = Vec2::ZERO; |
||||||
|
if key_input.pressed(KeyCode::KeyD) { movement.x += 1.0; } |
||||||
|
if key_input.pressed(KeyCode::KeyA) { movement.x -= 1.0; } |
||||||
|
if key_input.pressed(KeyCode::KeyS) { movement.y += 1.0; } |
||||||
|
if key_input.pressed(KeyCode::KeyW) { movement.y -= 1.0; } |
||||||
|
Rot2::radians(-camera.yaw) * movement.clamp_length_max(1.0) |
||||||
|
}, |
||||||
|
up: key_input.pressed(KeyCode::Space), |
||||||
|
down: key_input.pressed(KeyCode::ShiftLeft), |
||||||
|
} |
||||||
|
}; |
||||||
|
} |
||||||
@ -1,11 +1,15 @@ |
|||||||
pub mod asset_loading; |
pub mod asset_loading; |
||||||
pub mod block; |
pub mod block; |
||||||
pub mod network; |
pub mod network; |
||||||
|
pub mod player; |
||||||
|
|
||||||
// This is mostly just re-exportings things for now, but in the future
|
// This is mostly just re-exporting everything for now, but in the future,
|
||||||
// we might want to limit what gets exposed by the `prelude` module.
|
// we might want to select exactly what's exposed by the `prelude` module.
|
||||||
pub mod prelude { |
pub mod prelude { |
||||||
pub use super::asset_loading::LoadResource; |
|
||||||
pub use super::block::*; |
|
||||||
pub use super::network; |
pub use super::network; |
||||||
|
|
||||||
|
pub use super::block::*; |
||||||
|
pub use super::player::*; |
||||||
|
|
||||||
|
pub use super::asset_loading::LoadResource; |
||||||
} |
} |
||||||
|
|||||||
@ -1,13 +1,32 @@ |
|||||||
use bevy::prelude::*; |
use bevy::prelude::*; |
||||||
use lightyear::prelude::*; |
use lightyear::prelude::*; |
||||||
|
|
||||||
|
use bevy::ecs::entity::MapEntities; |
||||||
|
use lightyear::input::native::plugin::InputPlugin; |
||||||
|
use serde::{Deserialize, Serialize}; |
||||||
|
|
||||||
use crate::block::Block; |
use crate::block::Block; |
||||||
|
use crate::player::Player; |
||||||
|
|
||||||
|
#[derive(Default, Deserialize, Serialize, Reflect, Clone, PartialEq, Debug)] |
||||||
|
pub struct Inputs { |
||||||
|
pub movement: Vec2, |
||||||
|
pub up: bool, |
||||||
|
pub down: bool, |
||||||
|
} |
||||||
|
|
||||||
|
impl MapEntities for Inputs { |
||||||
|
fn map_entities<E: EntityMapper>(&mut self, _entity_mapper: &mut E) {} |
||||||
|
} |
||||||
|
|
||||||
pub struct ProtocolPlugin; |
pub struct ProtocolPlugin; |
||||||
|
|
||||||
impl Plugin for ProtocolPlugin { |
impl Plugin for ProtocolPlugin { |
||||||
fn build(&self, app: &mut App) { |
fn build(&self, app: &mut App) { |
||||||
|
app.add_plugins(InputPlugin::<Inputs>::default()); |
||||||
|
|
||||||
app.register_component::<Transform>(); |
app.register_component::<Transform>(); |
||||||
|
app.register_component::<Player>(); |
||||||
app.register_component::<Block>(); |
app.register_component::<Block>(); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|||||||
@ -0,0 +1,38 @@ |
|||||||
|
use bevy::prelude::*; |
||||||
|
|
||||||
|
use lightyear::prelude::input::native::ActionState; |
||||||
|
use serde::{Deserialize, Serialize}; |
||||||
|
|
||||||
|
use crate::network::Inputs; |
||||||
|
|
||||||
|
const MOVEMENT_SPEED: f32 = 5.0; |
||||||
|
|
||||||
|
#[derive(Component, PartialEq, Deserialize, Serialize)] |
||||||
|
#[require(Transform)] |
||||||
|
pub struct Player; |
||||||
|
|
||||||
|
pub fn server_movement( |
||||||
|
time: Res<Time<Fixed>>, |
||||||
|
mut players: Query<(&mut Transform, &ActionState<Inputs>)>, |
||||||
|
) { |
||||||
|
for (mut transform, inputs) in players.iter_mut() { |
||||||
|
shared_movement(&mut transform, *time, inputs); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
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; |
||||||
|
} |
||||||
Loading…
Reference in new issue