distributed_physics_test/src/net/plugin.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

63 lines
1.8 KiB
Rust

use std::net::SocketAddr;
use bevy::prelude::*;
use super::{
heartbeat::{PotentialPeerTimer, heartbeat, ping_potential_peers, timeout},
io::{Config, handle_network_input, handle_network_output},
packet::{InboundPacket, OutboundPacket},
peer::{
PeerChangeMessage, PeerMap, PotentialPeers, handle_new_peer, handle_peer_change,
new_peer_message,
},
queues::{NetworkReceive, NetworkSend},
socket::bind_socket,
};
pub struct NetIOPlugin {
listen: u16,
initial_peers: Vec<SocketAddr>,
}
impl NetIOPlugin {
pub fn maybe_peer(listen: u16, peer: Option<SocketAddr>) -> Self {
Self {
listen,
initial_peers: match peer {
Some(addr) => vec![addr],
None => Vec::new(),
},
}
}
}
impl Plugin for NetIOPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
FixedPreUpdate,
(handle_network_input, (handle_peer_change, new_peer_message)).chain(),
)
.add_systems(
FixedUpdate,
(heartbeat, timeout, handle_new_peer, ping_potential_peers),
)
.add_systems(FixedPostUpdate, handle_network_output)
.init_resource::<Config>()
.init_resource::<PeerMap>()
.init_resource::<PotentialPeerTimer>()
.insert_resource(PotentialPeers::new(self.initial_peers.clone()))
.add_message::<PeerChangeMessage>()
.add_message::<InboundPacket>()
.add_message::<OutboundPacket>();
match bind_socket(self.listen) {
Ok((send, receive)) => {
app.insert_resource(NetworkSend::new(send))
.insert_resource(NetworkReceive::new(receive));
}
Err(err) => {
warn!("Failed to set up networking: {err}");
}
};
}
}