Slightly better implementation of peers, still need to create a more generic system for deciding which components to distribute where and then use that for Peers.
This commit is contained in:
parent
e013fb427a
commit
53fe3333f0
14 changed files with 438 additions and 265 deletions
|
|
@ -2,48 +2,32 @@ use bevy::prelude::*;
|
|||
|
||||
use super::{
|
||||
packet::{InboundPacket, OutboundPacket, Packet},
|
||||
peer::Peer,
|
||||
state::NetworkState,
|
||||
peer::{Peer, PeerID},
|
||||
};
|
||||
|
||||
fn spawner<T: TryFrom<Vec<u8>> + Component>(
|
||||
mut inbound: EventReader<InboundPacket>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
for packet in inbound.read() {
|
||||
if let Ok(entity) = T::try_from(packet.0.message.clone()) {
|
||||
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<&Peer, Added<Peer>>,
|
||||
peers: Query<&PeerID, Added<Peer>>,
|
||||
entities: Query<&T>,
|
||||
mut outbound: EventWriter<OutboundPacket>,
|
||||
) {
|
||||
for peer in peers {
|
||||
for entity in entities {
|
||||
outbound.write(OutboundPacket(Packet::new(
|
||||
(*entity).clone().into(),
|
||||
peer.id,
|
||||
)));
|
||||
outbound.write(Packet::create((*entity).clone().into(), peer.id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Networked: Into<Vec<u8>> + TryFrom<Vec<u8>> + Component + Clone {
|
||||
fn register(app: &mut App);
|
||||
}
|
||||
|
||||
impl<T> Networked for T
|
||||
where
|
||||
T: Into<Vec<u8>> + TryFrom<Vec<u8>> + Component + Clone,
|
||||
{
|
||||
fn register(app: &mut App) {
|
||||
app.add_systems(
|
||||
FixedUpdate,
|
||||
(sender::<T>, spawner::<T>).run_if(in_state(NetworkState::MultiPlayer)),
|
||||
);
|
||||
}
|
||||
pub fn distribution_plugin<T: Into<Vec<u8>> + TryFrom<Vec<u8>> + Component + Clone>(app: &mut App) {
|
||||
app.add_systems(FixedUpdate, (sender::<T>, spawner::<T>));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue