diff --git a/client/lib/src/networking.rs b/client/lib/src/networking.rs index 9c9a79a6..1c3d0722 100644 --- a/client/lib/src/networking.rs +++ b/client/lib/src/networking.rs @@ -42,6 +42,7 @@ impl Plugin for NetworkPlugin { fn build(&self, app: &mut App) { app.init_resource::() .add_systems(OnEnter(GameState::Online(OnlineState::Lobby)), setup_socket) + .add_systems(OnExit(GameState::Online(OnlineState::Match)), teardown) .add_systems( FixedUpdate, wait_for_players.run_if(in_state(GameState::Online(OnlineState::Lobby))), @@ -157,6 +158,17 @@ fn setup_socket(mut commands: Commands) { commands.insert_resource(MatchboxSocket::from(sock)); } +fn teardown(mut commands: Commands) { + commands.remove_resource::>(); + commands.remove_resource::>(); + commands.remove_resource::>(); + + commands.remove_resource::(); + commands.remove_resource::(); + commands.remove_resource::(); + commands.remove_resource::(); +} + #[derive(Debug, Default)] enum ConnectionState { #[default] @@ -552,7 +564,11 @@ fn generate_online_input_streams( } } -fn handle_ggrs_events(mut sesh: ResMut>) { +fn handle_ggrs_events( + mut sesh: ResMut>, + mut next_main_state: ResMut>, + mut next_match_state: ResMut>, +) { let Session::P2P(s) = sesh.as_mut() else { return; }; @@ -560,18 +576,23 @@ fn handle_ggrs_events(mut sesh: ResMut>) { for event in s.events() { match event { ggrs::GgrsEvent::Disconnected { addr: _ } - | ggrs::GgrsEvent::NetworkInterrupted { - addr: _, - disconnect_timeout: _, - } - | ggrs::GgrsEvent::NetworkResumed { addr: _ } - | ggrs::GgrsEvent::WaitRecommendation { skip_frames: _ } | ggrs::GgrsEvent::DesyncDetected { frame: _, local_checksum: _, remote_checksum: _, addr: _, } => { + warn!("GGRS event: {:?}, transitioning to main menu", event); + next_main_state.set(GameState::MainMenu); + next_match_state.set(MatchState::None); + // TODO: Add like a notification for the user or something + } + ggrs::GgrsEvent::NetworkInterrupted { + addr: _, + disconnect_timeout: _, + } + | ggrs::GgrsEvent::NetworkResumed { addr: _ } + | ggrs::GgrsEvent::WaitRecommendation { skip_frames: _ } => { debug!("Unhandled GGRS event: {:?}", event); } _ => {} diff --git a/docs/tasks/main_todo.md b/docs/tasks/main_todo.md index 054eca9e..41d53c43 100644 --- a/docs/tasks/main_todo.md +++ b/docs/tasks/main_todo.md @@ -12,7 +12,6 @@ - More synctest - Immediate desync on some starts - Seems like a system ordering inconsistency -- Add a system that gets you out of a desync without crashing the game ### Before public playtests