add 3 distinct outcomes for the game
This commit is contained in:
39
src/game.rs
39
src/game.rs
@@ -3,7 +3,19 @@ use crate::level_generator::LevelGenerator;
|
||||
use crate::player::Player;
|
||||
use crate::position::Position;
|
||||
|
||||
pub const LEVELS: usize = 2;
|
||||
pub const LEVELS: usize = 10;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
/// represents a state of a game
|
||||
pub enum GameState {
|
||||
/// the game is ongoing (neither won or lost)
|
||||
RUNNING,
|
||||
/// the player died
|
||||
LOST,
|
||||
/// the player reached the Ω
|
||||
WON,
|
||||
}
|
||||
|
||||
|
||||
/// the main structure to hold all information about the ongoing game
|
||||
pub struct Game {
|
||||
@@ -31,6 +43,31 @@ impl Game {
|
||||
g
|
||||
}
|
||||
|
||||
/// returns true if the player is dead (life <= 0)
|
||||
fn player_is_dead(&self) -> bool { self.get_player().get_life() <= 0 }
|
||||
/// returns true if the player is standing on the End element
|
||||
fn player_reached_goal(&mut self) -> bool {
|
||||
match self.next_element(0, 0) {
|
||||
None => {}
|
||||
Some(a) => {
|
||||
match a {
|
||||
StructureElement::End => { return true; }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
};
|
||||
false
|
||||
}
|
||||
/// returns the state of the game (depending on player's life and position)
|
||||
pub fn get_game_state(&mut self) -> GameState {
|
||||
if self.player_is_dead() {
|
||||
return GameState::LOST;
|
||||
}
|
||||
if self.player_reached_goal() {
|
||||
return GameState::WON;
|
||||
}
|
||||
GameState::RUNNING
|
||||
}
|
||||
pub fn get_player(&self) -> &Player {
|
||||
&self.player
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user