From 73faca522f7bcbd04b32768705f0d61b3250405a Mon Sep 17 00:00:00 2001 From: Martin Indra Date: Tue, 19 Sep 2023 16:00:24 +0200 Subject: [PATCH] WIP --- Cargo.lock | 2 + crates/movement/Cargo.toml | 2 + crates/movement/src/movement.rs | 70 +++++++++++++++++++++++++++-- crates/multiplayer/src/lib.rs | 2 +- crates/multiplayer/src/playermsg.rs | 27 +++++++++++ 5 files changed, 98 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9fc421c40..42b8c0a90 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 bdac1f27b..a3e7a7495 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 167a92880..96799d6db 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