diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client.rs | 3 | ||||
-rw-r--r-- | src/protocol.rs | 3 | ||||
-rw-r--r-- | src/server.rs | 34 | ||||
-rw-r--r-- | src/shared.rs | 5 | ||||
-rw-r--r-- | src/shared/imperative.rs | 9 |
5 files changed, 47 insertions, 7 deletions
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<EntityMap>, mut input_reader: EventReader<server::InputEvent<Inputs>>, - 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<Time>) { + for (mut imperative, mut position) in players.iter_mut() { + match *imperative { + Imperative::Idle => {} + Imperative::WalkTo(target) => { + let distance = (target - position.0).length(); + let direction = (target - position.0).normalize_or_zero(); + let new_position = position.0 + + f32::min(MOVEMENT_SPEED * time.delta().as_secs_f32(), distance) * direction; + if position.0.distance(new_position) < f32::EPSILON { + position.0 = target; + *imperative = Imperative::Idle; + } else { + position.0 = new_position; + } + } + } + } +} diff --git a/src/shared.rs b/src/shared.rs index b907b57..5d6cbed 100644 --- a/src/shared.rs +++ b/src/shared.rs @@ -1,9 +1,12 @@ use crate::protocol::Replicate; +use crate::shared::imperative::*; use bevy::prelude::*; use lightyear::prelude::*; use serde::Deserialize; use serde::Serialize; +pub mod imperative; + pub const CLIENT_ID: u64 = 0; pub const KEY: [u8; 32] = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -17,6 +20,7 @@ pub struct PlayerBundle { position: PlayerPosition, color: PlayerColor, replicate: Replicate, + imperative: Imperative, } #[derive(Component, Message, Serialize, Deserialize, Clone, Debug, PartialEq)] @@ -35,6 +39,7 @@ impl PlayerBundle { position: PlayerPosition(position), color: PlayerColor(color), replicate: Replicate::default(), + imperative: Imperative::Idle, } } } diff --git a/src/shared/imperative.rs b/src/shared/imperative.rs new file mode 100644 index 0000000..1370ddb --- /dev/null +++ b/src/shared/imperative.rs @@ -0,0 +1,9 @@ +use bevy::prelude::*; +use serde::Deserialize; +use serde::Serialize; + +#[derive(Component, Copy, Clone, PartialEq, Debug, Deserialize, Serialize)] +pub enum Imperative { + Idle, + WalkTo(Vec2), +} |