1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use aftergame::AfterGamePlugin;
use bevy::{app::PluginGroupBuilder, prelude::*};
use de_core::{gresult::GameResult, nested_state, state::AppState};
use mainmenu::MainMenuPlugin;
use mapselection::MapSelectionPlugin;
use menu::{MenuPlugin, ScreenStatePlugin};
use multiplayer::MultiplayerPlugin;
use singleplayer::SinglePlayerPlugin;

mod aftergame;
mod mainmenu;
mod mapselection;
mod menu;
mod multiplayer;
mod singleplayer;

pub struct MenuPluginGroup;

impl PluginGroup for MenuPluginGroup {
    fn build(self) -> PluginGroupBuilder {
        PluginGroupBuilder::start::<Self>()
            .add(MenuStatePlugin)
            .add(MenuPlugin)
            .add(ScreenStatePlugin::<MenuState>::default())
            .add(MainMenuPlugin)
            .add(MapSelectionPlugin)
            .add(SinglePlayerPlugin)
            .add(MultiplayerPlugin)
            .add(AfterGamePlugin)
    }
}

nested_state!(
    AppState::InMenu -> MenuState,
    doc = "Top-level menu state. Each variant corresponds to menu section or a single menu screen.",
    enter = menu_entered_system,
    variants = {
        MainMenu,
        SinglePlayerGame,
        Multiplayer,
        AfterGame,
    }
);

fn menu_entered_system(
    mut next_state: ResMut<NextState<MenuState>>,
    result: Option<Res<GameResult>>,
) {
    if result.is_some() {
        next_state.set(MenuState::AfterGame);
    } else {
        next_state.set(MenuState::MainMenu);
    }
}