distributed_physics_test/src/net/distribution.rs
Michael Bradley 27b4644730
All checks were successful
CI / Formatting (push) Successful in 32s
Update to Bevy 0.17.2
2025-10-13 15:17:23 -04:00

33 lines
902 B
Rust

use bevy::prelude::*;
use super::{
packet::{InboundPacket, OutboundPacket, Packet},
peer::{Peer, PeerID},
};
fn spawner<T: TryFrom<Vec<u8>> + Component>(
mut inbound: MessageReader<InboundPacket>,
mut commands: Commands,
) {
for InboundPacket(packet) in inbound.read() {
if let Ok(entity) = T::try_from(packet.message.clone()) {
commands.spawn(entity);
}
}
}
fn sender<T: Into<Vec<u8>> + Component + Clone>(
peers: Query<&PeerID, Added<Peer>>,
entities: Query<&T>,
mut outbound: MessageWriter<OutboundPacket>,
) {
for peer in peers {
for entity in entities {
outbound.write(Packet::create((*entity).clone().into(), peer.id));
}
}
}
pub fn distribution_plugin<T: Into<Vec<u8>> + TryFrom<Vec<u8>> + Component + Clone>(app: &mut App) {
app.add_systems(FixedUpdate, (sender::<T>, spawner::<T>));
}