From 968b292eb1afafbd7fb4429a2947535ecbf8d4fd Mon Sep 17 00:00:00 2001 From: Martin Indra Date: Wed, 16 Aug 2023 21:22:52 +0200 Subject: [PATCH] WIP --- crates/menu/src/multiplayer/joined.rs | 25 +++++++++++-------------- crates/multiplayer/src/game.rs | 22 ++++++++++++++++++++++ crates/multiplayer/src/lib.rs | 2 +- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/crates/menu/src/multiplayer/joined.rs b/crates/menu/src/multiplayer/joined.rs index 3bc5d490..9c21d282 100644 --- a/crates/menu/src/multiplayer/joined.rs +++ b/crates/menu/src/multiplayer/joined.rs @@ -1,15 +1,8 @@ use bevy::prelude::*; -use de_core::{ - assets::asset_path, - gconfig::{GameConfig, LocalPlayers}, - player::Player, - state::AppState, -}; +use de_core::state::AppState; use de_gui::ToastEvent; use de_lobby_client::GetGameRequest; -use de_map::hash::MapHash; -use de_messages::Readiness; -use de_multiplayer::{GameReadinessEvent, ShutdownMultiplayerEvent}; +use de_multiplayer::{PeerJoinedEvent, PeerLeftEvent, ShutdownMultiplayerEvent}; use super::{ current::GameNameRes, @@ -21,10 +14,16 @@ pub(crate) struct JoinedGamePlugin; impl Plugin for JoinedGamePlugin { fn build(&self, app: &mut App) { - app.add_systems(OnExit(MultiplayerState::GameJoined), cleanup) + app.add_systems(OnEnter(MultiplayerState::GameJoined), refresh) + .add_systems(OnExit(MultiplayerState::GameJoined), cleanup) .add_systems( Update, - (refresh, handle_get_response).run_if(in_state(MultiplayerState::GameJoined)), + ( + refresh + .run_if(on_event::().or_else(on_event::())), + handle_get_response, + ) + .run_if(in_state(MultiplayerState::GameJoined)), ); } } @@ -36,14 +35,12 @@ fn cleanup(state: Res>, mut shutdown: EventWriter, mut sender: Sender) { - // TODO when player joins or first time + info!("Refreshing game info..."); sender.send(GetGameRequest::new(game_name.name_owned())); } fn handle_get_response( - mut commands: Commands, mut multi_state: ResMut>, - mut app_state: ResMut>, mut receiver: Receiver, mut toasts: EventWriter, ) { diff --git a/crates/multiplayer/src/game.rs b/crates/multiplayer/src/game.rs index ebee85cd..e8015e35 100644 --- a/crates/multiplayer/src/game.rs +++ b/crates/multiplayer/src/game.rs @@ -56,6 +56,24 @@ impl GameJoinedEvent { } } +#[derive(Event)] +pub struct PeerJoinedEvent(u8); + +impl PeerJoinedEvent { + pub fn id(&self) -> u8 { + self.0 + } +} + +#[derive(Event)] +pub struct PeerLeftEvent(u8); + +impl PeerLeftEvent { + pub fn id(&self) -> u8 { + self.0 + } +} + fn open_or_join( conf: Res, mut main_server: EventWriter, @@ -118,6 +136,8 @@ fn process_from_game( mut fatals: EventWriter, state: Res>, mut joined_events: EventWriter, + mut peer_joined_events: EventWriter, + mut peer_left_events: EventWriter, mut next_state: ResMut>, ) { for event in inputs.iter() { @@ -169,9 +189,11 @@ fn process_from_game( } FromGame::PeerJoined(id) => { info!("Peer {id} joined."); + peer_joined_events.send(PeerJoinedEvent(*id)); } FromGame::PeerLeft(id) => { info!("Peer {id} left."); + peer_left_events.send(PeerLeftEvent(*id)); } FromGame::GameReadiness(readiness) => { info!("Game readiness changed to: {readiness:?}"); diff --git a/crates/multiplayer/src/lib.rs b/crates/multiplayer/src/lib.rs index f8604f49..dbea8dc1 100644 --- a/crates/multiplayer/src/lib.rs +++ b/crates/multiplayer/src/lib.rs @@ -15,7 +15,7 @@ use stats::StatsPlugin; pub use crate::{ config::{ConnectionType, NetGameConf}, - game::{GameJoinedEvent, GameOpenedEvent}, + game::{GameJoinedEvent, GameOpenedEvent, PeerJoinedEvent, PeerLeftEvent}, lifecycle::{MultiplayerShuttingDownEvent, ShutdownMultiplayerEvent, StartMultiplayerEvent}, netstate::NetState, };