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 80cac40 commit edcdee5
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
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: 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
55 changes: 55 additions & 0 deletions crates/pathing/src/syncing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use bevy::prelude::*;
use de_core::{objects::Local, schedule::Movement, state::AppState};
use de_messages::ToPlayers;
use de_multiplayer::{NetEntities, ToPlayersEvent};

use crate::ScheduledPath;

pub struct SyncingPlugin;

impl Plugin for SyncingPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Movement,
(send_new_paths, send_removed_paths).run_if(in_state(AppState::InGame)),
);
}
}

// TODO rename
fn send_new_paths(
net_entities: NetEntities,
updates: Query<(Entity, &ScheduledPath), (With<Local>, Changed<ScheduledPath>)>,
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,
}));
}
}

0 comments on commit edcdee5

Please sign in to comment.