From e8fa96142a0ca9337ff554e55943530200f6f4dd Mon Sep 17 00:00:00 2001 From: Martin Indra Date: Tue, 19 Sep 2023 16:00:24 +0200 Subject: [PATCH] WIP --- Cargo.lock | 3 + crates/movement/Cargo.toml | 7 ++- crates/movement/src/movement.rs | 93 +++++++++++++++++++++++++++-- crates/multiplayer/src/lib.rs | 2 +- crates/multiplayer/src/playermsg.rs | 27 +++++++++ 5 files changed, 123 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9fc421c40..5f37820be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2537,9 +2537,12 @@ dependencies = [ "de_core", "de_index", "de_map", + "de_messages", + "de_multiplayer", "de_objects", "de_pathing", "de_types", + "fastrand 1.9.0", "glam", "parry2d", "parry3d", diff --git a/crates/movement/Cargo.toml b/crates/movement/Cargo.toml index bdac1f27b..b2b180972 100644 --- a/crates/movement/Cargo.toml +++ b/crates/movement/Cargo.toml @@ -16,12 +16,15 @@ 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 # Other bevy.workspace = true -parry3d.workspace = true -parry2d.workspace = true +fastrand.workspace = true glam.workspace = true +parry2d.workspace = true +parry3d.workspace = true diff --git a/crates/movement/src/movement.rs b/crates/movement/src/movement.rs index 167a92880..746c047a7 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, - objects::MovableSolid, + gconfig::GameConfig, + objects::{Local, 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; @@ -19,13 +22,18 @@ impl Plugin for MovementPlugin { fn build(&self, app: &mut App) { app.add_systems( PreMovement, - setup_entities.run_if(in_state(AppState::InGame)), + (setup_entities, setup_local).run_if(in_state(AppState::InGame)), ) .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,79 @@ fn update_transform( } } +// TODO move this? +// TODO add these +#[derive(Component)] +struct TransformSync(Duration); + +impl TransformSync { + fn new() -> Self { + Self(Duration::ZERO) + } + + // TODO docs + fn refresh(&mut self, time: Duration) { + // TODO constant + self.0 = time + Duration::from_secs(2) + Duration::from_millis(fastrand::u64(0..1_000)); + } + + fn outdated(&self, time: Duration) -> bool { + time >= self.0 + } +} + +// TODO rename +fn setup_local( + mut commands: Commands, + config: Res, + entities: Query, With, Without)>, +) { + // TODO move to system conditions + if !config.multiplayer() { + return; + } + + for entity in entities.iter() { + commands.entity(entity).insert(TransformSync::new()); + } +} + +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