Complete the first section

This commit is contained in:
Timothy Warren 2021-11-15 13:27:40 -05:00
parent 43e9ebd52b
commit 416af96be3
5 changed files with 106 additions and 7 deletions

View File

@ -1,5 +1,4 @@
use crate::{game_log::GameLog, CombatStats, Name, Player, SufferDamage}; use crate::{game_log::GameLog, CombatStats, Name, Player, RunState, SufferDamage};
use rltk::console;
use specs::prelude::*; use specs::prelude::*;
pub struct DamageSystem {} pub struct DamageSystem {}
@ -44,7 +43,10 @@ pub fn delete_the_dead(ecs: &mut World) {
dead.push(entity) dead.push(entity)
} }
Some(_) => console::log("You are dead"), Some(_) => {
let mut runstate = ecs.write_resource::<RunState>();
*runstate = RunState::GameOver;
}
} }
} }
} }

View File

@ -619,3 +619,42 @@ pub fn remove_item_menu(gs: &mut State, ctx: &mut Rltk) -> (ItemMenuResult, Opti
}, },
} }
} }
#[derive(PartialEq, Copy, Clone)]
pub enum GameOverResult {
NoSelection,
QuitToMenu,
}
pub fn game_over(ctx: &mut Rltk) -> GameOverResult {
ctx.print_color_centered(
15,
RGB::named(rltk::YELLOW),
RGB::named(rltk::BLACK),
"Your journey has ended!",
);
ctx.print_color_centered(
17,
RGB::named(rltk::WHITE),
RGB::named(rltk::BLACK),
"One day, we'll tell you all about how you did.",
);
ctx.print_color_centered(
18,
RGB::named(rltk::WHITE),
RGB::named(rltk::BLACK),
"That day, sadly, is not in this chapter...",
);
ctx.print_color_centered(
20,
RGB::named(rltk::MAGENTA),
RGB::named(rltk::BLACK),
"Press any key to return to the menu.",
);
match ctx.key {
None => GameOverResult::NoSelection,
Some(_) => GameOverResult::QuitToMenu,
}
}

View File

@ -327,7 +327,7 @@ impl<'a> System<'a> for ItemRemoveSystem {
Entities<'a>, Entities<'a>,
WriteStorage<'a, WantsToRemoveItem>, WriteStorage<'a, WantsToRemoveItem>,
WriteStorage<'a, Equipped>, WriteStorage<'a, Equipped>,
WriteStorage<'a, InBackpack> WriteStorage<'a, InBackpack>,
); );
fn run(&mut self, data: Self::SystemData) { fn run(&mut self, data: Self::SystemData) {
@ -335,7 +335,8 @@ impl<'a> System<'a> for ItemRemoveSystem {
for (entity, to_remove) in (&entities, &wants_remove).join() { for (entity, to_remove) in (&entities, &wants_remove).join() {
equipped.remove(to_remove.item); equipped.remove(to_remove.item);
backpack.insert(to_remove.item, InBackpack { owner: entity}) backpack
.insert(to_remove.item, InBackpack { owner: entity })
.expect("Unable to remove item and put in backpack"); .expect("Unable to remove item and put in backpack");
} }

View File

@ -19,6 +19,7 @@ pub mod saveload_system;
mod spawner; mod spawner;
mod visibility_system; mod visibility_system;
use crate::inventory_system::ItemRemoveSystem;
use components::*; use components::*;
use damage_system::DamageSystem; use damage_system::DamageSystem;
pub use game_log::GameLog; pub use game_log::GameLog;
@ -30,7 +31,6 @@ use monster_ai_system::MonsterAI;
use player::*; use player::*;
pub use rect::Rect; pub use rect::Rect;
use visibility_system::VisibilitySystem; use visibility_system::VisibilitySystem;
use crate::inventory_system::ItemRemoveSystem;
/// Cut down on the amount of syntax to register components /// Cut down on the amount of syntax to register components
macro_rules! register { macro_rules! register {
@ -63,6 +63,7 @@ pub enum RunState {
SaveGame, SaveGame,
NextLevel, NextLevel,
ShowRemoveItem, ShowRemoveItem,
GameOver,
} }
pub struct State { pub struct State {
@ -282,6 +283,15 @@ impl GameState for State {
} }
} }
} }
RunState::GameOver => match gui::game_over(ctx) {
gui::GameOverResult::NoSelection => {}
gui::GameOverResult::QuitToMenu => {
self.game_over_cleanup();
newrunstate = RunState::MainMenu {
menu_selection: gui::MainMenuSelection::NewGame,
};
}
},
} }
{ {
@ -389,6 +399,53 @@ impl State {
player_health.hp = i32::max(player_health.hp, player_health.max_hp / 2); player_health.hp = i32::max(player_health.hp, player_health.max_hp / 2);
} }
} }
fn game_over_cleanup(&mut self) {
// Delete everything
let mut to_delete = Vec::new();
for e in self.ecs.entities().join() {
to_delete.push(e);
}
for del in to_delete.iter() {
self.ecs
.delete_entity(*del)
.expect("Failed to delete entity");
}
// Build a new map and place the player
let worldmap;
{
let mut worldmap_resource = self.ecs.write_resource::<Map>();
*worldmap_resource = Map::new_map_rooms_and_corridors(1);
worldmap = worldmap_resource.clone();
}
// Spawn bad guys
for room in worldmap.rooms.iter().skip(1) {
spawner::spawn_room(&mut self.ecs, room, 1);
}
// Place the player and update resources
let (player_x, player_y) = worldmap.rooms[0].center();
let player_entity = spawner::player(&mut self.ecs, player_x, player_y);
let mut player_position = self.ecs.write_resource::<Point>();
*player_position = Point::new(player_x, player_y);
let mut position_components = self.ecs.write_storage::<Position>();
let mut player_entity_writer = self.ecs.write_resource::<Entity>();
*player_entity_writer = player_entity;
let player_pos_comp = position_components.get_mut(player_entity);
if let Some(player_pos_comp) = player_pos_comp {
player_pos_comp.x = player_x;
player_pos_comp.y = player_y;
}
// Mark the player's visibility as dirty
let mut viewshed_components = self.ecs.write_storage::<Viewshed>();
let vs = viewshed_components.get_mut(player_entity);
if let Some(vs) = vs {
vs.dirty = true;
}
}
} }
fn main() -> rltk::BError { fn main() -> rltk::BError {