From af11fa97fb8cb8a4c9a876b6f40a510869d25455 Mon Sep 17 00:00:00 2001 From: Michael Bradley Date: Sat, 18 Oct 2025 17:49:07 -0400 Subject: [PATCH] Convert distribution to observers And only distribute your own components --- src/net/distribution.rs | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/net/distribution.rs b/src/net/distribution.rs index d4d285b..70f2061 100644 --- a/src/net/distribution.rs +++ b/src/net/distribution.rs @@ -2,32 +2,51 @@ use bevy::prelude::*; use super::{ packet::{InboundPacket, OutboundPacket, Packet}, - peer::{Peer, PeerID}, + peer::PeerID, }; +#[derive(Component)] +pub struct PeerOwned; + fn spawner> + Component>( mut inbound: MessageReader, mut commands: Commands, ) { for InboundPacket(packet) in inbound.read() { - if let Ok(entity) = T::try_from(packet.message.clone()) { - commands.spawn(entity); + if let Ok(component) = T::try_from(packet.message.clone()) { + commands.spawn((component, PeerOwned)); } } } -fn sender> + Component + Clone>( - peers: Query<&PeerID, Added>, - entities: Query<&T>, +fn new_peer> + Component + Clone>( + add: On, + peers: Query<&PeerID>, + components: Query<&T, Without>, + mut outbound: MessageWriter, +) -> Result { + let peer = peers.get(add.entity)?; + for component in components { + outbound.write(Packet::create(component.clone().into(), peer.id)); + } + Ok(()) +} + +fn new_entity> + Component + Clone>( + add: On, + peers: Query<&PeerID>, + components: Query<&T, Without>, mut outbound: MessageWriter, ) { - for peer in peers { - for entity in entities { - outbound.write(Packet::create((*entity).clone().into(), peer.id)); + if let Ok(component) = components.get(add.entity) { + for peer in peers { + outbound.write(Packet::create(component.clone().into(), peer.id)); } } } pub fn distribution_plugin> + TryFrom> + Component + Clone>(app: &mut App) { - app.add_systems(FixedUpdate, (sender::, spawner::)); + app.add_systems(FixedUpdate, spawner::) + .add_observer(new_peer::) + .add_observer(new_entity::); }