Add GameOver turn state and screen
This commit is contained in:
parent
8b763db714
commit
a44c3466e0
42
src/main.rs
42
src/main.rs
@ -62,6 +62,47 @@ impl State {
|
||||
monster_systems: build_monster_scheduler(),
|
||||
}
|
||||
}
|
||||
|
||||
fn game_over(&mut self, ctx: &mut BTerm) {
|
||||
ctx.set_active_console(2);
|
||||
ctx.print_color_centered(2, RED, BLACK, "Your quest has ended.");
|
||||
ctx.print_color_centered(
|
||||
4,
|
||||
WHITE,
|
||||
BLACK,
|
||||
"Slain by a monster, your hero's journey has come to a premature end.",
|
||||
);
|
||||
ctx.print_color_centered(
|
||||
5,
|
||||
WHITE,
|
||||
BLACK,
|
||||
"The Amulet of Yala remains unclaimed, and your home town is not saved.",
|
||||
);
|
||||
ctx.print_color_centered(
|
||||
8,
|
||||
YELLOW,
|
||||
BLACK,
|
||||
"Don't worry, you can always try again with a new hero.",
|
||||
);
|
||||
ctx.print_color_centered(9, GREEN, BLACK, "Press 1 to play again.");
|
||||
|
||||
if let Some(VirtualKeyCode::Key1) = ctx.key {
|
||||
self.ecs = World::default();
|
||||
self.resources = Resources::default();
|
||||
let mut rng = RandomNumberGenerator::new();
|
||||
let map_builder = MapBuilder::new(&mut rng);
|
||||
spawn_player(&mut self.ecs, map_builder.player_start);
|
||||
map_builder
|
||||
.rooms
|
||||
.iter()
|
||||
.skip(1)
|
||||
.map(|r| r.center())
|
||||
.for_each(|pos| spawn_monster(&mut self.ecs, &mut rng, pos));
|
||||
self.resources.insert(map_builder.map);
|
||||
self.resources.insert(Camera::new(map_builder.player_start));
|
||||
self.resources.insert(TurnState::AwaitingInput);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GameState for State {
|
||||
@ -89,6 +130,7 @@ impl GameState for State {
|
||||
TurnState::MonsterTurn => self
|
||||
.monster_systems
|
||||
.execute(&mut self.ecs, &mut self.resources),
|
||||
TurnState::GameOver => self.game_over(ctx),
|
||||
}
|
||||
|
||||
render_draw_buffer(ctx).expect("Render error");
|
||||
|
@ -2,6 +2,7 @@ use crate::prelude::*;
|
||||
|
||||
#[system]
|
||||
#[read_component(WantsToAttack)]
|
||||
#[read_component(Player)]
|
||||
#[write_component(Health)]
|
||||
pub fn combat(ecs: &mut SubWorld, commands: &mut CommandBuffer) {
|
||||
let mut attackers = <(Entity, &WantsToAttack)>::query();
|
||||
|
@ -1,12 +1,23 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
#[system]
|
||||
pub fn end_turn(#[resource] turn_state: &mut TurnState) {
|
||||
let new_state = match turn_state {
|
||||
#[read_component(Health)]
|
||||
#[read_component(Player)]
|
||||
pub fn end_turn(ecs: &SubWorld, #[resource] turn_state: &mut TurnState) {
|
||||
let mut player_hp = <&Health>::query().filter(component::<Player>());
|
||||
let current_state = *turn_state;
|
||||
let mut new_state = match current_state {
|
||||
TurnState::AwaitingInput => return,
|
||||
TurnState::PlayerTurn => TurnState::MonsterTurn,
|
||||
TurnState::MonsterTurn => TurnState::AwaitingInput,
|
||||
_ => current_state,
|
||||
};
|
||||
|
||||
player_hp.iter(ecs).for_each(|hp| {
|
||||
if hp.current < 1 {
|
||||
new_state = TurnState::GameOver;
|
||||
}
|
||||
});
|
||||
|
||||
*turn_state = new_state;
|
||||
}
|
||||
|
@ -3,4 +3,5 @@ pub enum TurnState {
|
||||
AwaitingInput,
|
||||
PlayerTurn,
|
||||
MonsterTurn,
|
||||
GameOver,
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user