233 lines
8.1 KiB
Rust
233 lines
8.1 KiB
Rust
use crate::level::{Level, StructureElement};
|
|
use crate::level_generator::LevelGenerator;
|
|
use crate::player::Player;
|
|
use crate::position::{HasPosition, Position};
|
|
|
|
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 {
|
|
/// the player
|
|
player: Player,
|
|
/// the levels of the game
|
|
levels: Vec<Level>,
|
|
}
|
|
|
|
impl Game {
|
|
pub fn new(p: Player) -> Game {
|
|
let mut v: Vec<Level> = Vec::with_capacity(LEVELS);
|
|
for d in 0..LEVELS {
|
|
v.push(LevelGenerator::generate(d, d == 0, d == LEVELS - 1).render());
|
|
}
|
|
let mut g = Game {
|
|
player: p,
|
|
levels: v,
|
|
};
|
|
let start = {
|
|
g.get_level(0).start
|
|
};
|
|
g.get_mutable_player().get_position().set(0, start.0, start.1);
|
|
g.get_mutable_level(0).discover(&Position::new(0, start.0, start.1));
|
|
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
|
|
}
|
|
pub fn get_mutable_player(&mut self) -> &mut Player {
|
|
&mut self.player
|
|
}
|
|
pub fn get_level(&self, level: usize) -> &Level { &self.levels[level] }
|
|
pub fn get_mutable_level(&mut self, level: usize) -> &mut Level { &mut self.levels[level] }
|
|
/// check if the player can move in the given direction. This only checks for structure element
|
|
/// limitation as walls.
|
|
fn can_move(&mut self, dx: i16, dy: i16) -> bool {
|
|
let player_pos = &self.player.get_position();
|
|
let new_x: i16 = player_pos.get_x() as i16 + dx;
|
|
let new_y: i16 = player_pos.get_y() as i16 + dy;
|
|
let level = &mut self.levels[player_pos.get_level()];
|
|
match level.get_element(new_x, new_y) {
|
|
(None, _, _) => { return false; }
|
|
(Some(t), _, _) => {
|
|
match t {
|
|
StructureElement::Wall => { return false; }
|
|
_ => {}
|
|
}
|
|
}
|
|
};
|
|
true
|
|
}
|
|
/// returns the position (as tuple) of the next level's start point.
|
|
fn next_start(&self) -> (usize, usize, usize) {
|
|
let level = self.player.get_immutable_position().get_level() + 1;
|
|
let (x, y) = self.levels[level].start;
|
|
(level, x, y)
|
|
}
|
|
/// returns the position (as tuple) of the previous level's end point.
|
|
fn prev_end(&self) -> (usize, usize, usize) {
|
|
let level = self.player.get_immutable_position().get_level() - 1;
|
|
let (x, y) = self.levels[level].end;
|
|
(level, x, y)
|
|
}
|
|
/// get the next structure element sen from the player's position into the given direction.
|
|
fn next_element(&mut self, dx: i16, dy: i16) -> Option<StructureElement> {
|
|
let player_pos = &self.player.get_position();
|
|
let new_x: i16 = player_pos.get_x() as i16 + dx;
|
|
let new_y: i16 = player_pos.get_y() as i16 + dy;
|
|
let player_level = player_pos.get_level();
|
|
let level = &mut self.levels[player_level];
|
|
level.get_element(new_x, new_y).0
|
|
}
|
|
|
|
/// move the game's player into the given direction. If on the target field there is a stair,
|
|
/// the player follows the stair up/down to the next level.
|
|
/// Discovery is performed on the new location of the player.
|
|
/// The returned value is the direction that will bring the player back to his original field.
|
|
pub fn move_player(&mut self, mut dx: i16, mut dy: i16) -> (i16, i16) {
|
|
// verify the player can do the move
|
|
if !self.can_move(dx, dy) {
|
|
return (-dx, -dy);
|
|
}
|
|
|
|
let player_pos = &self.player.get_position();
|
|
let mut player_level = player_pos.get_level();
|
|
let structure = self.next_element(dx, dy);
|
|
|
|
// use stairs if walked onto one
|
|
match structure {
|
|
Some(e) => {
|
|
match e {
|
|
StructureElement::StairDown => {
|
|
(dx, dy) = (0, 0);
|
|
let (next_level, x, y) = self.next_start();
|
|
player_level = next_level;
|
|
self.get_mutable_player().get_position().set(next_level, x, y);
|
|
}
|
|
StructureElement::StairUp => {
|
|
(dx, dy) = (0, 0);
|
|
let (next_level, x, y) = self.prev_end();
|
|
player_level = next_level;
|
|
self.get_mutable_player().get_position().set(next_level, x, y);
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
|
|
// update position and discover area on new position
|
|
let (new_x, new_y) = self.get_mutable_player().get_position().change(dx, dy);
|
|
self.get_mutable_level(player_level).discover(&Position::new(player_level, new_x, new_y));
|
|
(-dx, -dy)
|
|
}
|
|
pub fn player_fights_monster(&mut self) -> bool {
|
|
let player_pos = &self.player.get_immutable_position();
|
|
let player_level = player_pos.get_level();
|
|
let level = &mut self.levels[player_level];
|
|
let (_, m, _) = level.get_element(player_pos.get_x() as i16, player_pos.get_y() as i16);
|
|
match m {
|
|
None => {}
|
|
Some(m) => {
|
|
// TODO fight the monster
|
|
self.player.change_life(-1);
|
|
m.decrease_life(1);
|
|
// monster died, player gains experience
|
|
if m.is_dead() {
|
|
self.player.gain_experience(m.get_experience_gain());
|
|
}
|
|
return m.is_dead();
|
|
}
|
|
};
|
|
true
|
|
}
|
|
pub fn player_collects_artifact(&mut self) {
|
|
let player_pos = &self.player.get_immutable_position();
|
|
let player_level = player_pos.get_level();
|
|
let level = &mut self.levels[player_level];
|
|
let (_, _, a) = level.get_element(player_pos.get_x() as i16, player_pos.get_y() as i16);
|
|
match a {
|
|
None => {}
|
|
Some(a) => {
|
|
a.collect(&mut self.player);
|
|
}
|
|
}
|
|
}
|
|
/// updates the player's current level. This will remove collected artifacts and dead monsters.
|
|
pub fn update_level(&mut self) {
|
|
let player_pos = &self.player.get_immutable_position();
|
|
let player_level = player_pos.get_level();
|
|
let level = &mut self.levels[player_level];
|
|
level.update();
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn game_has_correct_number_of_levels() {
|
|
let p = Player::new("foo", 42);
|
|
let g = Game::new(p);
|
|
assert_eq!(g.levels.len(), LEVELS);
|
|
}
|
|
|
|
#[test]
|
|
fn game_has_player() {
|
|
let p = Player::new("foo", 42);
|
|
let g = Game::new(p);
|
|
assert_eq!(g.get_player().get_name(), "foo");
|
|
assert_eq!(g.get_player().get_immutable_position().get_level(), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn game_has_mutable_player() {
|
|
let p = Player::new("foo", 42);
|
|
let mut g = Game::new(p);
|
|
assert_eq!(g.get_player().get_name(), "foo");
|
|
g.get_mutable_player().change_life(-1);
|
|
assert_eq!(g.get_player().get_life(), 41);
|
|
}
|
|
|
|
#[test]
|
|
fn game_get_level() {
|
|
let p = Player::new("foo", 42);
|
|
let mut g = Game::new(p);
|
|
g.get_level(0);
|
|
assert_ne!(g.get_level(0).start, (0, 0));
|
|
g.get_mutable_level(0).discover(&Position::new(0, 1, 1));
|
|
}
|