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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use bevy::{app::AppExit, prelude::*};

use crate::AppState;

use super::{
    components::{QuitButton, Screen, SettingsButton},
    styles::{HOVERED_BUTTON_COLOR, NORMAL_BUTTON_COLOR, PRESSED_BUTTON_COLOR},
};

pub fn interact_with_quit_button(
    commands: Commands,
    mut button_q: Query<
        (&Interaction, &mut BackgroundColor),
        (Changed<Interaction>, With<QuitButton>),
    >,
    state: Res<State<AppState>>,
    screen_q: Query<Entity, With<Screen>>,
    mut next_state: ResMut<NextState<AppState>>,
    mut app_exit_event_writer: EventWriter<AppExit>,
) {
    if let Ok((interaction, mut background_color)) = button_q.get_single_mut() {
        match *interaction {
            Interaction::Pressed => {
                *background_color = PRESSED_BUTTON_COLOR.into();
                despawn_screens(commands, screen_q);
                match state.get() {
                    AppState::Game => {
                        next_state.set(AppState::MainMenu);
                    }
                    AppState::MainMenu => {
                        app_exit_event_writer.send(AppExit);
                    }
                    // AppState::Settings => {
                    //     next_state.set(AppState::MainMenu);
                    // }
                    _ => (),
                }
            }
            Interaction::Hovered => {
                *background_color = HOVERED_BUTTON_COLOR.into();
            }
            Interaction::None => {
                *background_color = NORMAL_BUTTON_COLOR.into();
            }
        }
    }
}

pub fn interact_with_settings_button(
    mut button_q: Query<
        (&Interaction, &mut BackgroundColor),
        (Changed<Interaction>, With<SettingsButton>),
    >,
    // mut settings_state: ResMut<NextState<AppState>>,
) {
    if let Ok((interaction, mut background_color)) = button_q.get_single_mut() {
        match *interaction {
            Interaction::Pressed => {
                *background_color = PRESSED_BUTTON_COLOR.into();
                // settings_state.set(AppState::Settings);
            }
            Interaction::Hovered => {
                *background_color = HOVERED_BUTTON_COLOR.into();
            }
            Interaction::None => {
                *background_color = NORMAL_BUTTON_COLOR.into();
            }
        }
    }
}

// pub fn interact_with_respawn_button(
//     mut button_q: Query<
//         (&Interaction, &mut BackgroundColor),
//         (Changed<Interaction>, With<RespawnButton>),
//     >,
//     mut spawn_ev: EventWriter<SpawnEvent>,
//     mut state: ResMut<NextState<SimulationState>>,
//     cam_q: Query<&CameraTarget, With<Camera>>,
//     commands: Commands,
//     respawn_q: Query<Entity, With<Screen>>,
//     SimulationState maps: Res<Assets<Map>>,
//     map: Res<MapHandle>,
// ) {
//     if let Ok(cam) = cam_q.get_single() {
//         if let Some(map) = maps.get(map.0.id()) {
//             if let Ok((interaction, mut background_color)) = button_q.get_single_mut() {
//                 match *interaction {
//                     Interaction::Pressed => {
//                         *background_color = PRESSED_BUTTON_COLOR.into();
//                         despawn_screens(commands, respawn_q);
//                         state.set(SimulationState::Running);
//                         spawn_ev.send(SpawnEvent {
//                             player_number: cam.target,
//                             transform: Vec3 {
//                                 x: 5.0,
//                                 y: map.map_height as f32 * 2.0 + 1.0,
//                                 z: 0.0,
//                             },
//                         });
//                     }
//                     Interaction::Hovered => {
//                         *background_color = HOVERED_BUTTON_COLOR.into();
//                     }
//                     Interaction::None => {
//                         *background_color = NORMAL_BUTTON_COLOR.into();
//                     }
//                 }
//             }
//         }
//     }
// }

pub fn despawn_screens(mut commands: Commands, mut screen_q: Query<Entity, With<Screen>>) {
    for ent in screen_q.iter_mut() {
        commands.entity(ent).despawn_recursive();
    }
}

pub fn switch_to_menu(mut state: ResMut<NextState<AppState>>){
    state.set(AppState::MainMenu);
}