diff --git a/src/game/net.rs b/src/game/net.rs index 2c68886..5eb3538 100644 --- a/src/game/net.rs +++ b/src/game/net.rs @@ -2,28 +2,20 @@ use bevy::prelude::*; use crate::net::prelude::*; -pub fn handle_new_peer(new_peers: Query<&PeerID, Added>) { - for peer in new_peers { - info!("Peer {} was added", peer.id); - } +pub fn handle_new_peer(add: On, peers: Query<&PeerID>) -> Result { + let peer = peers.get(add.entity)?; + info!("Game: Peer {} was added", peer.id); + Ok(()) } -pub fn handle_deleted_peer( - mut old_peers: RemovedComponents, - peers: Query<&PeerID>, -) -> Result { - for entity in old_peers.read() { - if let Ok(peer) = peers.get(entity) { - info!("Peer {} was removed", peer.id); - } else { - info!("Peer {} was removed", entity); - } - } +pub fn handle_deleted_peer(remove: On, peers: Query<&PeerID>) -> Result { + let peer = peers.get(remove.entity)?; + info!("Game: Peer {} was removed", peer.id); Ok(()) } pub fn handle_incoming_packets(mut packets: MessageReader) { for InboundPacket(packet) in packets.read() { - info!("Packet received: {:?}", packet.message); + info!("Game: Packet received: {:?}", packet.message); } } diff --git a/src/game/plugin.rs b/src/game/plugin.rs index a7fdf48..586dd48 100644 --- a/src/game/plugin.rs +++ b/src/game/plugin.rs @@ -74,20 +74,13 @@ impl Plugin for GamePlugin { ) .add_systems( OnEnter(AppState::InGame), - ( - setup_from_seed, - (setup_player, setup_balls, setup_walls).after(setup_from_seed), - ), + (setup_from_seed, (setup_player, setup_balls, setup_walls)).chain(), ) .add_systems( FixedUpdate, ( check_for_seed.run_if(in_state(AppState::Loading)), - ( - handle_new_peer, - handle_deleted_peer, - handle_incoming_packets, - ), + handle_incoming_packets, ), ) .add_systems( @@ -103,7 +96,9 @@ impl Plugin for GamePlugin { ), quit.run_if(input_pressed(KeyCode::KeyQ)), ), - ); + ) + .add_observer(handle_new_peer) + .add_observer(handle_deleted_peer); match self.source { DataSource::Address(peer) => { diff --git a/src/game/ui.rs b/src/game/ui.rs index 60f17a1..a809f29 100644 --- a/src/game/ui.rs +++ b/src/game/ui.rs @@ -104,7 +104,7 @@ pub struct PotentialPeerUI; pub fn setup_potential_peer_ui(mut commands: Commands) { commands .spawn(( - Text::new("Potential peers:"), + Text::new("Potential peers:\n"), Node { position_type: PositionType::Absolute, bottom: Val::Px(5.0), 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::); } diff --git a/src/net/heartbeat.rs b/src/net/heartbeat.rs index 0d2f279..3e2003e 100644 --- a/src/net/heartbeat.rs +++ b/src/net/heartbeat.rs @@ -11,11 +11,9 @@ use super::{ queues::NetworkSend, }; -const PING_FREQUENCY: Duration = Duration::from_secs(3); -const MISSED_PINGS: u32 = 3; +pub const PING_FREQUENCY: Duration = Duration::from_secs(1); +const MISSED_PINGS: u32 = 5; -// TODO: Perhaps this needs a state rethink, is Single/Multiplayer actually useful vs Disconnected, Connecting, Connected? -// Would also help to state-scope some of these things, like InitialAddresses vs PeerMap pub fn heartbeat( peers: Query<(&PeerID, &PeerSendTiming)>, time: Res