roguelike-game/src/gui/main_menu.rs

148 lines
4.6 KiB
Rust
Raw Normal View History

use ::bracket_lib::prelude::*;
2022-02-02 09:45:19 -05:00
use crate::rex_assets::RexAssets;
use crate::{colors, RunState, State};
#[derive(PartialEq, Copy, Clone)]
pub enum MainMenuSelection {
NewGame,
LoadGame,
Quit,
}
#[derive(PartialEq, Copy, Clone)]
pub enum MainMenuResult {
NoSelection { selected: MainMenuSelection },
Selected { selected: MainMenuSelection },
}
pub fn main_menu(gs: &mut State, ctx: &mut BTerm) -> MainMenuResult {
let mut draw_batch = DrawBatch::new();
2022-02-02 09:45:19 -05:00
let save_exists = crate::saveload_system::does_save_exist();
let runstate = gs.ecs.fetch::<RunState>();
let assets = gs.ecs.fetch::<RexAssets>();
ctx.render_xp_sprite(&assets.menu, 0, 0);
draw_batch.draw_double_box(
Rect::with_size(24, 18, 31, 10),
ColorPair::new(colors::WHEAT, colors::BLACK),
);
2022-02-02 09:45:19 -05:00
draw_batch.print_color_centered(
20,
"Rust Roguelike",
ColorPair::new(colors::YELLOW, colors::BLACK),
);
draw_batch.print_color_centered(
21,
"by Timothy J. Warren",
ColorPair::new(colors::CYAN, colors::BLACK),
);
draw_batch.print_color_centered(
2022-02-02 09:45:19 -05:00
22,
"Use Up/Down Arrows and Enter",
ColorPair::new(colors::GRAY, colors::BLACK),
2022-02-02 09:45:19 -05:00
);
let mut y = 24;
if let RunState::MainMenu {
menu_selection: selection,
} = *runstate
{
if selection == MainMenuSelection::NewGame {
draw_batch.print_color_centered(
y,
"Begin New Game",
ColorPair::new(colors::MAGENTA, colors::BLACK),
);
2022-02-02 09:45:19 -05:00
} else {
draw_batch.print_color_centered(
y,
"Begin New Game",
ColorPair::new(colors::WHITE, colors::BLACK),
);
2022-02-02 09:45:19 -05:00
}
y += 1;
if save_exists {
if selection == MainMenuSelection::LoadGame {
draw_batch.print_color_centered(
y,
"Load Game",
ColorPair::new(colors::MAGENTA, colors::BLACK),
);
2022-02-02 09:45:19 -05:00
} else {
draw_batch.print_color_centered(
y,
"Load Game",
ColorPair::new(colors::WHITE, colors::BLACK),
);
2022-02-02 09:45:19 -05:00
}
y += 1;
}
if selection == MainMenuSelection::Quit {
draw_batch.print_color_centered(
y,
"Quit",
ColorPair::new(colors::MAGENTA, colors::BLACK),
);
2022-02-02 09:45:19 -05:00
} else {
draw_batch.print_color_centered(
y,
"Quit",
ColorPair::new(colors::WHITE, colors::BLACK),
);
2022-02-02 09:45:19 -05:00
}
draw_batch.submit(6000).expect("Failed to render main menu");
2022-02-02 09:45:19 -05:00
return match ctx.key {
None => MainMenuResult::NoSelection {
selected: selection,
},
Some(key) => match key {
VirtualKeyCode::Escape => MainMenuResult::NoSelection {
selected: MainMenuSelection::Quit,
},
VirtualKeyCode::Up => MainMenuResult::NoSelection {
selected: match selection {
MainMenuSelection::NewGame => MainMenuSelection::Quit,
MainMenuSelection::LoadGame => MainMenuSelection::NewGame,
MainMenuSelection::Quit => {
if save_exists {
MainMenuSelection::LoadGame
} else {
MainMenuSelection::NewGame
}
}
},
},
VirtualKeyCode::Down => MainMenuResult::NoSelection {
selected: match selection {
MainMenuSelection::NewGame => {
if save_exists {
MainMenuSelection::LoadGame
} else {
MainMenuSelection::Quit
}
}
MainMenuSelection::LoadGame => MainMenuSelection::Quit,
MainMenuSelection::Quit => MainMenuSelection::NewGame,
},
},
VirtualKeyCode::Return => MainMenuResult::Selected {
selected: selection,
},
_ => MainMenuResult::NoSelection {
selected: selection,
},
},
};
}
MainMenuResult::NoSelection {
selected: MainMenuSelection::NewGame,
}
}