|
| 1 | +use bevy::prelude::*; |
| 2 | +use bevy_kira_audio::prelude::*; |
| 3 | +use bevy_mod_picking::prelude::*; |
| 4 | +use pyri_state::extra::entity_scope::StateScope; |
| 5 | +use pyri_state::prelude::*; |
| 6 | + |
| 7 | +use crate::core::pause::Pause; |
| 8 | +use crate::core::UpdateSet; |
| 9 | +use crate::game::actor::level::up::LevelUp; |
| 10 | +use crate::game::actor::level::Level; |
| 11 | +use crate::screen::fade_out; |
| 12 | +use crate::screen::playing::PlayingAssets; |
| 13 | +use crate::screen::playing::PlayingMenu; |
| 14 | +use crate::screen::Screen; |
| 15 | +use crate::ui::prelude::*; |
| 16 | +use crate::util::prelude::*; |
| 17 | + |
| 18 | +pub(super) fn plugin(app: &mut App) { |
| 19 | + app.add_systems( |
| 20 | + StateFlush, |
| 21 | + PlayingMenu::Victory.on_edge(Pause::disable, (Pause::enable_default, open_victory_menu)), |
| 22 | + ); |
| 23 | + |
| 24 | + app.init_resource::<EndlessMode>(); |
| 25 | + |
| 26 | + app.add_systems( |
| 27 | + Update, |
| 28 | + PlayingMenu::Victory |
| 29 | + .enter() |
| 30 | + .in_set(UpdateSet::SyncLate) |
| 31 | + .run_if(on_event::<LevelUp>().and_then(detect_victory)), |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Default, Resource)] |
| 36 | +pub struct EndlessMode(bool); |
| 37 | + |
| 38 | +fn open_victory_menu(mut commands: Commands, ui_root: Res<UiRoot>) { |
| 39 | + commands |
| 40 | + .spawn_with(victory_overlay) |
| 41 | + .set_parent(ui_root.body); |
| 42 | + commands.spawn_with(victory_menu).set_parent(ui_root.body); |
| 43 | +} |
| 44 | + |
| 45 | +pub fn detect_victory(level_query: Query<&Level>, endless_mode: Res<EndlessMode>) -> bool { |
| 46 | + if endless_mode.0 { |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + if let Some(level) = level_query.iter().last() { |
| 51 | + return level.current >= 10; |
| 52 | + } |
| 53 | + |
| 54 | + false |
| 55 | +} |
| 56 | + |
| 57 | +fn victory_overlay(mut entity: EntityWorldMut) { |
| 58 | + entity.add(widget::blocking_overlay).insert(( |
| 59 | + Name::new("VictoryOverlay"), |
| 60 | + ZIndex::Global(1), |
| 61 | + ThemeColor::Overlay.target::<BackgroundColor>(), |
| 62 | + StateScope::<PlayingMenu>::default(), |
| 63 | + )); |
| 64 | +} |
| 65 | + |
| 66 | +fn victory_menu(mut entity: EntityWorldMut) { |
| 67 | + entity |
| 68 | + .insert(( |
| 69 | + Name::new("VictoryMenu"), |
| 70 | + NodeBundle { |
| 71 | + style: Style::ABS_COLUMN_MID, |
| 72 | + z_index: ZIndex::Global(2), |
| 73 | + ..default() |
| 74 | + }, |
| 75 | + StateScope::<PlayingMenu>::default(), |
| 76 | + )) |
| 77 | + .with_children(|children| { |
| 78 | + children.spawn_with(header); |
| 79 | + children.spawn_with(button_container); |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +const HEADER: &str = "Victory :)"; |
| 84 | + |
| 85 | +fn header(mut entity: EntityWorldMut) { |
| 86 | + entity.insert(( |
| 87 | + Name::new("Header"), |
| 88 | + TextBundle::from_section( |
| 89 | + HEADER, |
| 90 | + TextStyle { |
| 91 | + font: BOLD_FONT_HANDLE, |
| 92 | + ..default() |
| 93 | + }, |
| 94 | + ) |
| 95 | + .with_style(Style { |
| 96 | + margin: UiRect::top(Vw(4.5)), |
| 97 | + ..default() |
| 98 | + }), |
| 99 | + DynamicFontSize::new(Vw(5.0)).with_step(8.0), |
| 100 | + ThemeColorForText(vec![ThemeColor::BodyText]), |
| 101 | + )); |
| 102 | +} |
| 103 | + |
| 104 | +fn button_container(mut entity: EntityWorldMut) { |
| 105 | + entity |
| 106 | + .insert(( |
| 107 | + Name::new("ButtonContainer"), |
| 108 | + NodeBundle { |
| 109 | + style: Style { |
| 110 | + align_items: AlignItems::Center, |
| 111 | + flex_direction: FlexDirection::Column, |
| 112 | + margin: UiRect::top(VMin(6.0)), |
| 113 | + row_gap: Vw(2.5), |
| 114 | + ..default() |
| 115 | + }, |
| 116 | + ..default() |
| 117 | + }, |
| 118 | + )) |
| 119 | + .with_children(|children| { |
| 120 | + children.spawn_with(keep_playing_button); |
| 121 | + children.spawn_with(restart_button); |
| 122 | + children.spawn_with(quit_to_title_button); |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +pub fn reset_endless_mode(mut endless_mode: ResMut<EndlessMode>) { |
| 127 | + endless_mode.0 = false; |
| 128 | +} |
| 129 | + |
| 130 | +fn keep_playing_button(mut entity: EntityWorldMut) { |
| 131 | + entity.add(widget::menu_button("Keep Playing")).insert(( |
| 132 | + On::<Pointer<Click>>::run( |
| 133 | + |mut endless_mode: ResMut<EndlessMode>, mut playing_menu: NextMut<PlayingMenu>| { |
| 134 | + endless_mode.0 = true; |
| 135 | + playing_menu.disable(); |
| 136 | + }, |
| 137 | + ), |
| 138 | + Style { |
| 139 | + height: Vw(9.0), |
| 140 | + width: Vw(38.0), |
| 141 | + align_items: AlignItems::Center, |
| 142 | + justify_content: JustifyContent::Center, |
| 143 | + ..default() |
| 144 | + }, |
| 145 | + )); |
| 146 | +} |
| 147 | + |
| 148 | +fn restart_button(mut entity: EntityWorldMut) { |
| 149 | + entity.add(widget::menu_button("Restart")).insert(( |
| 150 | + On::<Pointer<Click>>::run( |
| 151 | + |mut commands: Commands, audio: Res<Audio>, assets: Res<PlayingAssets>| { |
| 152 | + audio.play(assets.sfx_restart.clone()).with_volume(0.7); |
| 153 | + commands.spawn_with(fade_out(Screen::Playing)); |
| 154 | + }, |
| 155 | + ), |
| 156 | + Style { |
| 157 | + height: Vw(9.0), |
| 158 | + width: Vw(38.0), |
| 159 | + align_items: AlignItems::Center, |
| 160 | + justify_content: JustifyContent::Center, |
| 161 | + ..default() |
| 162 | + }, |
| 163 | + )); |
| 164 | +} |
| 165 | + |
| 166 | +fn quit_to_title_button(mut entity: EntityWorldMut) { |
| 167 | + entity.add(widget::menu_button("Quit to title")).insert(( |
| 168 | + On::<Pointer<Click>>::run(Screen::Title.enter()), |
| 169 | + Style { |
| 170 | + height: Vw(9.0), |
| 171 | + width: Vw(38.0), |
| 172 | + align_items: AlignItems::Center, |
| 173 | + justify_content: JustifyContent::Center, |
| 174 | + ..default() |
| 175 | + }, |
| 176 | + )); |
| 177 | +} |
0 commit comments