| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | use bevy::app::AppExit; |
| 8 | use bevy::ecs::hierarchy::ChildSpawnerCommands; |
| 9 | use bevy::picking::hover::Hovered; |
| 10 | use bevy::prelude::*; |
| 11 | use bevy::ui::Display; |
| 12 | use bevy::ui_widgets::{Activate, Button}; |
| 13 | |
| 14 | use crate::economy::advance_clock; |
| 15 | use crate::front::reset_game; |
| 16 | use crate::model::{SimClock, Tool}; |
| 17 | use crate::settings::{self, Settings, WindowModeSetting}; |
| 18 | |
| 19 | const PANEL: Color = Color::srgba(0.18, 0.2, 0.14, 0.97); |
| 20 | const OVERLAY: Color = Color::srgba(0.07, 0.08, 0.05, 0.9); |
| 21 | const PARCH: Color = Color::srgb(0.92, 0.88, 0.76); |
| 22 | const AMBER: Color = Color::srgb(0.95, 0.68, 0.2); |
| 23 | const BTN: Color = Color::srgba(0.27, 0.29, 0.21, 0.95); |
| 24 | const BTN_HOVER: Color = Color::srgba(0.35, 0.37, 0.27, 0.98); |
| 25 | |
| 26 | |
| 27 | const RESOLUTIONS: [[u32; 2]; 4] = [[1280, 720], [1600, 900], [1920, 1080], [2560, 1440]]; |
| 28 | const WINDOW_MODES: [WindowModeSetting; 3] = [ |
| 29 | WindowModeSetting::Windowed, |
| 30 | WindowModeSetting::Borderless, |
| 31 | WindowModeSetting::Fullscreen, |
| 32 | ]; |
| 33 | |
| 34 | const FPS_STEPS: [u32; 5] = [30, 60, 120, 144, 0]; |
| 35 | |
| 36 | const TEXT_SIZES: [f32; 8] = [0.8, 0.9, 1.0, 1.15, 1.3, 1.5, 1.75, 2.0]; |
| 37 | |
| 38 | const HOW_TO_PLAY: [&str; 10] = [ |
| 39 | "Goal: keep the three front sectors (right) supplied with Ammo, Food and Troops.", |
| 40 | "Home side: factories make Ammo, farms make Food, the capital and cities make Troops.", |
| 41 | "Supplied fronts push the red line east. Lose if any sector is pushed back to its limit.", |
| 42 | "Drag from one node to another to send its goods. Air raids can bomb track.", |
| 43 | "Track is laid through stations on the way, and a train is bought.", |
| 44 | "A train only carries what its stops load. Plan before you drag.", |
| 45 | "Bombed track is orange, or red with a gap. Click it (wrench mark) to repair for $150.", |
| 46 | "Cities go hungry without Food. Hungry cities slow war production everywhere.", |
| 47 | "Wounded go to a city or the capital (hospital). Hospital trains carry only wounded.", |
| 48 | "Space pauses, 1/2/3 set speed. Right-drag pans, wheel zooms. Esc opens menu. H or ? shows help.", |
| 49 | ]; |
| 50 | |
| 51 | #[derive(Clone, Copy, PartialEq, Eq, Debug, Default)] |
| 52 | pub enum MenuScreen { |
| 53 | #[default] |
| 54 | Main, |
| 55 | Pause, |
| 56 | Settings, |
| 57 | HowToPlay, |
| 58 | } |
| 59 | |
| 60 | |
| 61 | #[derive(Resource, Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 62 | pub struct Menu { |
| 63 | pub screen: Option<MenuScreen>, |
| 64 | |
| 65 | pub return_to: MenuScreen, |
| 66 | } |
| 67 | |
| 68 | impl Menu { |
| 69 | pub fn is_open(&self) -> bool { |
| 70 | self.screen.is_some() |
| 71 | } |
| 72 | |
| 73 | fn is_sub_screen(&self) -> bool { |
| 74 | matches!( |
| 75 | self.screen, |
| 76 | Some(MenuScreen::Settings | MenuScreen::HowToPlay) |
| 77 | ) |
| 78 | } |
| 79 | |
| 80 | fn show(&mut self, next: MenuScreen) { |
| 81 | if matches!(next, MenuScreen::Settings | MenuScreen::HowToPlay) && !self.is_sub_screen() { |
| 82 | self.return_to = self.screen.unwrap_or(MenuScreen::Main); |
| 83 | } |
| 84 | self.screen = Some(next); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | fn go_back(&mut self) { |
| 89 | self.screen = match self.screen { |
| 90 | Some(MenuScreen::Settings | MenuScreen::HowToPlay) => Some(self.return_to), |
| 91 | Some(MenuScreen::Pause) => None, |
| 92 | other => other, |
| 93 | }; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | pub struct MenuPlugin; |
| 98 | |
| 99 | impl Plugin for MenuPlugin { |
| 100 | fn build(&self, app: &mut App) { |
| 101 | app.add_observer(on_menu_action) |
| 102 | .add_systems(Startup, spawn_root) |
| 103 | .add_systems( |
| 104 | Update, |
| 105 | ( |
| 106 | menu_escape, |
| 107 | sync_pause.before(advance_clock), |
| 108 | render_menu, |
| 109 | update_menu_buttons, |
| 110 | ), |
| 111 | ); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | |
| 117 | fn menu_escape(keys: Res<ButtonInput<KeyCode>>, mut menu: ResMut<Menu>, mut tool: ResMut<Tool>) { |
| 118 | if !keys.just_pressed(KeyCode::Escape) { |
| 119 | return; |
| 120 | } |
| 121 | if menu.is_open() { |
| 122 | menu.go_back(); |
| 123 | } else if !matches!(*tool, Tool::Inspect) { |
| 124 | *tool = Tool::Inspect; |
| 125 | } else { |
| 126 | menu.show(MenuScreen::Pause); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | |
| 132 | fn sync_pause(menu: Res<Menu>, mut clock: ResMut<SimClock>, mut saved: Local<Option<u32>>) { |
| 133 | if menu.is_open() { |
| 134 | if saved.is_none() { |
| 135 | *saved = Some(clock.speed); |
| 136 | } |
| 137 | if clock.speed != 0 { |
| 138 | clock.speed = 0; |
| 139 | } |
| 140 | } else if let Some(speed) = saved.take() |
| 141 | && clock.speed == 0 |
| 142 | { |
| 143 | clock.speed = speed; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | #[derive(Component)] |
| 148 | struct MenuRoot; |
| 149 | |
| 150 | |
| 151 | #[derive(Component)] |
| 152 | struct MenuContent; |
| 153 | |
| 154 | |
| 155 | #[derive(Component, Clone, Copy)] |
| 156 | enum MenuAction { |
| 157 | NewOperation, |
| 158 | Resume, |
| 159 | Quit, |
| 160 | Go(MenuScreen), |
| 161 | Back, |
| 162 | Step(Setting, i32), |
| 163 | } |
| 164 | |
| 165 | |
| 166 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 167 | enum Setting { |
| 168 | Resolution, |
| 169 | WindowMode, |
| 170 | VSync, |
| 171 | FpsLimit, |
| 172 | ShowPerf, |
| 173 | TextSize, |
| 174 | Master, |
| 175 | Effects, |
| 176 | Ambient, |
| 177 | Mute, |
| 178 | } |
| 179 | |
| 180 | fn spawn_root(mut commands: Commands) { |
| 181 | commands.spawn(( |
| 182 | MenuRoot, |
| 183 | Node { |
| 184 | position_type: PositionType::Absolute, |
| 185 | left: Val::Px(0.0), |
| 186 | right: Val::Px(0.0), |
| 187 | top: Val::Px(0.0), |
| 188 | bottom: Val::Px(0.0), |
| 189 | justify_content: JustifyContent::Center, |
| 190 | align_items: AlignItems::Center, |
| 191 | display: Display::None, |
| 192 | ..default() |
| 193 | }, |
| 194 | BackgroundColor(OVERLAY), |
| 195 | GlobalZIndex(200), |
| 196 | children![( |
| 197 | MenuContent, |
| 198 | Node { |
| 199 | flex_direction: FlexDirection::Column, |
| 200 | align_items: AlignItems::Center, |
| 201 | row_gap: Val::Px(14.0), |
| 202 | padding: UiRect::all(Val::Px(28.0)), |
| 203 | border: UiRect::all(Val::Px(2.0)), |
| 204 | ..default() |
| 205 | }, |
| 206 | BackgroundColor(PANEL), |
| 207 | BorderColor::all(AMBER), |
| 208 | )], |
| 209 | )); |
| 210 | } |
| 211 | |
| 212 | fn text(value: &str, size: f32, color: Color) -> impl Bundle { |
| 213 | ( |
| 214 | Text::new(value), |
| 215 | TextFont::from_font_size(size), |
| 216 | TextColor(color), |
| 217 | ) |
| 218 | } |
| 219 | |
| 220 | fn menu_button(parent: &mut ChildSpawnerCommands, label: &str, width: f32, action: MenuAction) { |
| 221 | parent |
| 222 | .spawn(( |
| 223 | Button, |
| 224 | Node { |
| 225 | width: Val::Px(width), |
| 226 | height: Val::Px(36.0), |
| 227 | align_items: AlignItems::Center, |
| 228 | justify_content: JustifyContent::Center, |
| 229 | ..default() |
| 230 | }, |
| 231 | BackgroundColor(BTN), |
| 232 | Hovered::default(), |
| 233 | action, |
| 234 | )) |
| 235 | .with_children(|b| { |
| 236 | b.spawn(text(label, 18.0, PARCH)); |
| 237 | }); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | fn render_menu( |
| 242 | mut commands: Commands, |
| 243 | menu: Res<Menu>, |
| 244 | settings: Res<Settings>, |
| 245 | mut roots: Query<&mut Node, With<MenuRoot>>, |
| 246 | contents: Query<Entity, With<MenuContent>>, |
| 247 | ) { |
| 248 | let target = if menu.is_open() { |
| 249 | Display::Flex |
| 250 | } else { |
| 251 | Display::None |
| 252 | }; |
| 253 | for mut node in &mut roots { |
| 254 | if node.display != target { |
| 255 | node.display = target; |
| 256 | } |
| 257 | } |
| 258 | if !(menu.is_changed() || settings.is_changed()) { |
| 259 | return; |
| 260 | } |
| 261 | let Ok(content) = contents.single() else { |
| 262 | return; |
| 263 | }; |
| 264 | let mut entity = commands.entity(content); |
| 265 | entity.despawn_related::<Children>(); |
| 266 | let Some(screen) = menu.screen else { |
| 267 | return; |
| 268 | }; |
| 269 | entity.with_children(|p| match screen { |
| 270 | MenuScreen::Main => main_screen(p), |
| 271 | MenuScreen::Pause => pause_screen(p), |
| 272 | MenuScreen::HowToPlay => how_to_play_screen(p), |
| 273 | MenuScreen::Settings => settings_screen(p, &settings), |
| 274 | }); |
| 275 | } |
| 276 | |
| 277 | fn main_screen(p: &mut ChildSpawnerCommands) { |
| 278 | p.spawn(text("IRON LIFELINE", 48.0, AMBER)); |
| 279 | p.spawn((text("Keep the front alive.", 20.0, PARCH), Node { |
| 280 | margin: UiRect::bottom(Val::Px(12.0)), |
| 281 | ..default() |
| 282 | })); |
| 283 | menu_button(p, "New Operation", 280.0, MenuAction::NewOperation); |
| 284 | menu_button(p, "How to Play", 280.0, MenuAction::Go(MenuScreen::HowToPlay)); |
| 285 | menu_button(p, "Settings", 280.0, MenuAction::Go(MenuScreen::Settings)); |
| 286 | menu_button(p, "Quit", 280.0, MenuAction::Quit); |
| 287 | } |
| 288 | |
| 289 | fn pause_screen(p: &mut ChildSpawnerCommands) { |
| 290 | p.spawn(text("PAUSED", 36.0, AMBER)); |
| 291 | menu_button(p, "Resume", 280.0, MenuAction::Resume); |
| 292 | menu_button(p, "How to Play", 280.0, MenuAction::Go(MenuScreen::HowToPlay)); |
| 293 | menu_button(p, "Settings", 280.0, MenuAction::Go(MenuScreen::Settings)); |
| 294 | menu_button(p, "Main Menu", 280.0, MenuAction::Go(MenuScreen::Main)); |
| 295 | menu_button(p, "Quit", 280.0, MenuAction::Quit); |
| 296 | } |
| 297 | |
| 298 | fn how_to_play_screen(p: &mut ChildSpawnerCommands) { |
| 299 | p.spawn(text("HOW TO PLAY", 30.0, AMBER)); |
| 300 | p.spawn(Node { |
| 301 | flex_direction: FlexDirection::Column, |
| 302 | row_gap: Val::Px(7.0), |
| 303 | width: Val::Px(760.0), |
| 304 | ..default() |
| 305 | }) |
| 306 | .with_children(|lines| { |
| 307 | for line in HOW_TO_PLAY { |
| 308 | lines.spawn(text(line, 15.0, PARCH)); |
| 309 | } |
| 310 | }); |
| 311 | menu_button(p, "Back", 280.0, MenuAction::Back); |
| 312 | } |
| 313 | |
| 314 | fn settings_screen(p: &mut ChildSpawnerCommands, settings: &Settings) { |
| 315 | let d = &settings.display; |
| 316 | let a = &settings.audio; |
| 317 | p.spawn(text("SETTINGS", 30.0, AMBER)); |
| 318 | let rows = [ |
| 319 | ("Resolution", resolution_text(d.resolution), Setting::Resolution), |
| 320 | ("Window mode", window_mode_text(d.window_mode), Setting::WindowMode), |
| 321 | ("VSync", on_off(d.vsync), Setting::VSync), |
| 322 | ("FPS limit", fps_text(d.fps_limit), Setting::FpsLimit), |
| 323 | ("Show performance", on_off(d.show_performance), Setting::ShowPerf), |
| 324 | ("Text size", percent(d.text_size), Setting::TextSize), |
| 325 | ("Master volume", percent(a.master), Setting::Master), |
| 326 | ("Effects volume", percent(a.effects), Setting::Effects), |
| 327 | ("Ambient volume", percent(a.ambient), Setting::Ambient), |
| 328 | ("Mute", on_off(a.muted), Setting::Mute), |
| 329 | ]; |
| 330 | for (name, value, which) in rows { |
| 331 | setting_row(p, name, &value, which); |
| 332 | } |
| 333 | menu_button(p, "Back", 280.0, MenuAction::Back); |
| 334 | } |
| 335 | |
| 336 | |
| 337 | fn setting_row(p: &mut ChildSpawnerCommands, name: &str, value: &str, which: Setting) { |
| 338 | p.spawn(Node { |
| 339 | width: Val::Px(520.0), |
| 340 | justify_content: JustifyContent::SpaceBetween, |
| 341 | align_items: AlignItems::Center, |
| 342 | ..default() |
| 343 | }) |
| 344 | .with_children(|r| { |
| 345 | r.spawn(( |
| 346 | text(name, 17.0, PARCH), |
| 347 | Node { |
| 348 | width: Val::Px(210.0), |
| 349 | ..default() |
| 350 | }, |
| 351 | )); |
| 352 | r.spawn(Node { |
| 353 | align_items: AlignItems::Center, |
| 354 | column_gap: Val::Px(8.0), |
| 355 | ..default() |
| 356 | }) |
| 357 | .with_children(|c| { |
| 358 | menu_button(c, "<", 36.0, MenuAction::Step(which, -1)); |
| 359 | c.spawn(( |
| 360 | text(value, 17.0, AMBER), |
| 361 | Node { |
| 362 | width: Val::Px(180.0), |
| 363 | justify_content: JustifyContent::Center, |
| 364 | ..default() |
| 365 | }, |
| 366 | )); |
| 367 | menu_button(c, ">", 36.0, MenuAction::Step(which, 1)); |
| 368 | }); |
| 369 | }); |
| 370 | } |
| 371 | |
| 372 | fn on_off(on: bool) -> String { |
| 373 | if on { "On" } else { "Off" }.to_string() |
| 374 | } |
| 375 | |
| 376 | fn percent(v: f32) -> String { |
| 377 | format!("{}%", (v * 100.0).round() as i32) |
| 378 | } |
| 379 | |
| 380 | fn resolution_text(r: [u32; 2]) -> String { |
| 381 | format!("{}x{}", r[0], r[1]) |
| 382 | } |
| 383 | |
| 384 | fn window_mode_text(mode: WindowModeSetting) -> String { |
| 385 | match mode { |
| 386 | WindowModeSetting::Windowed => "Windowed", |
| 387 | WindowModeSetting::Borderless => "Borderless", |
| 388 | WindowModeSetting::Fullscreen => "Fullscreen", |
| 389 | } |
| 390 | .to_string() |
| 391 | } |
| 392 | |
| 393 | fn fps_text(fps: u32) -> String { |
| 394 | if fps == 0 { |
| 395 | "Unlimited".to_string() |
| 396 | } else { |
| 397 | fps.to_string() |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | |
| 402 | fn index_of<T: PartialEq>(list: &[T], value: &T) -> usize { |
| 403 | list.iter().position(|v| v == value).unwrap_or(0) |
| 404 | } |
| 405 | |
| 406 | |
| 407 | fn step_index(index: usize, len: usize, dir: i32) -> usize { |
| 408 | (index as i32 + dir).clamp(0, len as i32 - 1) as usize |
| 409 | } |
| 410 | |
| 411 | |
| 412 | fn step_volume(v: f32, dir: i32) -> f32 { |
| 413 | let tenths = (v * 10.0).round() as i32; |
| 414 | (tenths + dir).clamp(0, 10) as f32 / 10.0 |
| 415 | } |
| 416 | |
| 417 | fn step(settings: &mut Settings, which: Setting, dir: i32) { |
| 418 | let d = &mut settings.display; |
| 419 | let a = &mut settings.audio; |
| 420 | match which { |
| 421 | Setting::Resolution => { |
| 422 | let i = index_of(&RESOLUTIONS[..], &d.resolution); |
| 423 | d.resolution = RESOLUTIONS[step_index(i, RESOLUTIONS.len(), dir)]; |
| 424 | } |
| 425 | Setting::WindowMode => { |
| 426 | let i = index_of(&WINDOW_MODES[..], &d.window_mode); |
| 427 | d.window_mode = WINDOW_MODES[step_index(i, WINDOW_MODES.len(), dir)]; |
| 428 | } |
| 429 | Setting::VSync => d.vsync = !d.vsync, |
| 430 | Setting::FpsLimit => { |
| 431 | let i = index_of(&FPS_STEPS[..], &d.fps_limit); |
| 432 | d.fps_limit = FPS_STEPS[step_index(i, FPS_STEPS.len(), dir)]; |
| 433 | } |
| 434 | Setting::ShowPerf => d.show_performance = !d.show_performance, |
| 435 | Setting::TextSize => { |
| 436 | let i = index_of(&TEXT_SIZES[..], &d.text_size); |
| 437 | d.text_size = TEXT_SIZES[step_index(i, TEXT_SIZES.len(), dir)]; |
| 438 | } |
| 439 | Setting::Master => a.master = step_volume(a.master, dir), |
| 440 | Setting::Effects => a.effects = step_volume(a.effects, dir), |
| 441 | Setting::Ambient => a.ambient = step_volume(a.ambient, dir), |
| 442 | Setting::Mute => a.muted = !a.muted, |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | fn on_menu_action( |
| 447 | ev: On<Activate>, |
| 448 | actions: Query<&MenuAction>, |
| 449 | mut menu: ResMut<Menu>, |
| 450 | mut settings: ResMut<Settings>, |
| 451 | mut exit: MessageWriter<AppExit>, |
| 452 | mut commands: Commands, |
| 453 | ) { |
| 454 | let Ok(action) = actions.get(ev.entity) else { |
| 455 | return; |
| 456 | }; |
| 457 | match *action { |
| 458 | MenuAction::NewOperation => { |
| 459 | menu.screen = None; |
| 460 | commands.queue(|world: &mut World| reset_game(world)); |
| 461 | } |
| 462 | MenuAction::Resume => menu.screen = None, |
| 463 | MenuAction::Quit => { |
| 464 | exit.write(AppExit::Success); |
| 465 | } |
| 466 | MenuAction::Go(screen) => menu.show(screen), |
| 467 | MenuAction::Back => menu.go_back(), |
| 468 | MenuAction::Step(which, dir) => { |
| 469 | |
| 470 | let mut next = settings.clone(); |
| 471 | step(&mut next, which, dir); |
| 472 | if next != *settings { |
| 473 | *settings = next; |
| 474 | settings::save(&settings); |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | fn update_menu_buttons(mut buttons: Query<(&Hovered, &mut BackgroundColor), With<MenuAction>>) { |
| 481 | for (hovered, mut bg) in &mut buttons { |
| 482 | let target = if hovered.get() { BTN_HOVER } else { BTN }; |
| 483 | if bg.0 != target { |
| 484 | bg.0 = target; |
| 485 | } |
| 486 | } |
| 487 | } |