diff --git a/Cargo.lock b/Cargo.lock index 9fc421c4..42b8c0a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2537,6 +2537,8 @@ dependencies = [ "de_core", "de_index", "de_map", + "de_messages", + "de_multiplayer", "de_objects", "de_pathing", "de_types", diff --git a/crates/movement/Cargo.toml b/crates/movement/Cargo.toml index bdac1f27..a3e7a749 100644 --- a/crates/movement/Cargo.toml +++ b/crates/movement/Cargo.toml @@ -16,6 +16,8 @@ categories.workspace = true de_core.workspace = true de_index.workspace = true de_map.workspace = true +de_messages.workspace = true +de_multiplayer.workspace = true de_objects.workspace = true de_pathing.workspace = true de_types.workspace = true diff --git a/crates/movement/src/movement.rs b/crates/movement/src/movement.rs index 167a9288..96799d6d 100644 --- a/crates/movement/src/movement.rs +++ b/crates/movement/src/movement.rs @@ -1,13 +1,16 @@ -use std::marker::PhantomData; +use std::{marker::PhantomData, time::Duration}; use bevy::prelude::*; use de_core::{ gamestate::GameState, + gconfig::GameConfig, objects::MovableSolid, schedule::{Movement, PreMovement}, state::AppState, }; use de_map::size::MapBounds; +use de_messages::ToPlayers; +use de_multiplayer::{NetEntities, NetRecvTransformEvent, ToPlayersEvent}; use de_objects::EXCLUSION_OFFSET; use de_types::projection::ToAltitude; @@ -23,9 +26,14 @@ impl Plugin for MovementPlugin { ) .add_systems( Movement, - update_transform - .run_if(in_state(GameState::Playing)) - .in_set(MovementSet::UpdateTransform), + ( + update_transform.in_set(MovementSet::UpdateTransform), + receive_remote_updates + .run_if(on_event::()) + .after(update_transform), + send_remote_updates.after(update_transform), + ) + .run_if(in_state(GameState::Playing)), ); } } @@ -147,6 +155,60 @@ fn update_transform( } } +// TODO move this? +// TODO add these +#[derive(Component)] +struct TransformSync(Duration); + +impl TransformSync { + // TODO docs + fn refresh(&mut self, time: Duration) { + // TODO randomization + // TODO constant + self.0 = time + Duration::from_secs(2); + } + + fn outdated(&self, time: Duration) -> bool { + time >= self.0 + } +} + +fn receive_remote_updates( + mut entities: Query<&mut Transform>, + mut events: EventReader, +) { + for event in events.iter() { + if let Ok(mut transform) = entities.get_mut(event.entity()) { + *transform = event.transform(); + } + } +} + +fn send_remote_updates( + time: Res