33 lines
902 B
Rust
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>));
|
|
}
|