Skip to content

Commit

Permalink
Add primitive return to main menu after online game
Browse files Browse the repository at this point in the history
  • Loading branch information
haihala committed Dec 18, 2024
1 parent 6e4382f commit 1c83350
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
35 changes: 28 additions & 7 deletions client/lib/src/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl Plugin for NetworkPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<InputStream>()
.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))),
Expand Down Expand Up @@ -157,6 +158,17 @@ fn setup_socket(mut commands: Commands) {
commands.insert_resource(MatchboxSocket::from(sock));
}

fn teardown(mut commands: Commands) {
commands.remove_resource::<MatchboxSocket<MultipleChannels>>();
commands.remove_resource::<bevy_ggrs::Session<Config>>();
commands.remove_resource::<bevy_ggrs::LocalInputs<Config>>();

commands.remove_resource::<Characters>();
commands.remove_resource::<Controllers>();
commands.remove_resource::<LocalCharacter>();
commands.remove_resource::<LocalController>();
}

#[derive(Debug, Default)]
enum ConnectionState {
#[default]
Expand Down Expand Up @@ -552,26 +564,35 @@ fn generate_online_input_streams(
}
}

fn handle_ggrs_events(mut sesh: ResMut<Session<Config>>) {
fn handle_ggrs_events(
mut sesh: ResMut<Session<Config>>,
mut next_main_state: ResMut<NextState<GameState>>,
mut next_match_state: ResMut<NextState<MatchState>>,
) {
let Session::P2P(s) = sesh.as_mut() else {
return;
};

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);
}
_ => {}
Expand Down
1 change: 0 additions & 1 deletion docs/tasks/main_todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 1c83350

Please sign in to comment.