aboutsummaryrefslogtreecommitdiffstats
path: root/src/server.rs
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2024-03-14 14:24:43 +0100
committerLibravatar Alexander Foremny <aforemny@posteo.de>2024-03-15 02:50:59 +0100
commit02750384df3142bda62a7ce0feb08d115aa6af29 (patch)
treed7b6d4da9a2cd9214a623d19d439b4c9b7e029b7 /src/server.rs
parent69584a302d132dc2bcc3837437e7347a3e0a5114 (diff)
feat: walking
Diffstat (limited to 'src/server.rs')
-rw-r--r--src/server.rs34
1 files changed, 29 insertions, 5 deletions
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;
+ }
+ }
+ }
+ }
+}