From 3921537360da2206f698aa60cbd2a6d320a16288 Mon Sep 17 00:00:00 2001 From: Michael Bradley Date: Sun, 26 Oct 2025 18:34:13 -0400 Subject: [PATCH] Add trait wrapping the distribution query filters --- src/game/plugin.rs | 2 +- src/game/runtime.rs | 4 +- src/game/seed.rs | 2 +- src/net/distribution.rs | 91 +++++++++++++++++++++++++++-------------- src/net/mod.rs | 3 +- 5 files changed, 67 insertions(+), 35 deletions(-) diff --git a/src/game/plugin.rs b/src/game/plugin.rs index 13ea24f..57d4100 100644 --- a/src/game/plugin.rs +++ b/src/game/plugin.rs @@ -62,7 +62,7 @@ impl Plugin for GamePlugin { fn build(&self, app: &mut App) { app.add_plugins(( NetIOPlugin::maybe_peer(self.port, self.source.try_to_address()), - distribution_plugin::, + DistributionPlugin::::default(), PhysicsPlugins::default().with_length_unit(50.0), )) .init_state::() diff --git a/src/game/runtime.rs b/src/game/runtime.rs index 405a7e9..8c5bfa6 100644 --- a/src/game/runtime.rs +++ b/src/game/runtime.rs @@ -7,6 +7,8 @@ use bevy::{ prelude::*, }; +use crate::net::prelude::PeerOwned; + use super::{ objects::{Player, Radius}, seed::Seed, @@ -85,6 +87,6 @@ pub fn zoom_camera( Ok(()) } -pub fn reset_seed(mut seed: Single<&mut Seed>) { +pub fn reset_seed(mut seed: Single<&mut Seed, Without>) { **seed = Seed::random() } diff --git a/src/game/seed.rs b/src/game/seed.rs index 8c470ce..43b3fc2 100644 --- a/src/game/seed.rs +++ b/src/game/seed.rs @@ -6,7 +6,7 @@ use std::{ use bevy::prelude::*; use rand::random; -use crate::net::prelude::{EntityNetworkID, Networked}; +use crate::net::prelude::EntityNetworkID; /// Value with which to initialize the PRNG #[derive(Clone, Component, Copy, Debug)] diff --git a/src/net/distribution.rs b/src/net/distribution.rs index 6c47c61..90c8b97 100644 --- a/src/net/distribution.rs +++ b/src/net/distribution.rs @@ -1,3 +1,5 @@ +use std::marker::PhantomData; + use bevy::{ ecs::{component::Mutable, query::QueryFilter}, prelude::*, @@ -51,17 +53,27 @@ where } /// Components wishing to be networked must implement this type -pub trait Networked: Component + NetworkEncodable + NetworkDecodable { - type LocalFilter: QueryFilter; - type RemoteFilter: QueryFilter; +pub trait Networked: Component + NetworkEncodable + NetworkDecodable {} + +impl Networked for T where + T: Component + NetworkEncodable + NetworkDecodable +{ } -impl Networked for T -where - T: Component + NetworkEncodable + NetworkDecodable, -{ - type LocalFilter = Without; - type RemoteFilter = With; +pub trait NetworkSettings: 'static + Send + Sync { + type IncomingFilter: QueryFilter; + type LocalChangeFilter: QueryFilter; + type NewPeerFilter: QueryFilter; + type NewLocalEntityFilter: QueryFilter; +} + +pub struct DefaultNetworkSettings; + +impl NetworkSettings for DefaultNetworkSettings { + type IncomingFilter = With; + type LocalChangeFilter = Without; + type NewPeerFilter = Without; + type NewLocalEntityFilter = Without; } fn incoming_network_entity< @@ -85,9 +97,21 @@ fn incoming_network_entity< } } -fn new_peer( +fn changed_local_entity( + components: Query<(&T, &EntityNetworkID), (Changed, F)>, + peers: Query<&PeerID>, + mut outbound: MessageWriter, +) { + for (component, id) in components { + for peer in peers { + outbound.write(Packet::create(peer.id, id.0, component.encode())); + } + } +} + +fn new_peer( add: On, - components: Query<(&T, &EntityNetworkID), Without>, + components: Query<(&T, &EntityNetworkID), F>, peers: Query<&PeerID>, mut outbound: MessageWriter, ) -> Result { @@ -98,9 +122,9 @@ fn new_peer( Ok(()) } -fn new_local_entity( +fn new_local_entity( add: On, - components: Query<(&T, &EntityNetworkID), Without>, + components: Query<(&T, &EntityNetworkID), F>, peers: Query<&PeerID>, mut outbound: MessageWriter, ) { @@ -111,26 +135,31 @@ fn new_local_entity( } } -fn changed_local_entity( - components: Query<(&T, &EntityNetworkID), (F, Changed)>, - peers: Query<&PeerID>, - mut outbound: MessageWriter, -) { - for (component, id) in components { - for peer in peers { - outbound.write(Packet::create(peer.id, id.0, component.encode())); +#[derive(Debug)] +pub struct DistributionPlugin { + _phantom1: PhantomData, + _phantom2: PhantomData, +} + +impl Default for DistributionPlugin { + fn default() -> Self { + Self { + _phantom1: Default::default(), + _phantom2: Default::default(), } } } -pub fn distribution_plugin(app: &mut App) { - app.add_systems( - FixedUpdate, - ( - changed_local_entity::, - incoming_network_entity::, - ), - ) - .add_observer(new_peer::) - .add_observer(new_local_entity::); +impl Plugin for DistributionPlugin { + fn build(&self, app: &mut App) { + app.add_systems( + FixedUpdate, + ( + incoming_network_entity::, + changed_local_entity::, + ), + ) + .add_observer(new_peer::) + .add_observer(new_local_entity::); + } } diff --git a/src/net/mod.rs b/src/net/mod.rs index bcbb70f..e172fbe 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -11,7 +11,8 @@ mod thread; #[allow(unused_imports)] pub mod prelude { pub use super::distribution::{ - EntityNetworkID, NetworkDecodable, NetworkEncodable, Networked, distribution_plugin, + DefaultNetworkSettings, DistributionPlugin, EntityNetworkID, NetworkDecodable, + NetworkEncodable, NetworkSettings, Networked, PeerOwned, }; pub use super::packet::{InboundPacket, OutboundPacket, Packet}; pub use super::peer::{Peer, PeerID, PeerReceiveTiming, PeerSendTiming, PotentialPeers};