Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Indy2222 committed Sep 19, 2023
1 parent c373022 commit 73faca5
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/movement/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 66 additions & 4 deletions crates/movement/src/movement.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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::<NetRecvTransformEvent>())
.after(update_transform),
send_remote_updates.after(update_transform),
)
.run_if(in_state(GameState::Playing)),
);
}
}
Expand Down Expand Up @@ -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<NetRecvTransformEvent>,
) {
for event in events.iter() {
if let Ok(mut transform) = entities.get_mut(event.entity()) {
*transform = event.transform();
}
}
}

fn send_remote_updates(
time: Res<Time>,
config: Res<GameConfig>,
net_entities: NetEntities,
mut entities: Query<(Entity, &mut TransformSync, &Transform)>,
mut net_events: EventWriter<ToPlayersEvent>,
) {
// TODO move to system conditions
if !config.multiplayer() {
return;
}

let time = time.elapsed();
for (entity, mut sync, tranform) in entities.iter_mut() {
if sync.outdated(time) {
sync.refresh(time);
}

net_events.send(ToPlayersEvent::new(ToPlayers::Transform {
entity: net_entities.local_net_id(entity),
transform: tranform.into(),
}));
}
}

fn clamp(bounds: &MapBounds, translation: Vec3) -> Vec3 {
let offset = Vec2::splat(EXCLUSION_OFFSET);
let a = (bounds.min() + offset).to_msl();
Expand Down
2 changes: 1 addition & 1 deletion crates/multiplayer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use crate::{
netstate::NetState,
playermsg::{
GameNetSet, NetEntities, NetEntityCommands, NetRecvDespawnActiveEvent, NetRecvHealthEvent,
NetRecvSpawnActiveEvent,
NetRecvSpawnActiveEvent, NetRecvTransformEvent,
},
};
use crate::{netstate::NetStatePlugin, network::NetworkPlugin};
Expand Down
27 changes: 27 additions & 0 deletions crates/multiplayer/src/playermsg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl Plugin for PlayerMsgPlugin {
app.add_event::<NetRecvSpawnActiveEvent>()
.add_event::<NetRecvDespawnActiveEvent>()
.add_event::<NetRecvHealthEvent>()
.add_event::<NetRecvTransformEvent>()
.add_systems(OnEnter(AppState::InGame), setup)
.add_systems(OnExit(AppState::InGame), cleanup)
.add_systems(
Expand Down Expand Up @@ -124,6 +125,26 @@ impl NetRecvHealthEvent {
}
}

#[derive(Event)]
pub struct NetRecvTransformEvent {
entity: Entity,
transform: Transform,
}

impl NetRecvTransformEvent {
fn new(entity: Entity, transform: Transform) -> Self {
Self { entity, transform }
}

pub fn entity(&self) -> Entity {
self.entity
}

pub fn transform(&self) -> Transform {
self.transform
}
}

#[derive(SystemParam)]
pub struct NetEntities<'w> {
config: Res<'w, GameConfig>,
Expand Down Expand Up @@ -305,6 +326,7 @@ fn recv_messages(
mut inputs: EventReader<FromPlayersEvent>,
mut spawn_events: EventWriter<NetRecvSpawnActiveEvent>,
mut despawn_events: EventWriter<NetRecvDespawnActiveEvent>,
mut transform_events: EventWriter<NetRecvTransformEvent>,
mut health_events: EventWriter<NetRecvHealthEvent>,
) {
for input in inputs.iter() {
Expand All @@ -329,6 +351,11 @@ fn recv_messages(
let local = net_commands.deregister(*entity);
despawn_events.send(NetRecvDespawnActiveEvent::new(local));
}
ToPlayers::Transform { entity, transform } => {
if let Some(local) = net_commands.local_id(*entity) {
transform_events.send(NetRecvTransformEvent::new(local, transform.into()));
}
}
ToPlayers::ChangeHealth { entity, delta } => {
let Some(local) = net_commands.local_id(*entity) else {
warn!("Received net health update of unrecognized entity: {entity:?}");
Expand Down

0 comments on commit 73faca5

Please sign in to comment.