saveload system

localstorage isn't supported by wasm, so playing online will probably just not have save games for a while
This commit is contained in:
Llywelwyn 2023-07-09 12:09:30 +01:00
parent dd91a8cca7
commit 51060f1a85
11 changed files with 290 additions and 63 deletions

View file

@ -273,34 +273,40 @@ pub enum MainMenuResult {
}
pub fn main_menu(gs: &mut State, ctx: &mut Rltk) -> MainMenuResult {
let save_exists = super::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);
ctx.print_color(38, 21, RGB::named(rltk::GREEN), RGB::from_f32(0.11, 0.11, 0.11), "RUST-RL");
ctx.print_color(40, 21, RGB::named(rltk::GREEN), RGB::from_f32(0.11, 0.11, 0.11), "RUST-RL");
if let RunState::MainMenu { menu_selection: selection } = *runstate {
let mut y = 24;
if selection == MainMenuSelection::NewGame {
ctx.print_color(34, 24, RGB::named(rltk::YELLOW), RGB::from_f32(0.11, 0.11, 0.11), "[");
ctx.print_color(36, 24, RGB::named(rltk::GREEN), RGB::from_f32(0.11, 0.11, 0.11), "new game");
ctx.print_color(45, 24, RGB::named(rltk::YELLOW), RGB::from_f32(0.11, 0.11, 0.11), "]");
ctx.print_color(37, 24, RGB::named(rltk::YELLOW), RGB::from_f32(0.11, 0.11, 0.11), "[");
ctx.print_color(39, 24, RGB::named(rltk::GREEN), RGB::from_f32(0.11, 0.11, 0.11), "new game");
ctx.print_color(48, 24, RGB::named(rltk::YELLOW), RGB::from_f32(0.11, 0.11, 0.11), "]");
} else {
ctx.print_color(36, 24, RGB::named(rltk::WHITE), RGB::from_f32(0.11, 0.11, 0.11), "new game");
ctx.print_color(39, 24, RGB::named(rltk::WHITE), RGB::from_f32(0.11, 0.11, 0.11), "new game");
}
if selection == MainMenuSelection::LoadGame {
ctx.print_color(38, 26, RGB::named(rltk::YELLOW), RGB::from_f32(0.11, 0.11, 0.11), "[");
ctx.print_color(40, 26, RGB::named(rltk::GREEN), RGB::from_f32(0.11, 0.11, 0.11), "load game");
ctx.print_color(50, 26, RGB::named(rltk::YELLOW), RGB::from_f32(0.11, 0.11, 0.11), "]");
} else {
ctx.print_color(40, 26, RGB::named(rltk::WHITE), RGB::from_f32(0.11, 0.11, 0.11), "load game");
y += 2;
if save_exists {
if selection == MainMenuSelection::LoadGame {
ctx.print_color(36, y, RGB::named(rltk::YELLOW), RGB::from_f32(0.11, 0.11, 0.11), "[");
ctx.print_color(38, y, RGB::named(rltk::GREEN), RGB::from_f32(0.11, 0.11, 0.11), "load game");
ctx.print_color(48, y, RGB::named(rltk::YELLOW), RGB::from_f32(0.11, 0.11, 0.11), "]");
} else {
ctx.print_color(38, y, RGB::named(rltk::WHITE), RGB::from_f32(0.11, 0.11, 0.11), "load game");
}
y += 2;
}
if selection == MainMenuSelection::Quit {
ctx.print_color(34, 28, RGB::named(rltk::YELLOW), RGB::from_f32(0.11, 0.11, 0.11), "[");
ctx.print_color(36, 28, RGB::named(rltk::GREEN), RGB::from_f32(0.11, 0.11, 0.11), "goodbye!");
ctx.print_color(45, 28, RGB::named(rltk::YELLOW), RGB::from_f32(0.11, 0.11, 0.11), "]");
ctx.print_color(37, y, RGB::named(rltk::YELLOW), RGB::from_f32(0.11, 0.11, 0.11), "[");
ctx.print_color(39, y, RGB::named(rltk::GREEN), RGB::from_f32(0.11, 0.11, 0.11), "goodbye!");
ctx.print_color(48, y, RGB::named(rltk::YELLOW), RGB::from_f32(0.11, 0.11, 0.11), "]");
} else {
ctx.print_color(36, 28, RGB::named(rltk::WHITE), RGB::from_f32(0.11, 0.11, 0.11), "quit");
ctx.print_color(43, y, RGB::named(rltk::WHITE), RGB::from_f32(0.11, 0.11, 0.11), "quit");
}
match ctx.key {
@ -312,21 +318,27 @@ pub fn main_menu(gs: &mut State, ctx: &mut Rltk) -> MainMenuResult {
VirtualKeyCode::N => return MainMenuResult::NoSelection { selected: MainMenuSelection::NewGame },
VirtualKeyCode::L => return MainMenuResult::NoSelection { selected: MainMenuSelection::LoadGame },
VirtualKeyCode::Up => {
let new_selection;
let mut new_selection;
match selection {
MainMenuSelection::NewGame => new_selection = MainMenuSelection::Quit,
MainMenuSelection::LoadGame => new_selection = MainMenuSelection::NewGame,
MainMenuSelection::Quit => new_selection = MainMenuSelection::LoadGame,
}
if new_selection == MainMenuSelection::LoadGame && !save_exists {
new_selection = MainMenuSelection::NewGame;
}
return MainMenuResult::NoSelection { selected: new_selection };
}
VirtualKeyCode::Down => {
let new_selection;
let mut new_selection;
match selection {
MainMenuSelection::NewGame => new_selection = MainMenuSelection::LoadGame,
MainMenuSelection::LoadGame => new_selection = MainMenuSelection::Quit,
MainMenuSelection::Quit => new_selection = MainMenuSelection::NewGame,
}
if new_selection == MainMenuSelection::LoadGame && !save_exists {
new_selection = MainMenuSelection::Quit;
}
return MainMenuResult::NoSelection { selected: new_selection };
}
VirtualKeyCode::Return => return MainMenuResult::Selected { selected: selection },