add 3 distinct outcomes for the game
This commit is contained in:
parent
6b6206370b
commit
2a86201c3e
39
src/game.rs
39
src/game.rs
|
@ -3,7 +3,19 @@ use crate::level_generator::LevelGenerator;
|
||||||
use crate::player::Player;
|
use crate::player::Player;
|
||||||
use crate::position::Position;
|
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
|
/// the main structure to hold all information about the ongoing game
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
|
@ -31,6 +43,31 @@ impl Game {
|
||||||
g
|
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 {
|
pub fn get_player(&self) -> &Player {
|
||||||
&self.player
|
&self.player
|
||||||
}
|
}
|
||||||
|
|
|
@ -276,11 +276,11 @@ impl LevelGenerator {
|
||||||
// TODO randomize enemies here
|
// TODO randomize enemies here
|
||||||
match rng.gen_range(1..=100) {
|
match rng.gen_range(1..=100) {
|
||||||
1..=50 => {
|
1..=50 => {
|
||||||
println!("Orc! {} {} {}", self.level, t_x, t_y);
|
enemies.push(Box::new(Orc::new_with_position(Position::new(self.level, t_x, t_y))));
|
||||||
enemies.push(Box::new(Orc::new_with_position(Position::new(self.level, t_x, t_y)))); }
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!("Rat! {} {} {}", self.level, t_x, t_y);
|
enemies.push(Box::new(Rat::new_with_position(Position::new(self.level, t_x, t_y))));
|
||||||
enemies.push(Box::new(Rat::new_with_position(Position::new(self.level, t_x, t_y))));}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
45
src/main.rs
45
src/main.rs
|
@ -11,10 +11,11 @@ use ratatui::{
|
||||||
widgets::Paragraph,
|
widgets::Paragraph,
|
||||||
};
|
};
|
||||||
use ratatui::prelude::*;
|
use ratatui::prelude::*;
|
||||||
use ratatui::widgets::Block;
|
use ratatui::widgets::{Block, Borders, BorderType, Wrap};
|
||||||
|
use ratatui::widgets::block::{Position, Title};
|
||||||
use whoami::realname;
|
use whoami::realname;
|
||||||
|
|
||||||
use crate::game::Game;
|
use crate::game::{Game, GameState};
|
||||||
use crate::level_widget::LevelWidget;
|
use crate::level_widget::LevelWidget;
|
||||||
// use crate::level_widget::LevelWidget;
|
// use crate::level_widget::LevelWidget;
|
||||||
use crate::player::Player;
|
use crate::player::Player;
|
||||||
|
@ -96,6 +97,46 @@ fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
game.player_collects_artifact();
|
game.player_collects_artifact();
|
||||||
game.update_level();
|
game.update_level();
|
||||||
|
if game.get_game_state() != GameState::RUNNING {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
loop {
|
||||||
|
let _ = terminal.draw(|frame| {
|
||||||
|
let mut area = frame.size();
|
||||||
|
let w = area.width / 2;
|
||||||
|
let h = area.height / 2;
|
||||||
|
area.x += w - 20;
|
||||||
|
area.y += h - 10;
|
||||||
|
area.width = 40;
|
||||||
|
area.height = 20;
|
||||||
|
let block = Block::default()
|
||||||
|
.title("Game ended")
|
||||||
|
.title(Title::from("Press `q` to quit!").position(Position::Bottom))
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_style(Style::default().fg(Color::White))
|
||||||
|
.border_type(BorderType::Rounded)
|
||||||
|
.style(Style::default().bg(Color::Black));
|
||||||
|
let paragraph = match game.get_game_state() {
|
||||||
|
GameState::RUNNING => {
|
||||||
|
Paragraph::new("Quitting is for cowards! You'll better try again!")
|
||||||
|
}
|
||||||
|
GameState::LOST => {
|
||||||
|
Paragraph::new("Sorry, you died in the dungeon. Better luck next time!")
|
||||||
|
}
|
||||||
|
GameState::WON => {
|
||||||
|
Paragraph::new("Congratulation! You mastered your way through the dungeon and won the game.")
|
||||||
|
}
|
||||||
|
}.block(block).wrap(Wrap { trim: true });
|
||||||
|
frame.render_widget(paragraph, area);
|
||||||
|
});
|
||||||
|
if event::poll(std::time::Duration::from_millis(16))? {
|
||||||
|
if let event::Event::Key(key) = event::read()? {
|
||||||
|
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue