From 02750384df3142bda62a7ce0feb08d115aa6af29 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Thu, 14 Mar 2024 14:24:43 +0100 Subject: feat: walking --- src/client.rs | 3 ++- src/protocol.rs | 3 ++- src/server.rs | 34 +++++++++++++++++++++++++++++----- src/shared.rs | 5 +++++ src/shared/imperative.rs | 9 +++++++++ 5 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 src/shared/imperative.rs diff --git a/src/client.rs b/src/client.rs index b36bba9..bac597e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,5 +1,6 @@ use crate::client::network::*; use crate::protocol::*; +use crate::shared::imperative::*; use crate::shared::*; use bevy::input::mouse::MouseButton; use bevy::prelude::*; @@ -75,7 +76,7 @@ fn buffer_input( if let Some(world_position) = camera.viewport_to_world_2d(camera_transform, cursor_position) { - client.add_input(Inputs::Teleport(world_position)); + client.add_input(Inputs::Imperative(Imperative::WalkTo(world_position))); } } } diff --git a/src/protocol.rs b/src/protocol.rs index a9e2086..86d1fa0 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -1,3 +1,4 @@ +use crate::shared::imperative::*; use crate::shared::*; use bevy::prelude::*; use lightyear::prelude::*; @@ -6,7 +7,7 @@ use serde::Serialize; #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] pub enum Inputs { - Teleport(Vec2), + Imperative(Imperative), None, } impl UserAction for Inputs {} diff --git a/src/server.rs b/src/server.rs index 3a7dd40..188aad1 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,5 +1,6 @@ use crate::protocol::*; use crate::server::network::*; +use crate::shared::imperative::*; use crate::shared::*; use bevy::prelude::*; use bevy::utils::HashMap; @@ -29,7 +30,8 @@ impl Plugin for ServerPlugin { transport: self.transport.clone(), }) .add_systems(Update, connections) - .add_systems(FixedUpdate, player_input); + .add_systems(FixedUpdate, player_input) + .add_systems(Update, imperative); } } @@ -63,16 +65,16 @@ fn connections( fn player_input( entity_map: Res, mut input_reader: EventReader>, - mut positions: Query<&mut PlayerPosition>, + mut imperatives: Query<&mut Imperative>, ) { for input in input_reader.read() { let client_id = input.context(); if let Some(input) = input.input() { if let Some(entity_id) = entity_map.0.get(client_id) { match input { - Inputs::Teleport(new_position) => { - if let Ok(mut position) = positions.get_mut(*entity_id) { - position.0 = *new_position; + Inputs::Imperative(new_imperative) => { + if let Ok(mut imperative) = imperatives.get_mut(*entity_id) { + *imperative = *new_imperative; } } _ => {} @@ -81,3 +83,25 @@ fn player_input( } } } + +const MOVEMENT_SPEED: f32 = 80.; + +fn imperative(mut players: Query<(&mut Imperative, &mut PlayerPosition)>, time: Res