Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Indy2222 committed Sep 22, 2023
1 parent f10a665 commit beccff4
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 3 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: 1 addition & 1 deletion crates/messages/src/players/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub enum ToPlayers {
/// replaced by this one.
SetPath {
entity: EntityNet,
waypoints: PathNet,
waypoints: Option<PathNet>,
},
/// Instantaneously transform an object.
///
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, NetRecvTransformEvent,
NetRecvSetPathEvent, NetRecvSpawnActiveEvent, NetRecvTransformEvent,
},
};
use crate::{netstate::NetStatePlugin, network::NetworkPlugin};
Expand Down
35 changes: 34 additions & 1 deletion crates/multiplayer/src/playermsg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy::{
};
use de_core::{gconfig::GameConfig, schedule::PreMovement, state::AppState};
use de_messages::{EntityNet, NetEntityIndex, ToPlayers};
use de_types::{objects::ActiveObjectType, player::Player};
use de_types::{objects::ActiveObjectType, path::Path, player::Player};

use crate::messages::{FromPlayersEvent, MessagesSet};

Expand All @@ -18,6 +18,7 @@ impl Plugin for PlayerMsgPlugin {
.add_event::<NetRecvDespawnActiveEvent>()
.add_event::<NetRecvHealthEvent>()
.add_event::<NetRecvTransformEvent>()
.add_event::<NetRecvSetPathEvent>()
.add_systems(OnEnter(AppState::InGame), setup)
.add_systems(OnExit(AppState::InGame), cleanup)
.add_systems(
Expand Down Expand Up @@ -145,6 +146,26 @@ impl NetRecvTransformEvent {
}
}

#[derive(Event)]
pub struct NetRecvSetPathEvent {
entity: Entity,
path: Option<Path>,
}

impl NetRecvSetPathEvent {
fn new(entity: Entity, path: Option<Path>) -> Self {
Self { entity, path }
}

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

pub fn path(&self) -> Option<&Path> {
self.path.as_ref()
}
}

#[derive(SystemParam)]
pub struct NetEntities<'w> {
config: Res<'w, GameConfig>,
Expand Down Expand Up @@ -329,6 +350,7 @@ fn recv_messages(
mut inputs: EventReader<FromPlayersEvent>,
mut spawn_events: EventWriter<NetRecvSpawnActiveEvent>,
mut despawn_events: EventWriter<NetRecvDespawnActiveEvent>,
mut path_events: EventWriter<NetRecvSetPathEvent>,
mut transform_events: EventWriter<NetRecvTransformEvent>,
mut health_events: EventWriter<NetRecvHealthEvent>,
) {
Expand All @@ -354,6 +376,17 @@ fn recv_messages(
let local = net_commands.deregister(*entity);
despawn_events.send(NetRecvDespawnActiveEvent::new(local));
}
ToPlayers::SetPath { entity, waypoints } => {
let Some(local) = net_commands.remote_local_id(*entity) else {
warn!("Received net path update of unrecognized entity: {entity:?}");
continue;
};

path_events.send(NetRecvSetPathEvent::new(
local,
waypoints.as_ref().map(|p| p.into()),
));
}
ToPlayers::Transform { entity, transform } => {
if let Some(local) = net_commands.remote_local_id(*entity) {
transform_events.send(NetRecvTransformEvent::new(local, transform.into()));
Expand Down
2 changes: 2 additions & 0 deletions crates/pathing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ categories.workspace = true
# DE
de_core.workspace = true
de_map.workspace = true
de_messages.workspace = true
de_multiplayer.workspace = true
de_objects.workspace = true
de_types.workspace = true

Expand Down
3 changes: 3 additions & 0 deletions crates/pathing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod graph;
mod path;
mod pplugin;
mod query;
mod syncing;
mod triangulation;
mod utils;

Expand All @@ -24,6 +25,7 @@ pub use path::ScheduledPath;
use pplugin::PathingPlugin;
pub use pplugin::UpdateEntityPathEvent;
pub use query::{PathQueryProps, PathTarget};
use syncing::SyncingPlugin;

pub struct PathingPluginGroup;

Expand All @@ -32,5 +34,6 @@ impl PluginGroup for PathingPluginGroup {
PluginGroupBuilder::start::<Self>()
.add(FinderPlugin)
.add(PathingPlugin)
.add(SyncingPlugin)
}
}
4 changes: 4 additions & 0 deletions crates/pathing/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ impl ScheduledPath {
Self { path, current }
}

pub(crate) fn path(&self) -> &Path {
&self.path
}

/// Returns the final point of the path schedule.
pub fn destination(&self) -> Vec2 {
self.path.waypoints()[0]
Expand Down
84 changes: 84 additions & 0 deletions crates/pathing/src/syncing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use bevy::prelude::*;
use de_core::{
objects::Local,
schedule::{Movement, PreMovement},
state::AppState,
};
use de_messages::ToPlayers;
use de_multiplayer::{GameNetSet, NetEntities, NetRecvSetPathEvent, ToPlayersEvent};

use crate::ScheduledPath;

pub struct SyncingPlugin;

impl Plugin for SyncingPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
PreMovement,
receive_paths
.run_if(on_event::<NetRecvSetPathEvent>())
.run_if(in_state(AppState::InGame))
.after(GameNetSet::Messages),
)
.add_systems(
Movement,
(send_new_paths, send_removed_paths).run_if(in_state(AppState::InGame)),
);
}
}

fn receive_paths(mut commands: Commands, mut events: EventReader<NetRecvSetPathEvent>) {
for event in events.iter() {
let mut entity_commands = commands.entity(event.entity());

match event.path() {
Some(path) => {
entity_commands.insert(ScheduledPath::new(path.clone()));
}
None => {
entity_commands.remove::<ScheduledPath>();
}
}
}
// TODO
}

type UpdatedPaths<'w, 's> =
Query<'w, 's, (Entity, &'static ScheduledPath), (With<Local>, Changed<ScheduledPath>)>;

fn send_new_paths(
net_entities: NetEntities,
updates: UpdatedPaths,
mut net_events: EventWriter<ToPlayersEvent>,
) {
// TODO only if is multiplayer

for (entity, scheduled_path) in updates.iter() {
net_events.send(ToPlayersEvent::new(ToPlayers::SetPath {
entity: net_entities.local_net_id(entity),
waypoints: Some(scheduled_path.path().try_into().unwrap()),
}));
}
}

// TODO rename
fn send_removed_paths(
net_entities: NetEntities,
locals: Query<Entity, With<Local>>,
mut removes: RemovedComponents<ScheduledPath>,
mut net_events: EventWriter<ToPlayersEvent>,
) {
// TODO only if multiplayer

for entity in removes.iter() {
if !locals.contains(entity) {
// It is either a spawned entity or non-local entity.
continue;
}

net_events.send(ToPlayersEvent::new(ToPlayers::SetPath {
entity: net_entities.local_net_id(entity),
waypoints: None,
}));
}
}
1 change: 1 addition & 0 deletions crates/types/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use glam::Vec2;

/// A path on the map defined by a sequence of way points. Start and target
/// position are included.
#[derive(Clone)]
pub struct Path {
length: f32,
waypoints: Vec<Vec2>,
Expand Down

0 comments on commit beccff4

Please sign in to comment.