Add main menu

This commit is contained in:
Timothy Warren 2021-11-08 10:22:11 -05:00
parent 7d0200b262
commit 84934128d5
2 changed files with 176 additions and 26 deletions

View File

@ -1,9 +1,21 @@
use crate::{ use crate::components::{CombatStats, InBackpack, Name, Player, Position, Viewshed};
game_log::GameLog, CombatStats, InBackpack, Map, Name, Player, Position, State, Viewshed, use crate::{game_log::GameLog, Map, RunState, State};
};
use rltk::{Point, Rltk, VirtualKeyCode, RGB}; use rltk::{Point, Rltk, VirtualKeyCode, RGB};
use specs::prelude::*; use specs::prelude::*;
#[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 draw_ui(ecs: &World, ctx: &mut Rltk) { pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
ctx.draw_box( ctx.draw_box(
0, 0,
@ -395,3 +407,113 @@ pub fn ranged_target(
(ItemMenuResult::NoResponse, None) (ItemMenuResult::NoResponse, None)
} }
pub fn main_menu(gs: &mut State, ctx: &mut Rltk) -> MainMenuResult {
let runstate = gs.ecs.fetch::<RunState>();
ctx.print_color_centered(
15,
RGB::named(rltk::YELLOW),
RGB::named(rltk::BLACK),
"Rust Roguelike Tutorial",
);
if let RunState::MainMenu {
menu_selection: selection,
} = *runstate
{
if selection == MainMenuSelection::NewGame {
ctx.print_color_centered(
24,
RGB::named(rltk::MAGENTA),
RGB::named(rltk::BLACK),
"Begin New Game",
);
} else {
ctx.print_color_centered(
24,
RGB::named(rltk::WHITE),
RGB::named(rltk::BLACK),
"Begin New Game",
);
}
if selection == MainMenuSelection::LoadGame {
ctx.print_color_centered(
25,
RGB::named(rltk::MAGENTA),
RGB::named(rltk::BLACK),
"Load Game",
);
} else {
ctx.print_color_centered(
25,
RGB::named(rltk::WHITE),
RGB::named(rltk::BLACK),
"Load Game",
);
}
if selection == MainMenuSelection::Quit {
ctx.print_color_centered(
26,
RGB::named(rltk::MAGENTA),
RGB::named(rltk::BLACK),
"Quit",
);
} else {
ctx.print_color_centered(26, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), "Quit");
}
match ctx.key {
None => {
return MainMenuResult::NoSelection {
selected: selection,
}
}
Some(key) => match key {
VirtualKeyCode::Escape => {
return MainMenuResult::NoSelection {
selected: selection,
}
}
VirtualKeyCode::Up => {
let newselection;
match selection {
MainMenuSelection::NewGame => newselection = MainMenuSelection::Quit,
MainMenuSelection::LoadGame => newselection = MainMenuSelection::NewGame,
MainMenuSelection::Quit => newselection = MainMenuSelection::LoadGame,
}
return MainMenuResult::NoSelection {
selected: newselection,
};
}
VirtualKeyCode::Down => {
let newselection;
match selection {
MainMenuSelection::NewGame => newselection = MainMenuSelection::LoadGame,
MainMenuSelection::LoadGame => newselection = MainMenuSelection::Quit,
MainMenuSelection::Quit => newselection = MainMenuSelection::NewGame,
}
return MainMenuResult::NoSelection {
selected: newselection,
};
}
VirtualKeyCode::Return => {
return MainMenuResult::Selected {
selected: selection,
}
}
_ => {
return MainMenuResult::NoSelection {
selected: selection,
}
}
},
}
}
MainMenuResult::NoSelection {
selected: MainMenuSelection::NewGame,
}
}

View File

@ -48,7 +48,13 @@ pub enum RunState {
MonsterTurn, MonsterTurn,
ShowInventory, ShowInventory,
ShowDropItem, ShowDropItem,
ShowTargeting { range: i32, item: Entity }, ShowTargeting {
range: i32,
item: Entity,
},
MainMenu {
menu_selection: gui::MainMenuSelection,
},
} }
pub struct State { pub struct State {
@ -91,8 +97,17 @@ impl State {
impl GameState for State { impl GameState for State {
fn tick(&mut self, ctx: &mut Rltk) { fn tick(&mut self, ctx: &mut Rltk) {
let mut newrunstate;
{
let runstate = self.ecs.fetch::<RunState>();
newrunstate = *runstate;
}
ctx.cls(); ctx.cls();
match newrunstate {
RunState::MainMenu { .. } => {}
_ => {
// Draw the UI // Draw the UI
draw_map(&self.ecs, ctx); draw_map(&self.ecs, ctx);
{ {
@ -112,11 +127,7 @@ impl GameState for State {
gui::draw_ui(&self.ecs, ctx); gui::draw_ui(&self.ecs, ctx);
} }
}
let mut newrunstate;
{
let runstate = self.ecs.fetch::<RunState>();
newrunstate = *runstate;
} }
match newrunstate { match newrunstate {
@ -212,6 +223,23 @@ impl GameState for State {
} }
} }
} }
RunState::MainMenu { .. } => {
let result = gui::main_menu(self, ctx);
match result {
gui::MainMenuResult::NoSelection { selected } => {
newrunstate = RunState::MainMenu {
menu_selection: selected,
}
}
gui::MainMenuResult::Selected { selected } => match selected {
gui::MainMenuSelection::NewGame => newrunstate = RunState::PreRun,
gui::MainMenuSelection::LoadGame => newrunstate = RunState::PreRun,
gui::MainMenuSelection::Quit => {
::std::process::exit(0);
}
},
}
}
} }
{ {