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
use bevy::prelude::*;
use de_core::gresult::GameResult;
use de_gui::{GuiCommands, LabelCommands, OuterStyle};

use crate::{menu::Menu, MenuState};

pub(crate) struct AfterGamePlugin;

impl Plugin for AfterGamePlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(OnEnter(MenuState::AfterGame), setup)
            .add_systems(OnEnter(MenuState::AfterGame), cleanup);
    }
}

fn cleanup(mut commands: Commands) {
    commands.remove_resource::<GameResult>();
}

fn setup(mut commands: GuiCommands, menu: Res<Menu>, result: Res<GameResult>) {
    let text = match result.as_ref() {
        GameResult::Finished(result) => {
            if result.won() {
                "You have won!".to_owned()
            } else {
                "You have lost!".to_owned()
            }
        }
        GameResult::Error(message) => {
            error!("Game finished with an error: {message}");
            format!("Error: {message}")
        }
    };
    let text_id = commands.spawn_label(OuterStyle::default(), text).id();
    commands.entity(menu.root_node()).add_child(text_id);
}