Compare commits
5 Commits
monsters_a
...
make_monst
| Author | SHA1 | Date | |
|---|---|---|---|
| cca9e8f285 | |||
| de5ea76913 | |||
| 6082a740e9 | |||
| db96dc5c8a | |||
| 2a86201c3e |
@@ -22,9 +22,11 @@ pub struct Chest {
|
||||
|
||||
impl Chest {
|
||||
pub fn new(position: Position) -> Self {
|
||||
let level = position.get_level();
|
||||
Self {
|
||||
position,
|
||||
gold: 10,
|
||||
// TODO maybe randomize this?
|
||||
gold: (level + 1) * 10,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
47
src/game.rs
47
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
|
||||
}
|
||||
@@ -131,6 +168,10 @@ impl Game {
|
||||
// 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();
|
||||
}
|
||||
};
|
||||
@@ -149,11 +190,11 @@ impl Game {
|
||||
}
|
||||
}
|
||||
/// updates the player's current level. This will remove collected artifacts and dead monsters.
|
||||
pub fn update_level(&mut self) {
|
||||
pub fn update_level(&mut self, ticks: u64) {
|
||||
let player_pos = &self.player.get_immutable_position();
|
||||
let player_level = player_pos.get_level();
|
||||
let level = &mut self.levels[player_level];
|
||||
level.update();
|
||||
level.update(ticks);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
11
src/level.rs
11
src/level.rs
@@ -152,7 +152,7 @@ impl Level {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
pub fn update(&mut self, ticks: u64) {
|
||||
for (index, a) in &mut self.artifacts.iter().enumerate() {
|
||||
if a.was_collected() {
|
||||
self.artifacts.remove(index);
|
||||
@@ -165,6 +165,15 @@ impl Level {
|
||||
break;
|
||||
}
|
||||
}
|
||||
for index in 0..self.monsters.len() {
|
||||
let mut dx = 0;
|
||||
let speed = 50;
|
||||
if ticks % speed == 0 {
|
||||
dx = if (ticks / speed) % 2 == 0 { -1 } else { 1 };
|
||||
}
|
||||
|
||||
self.monsters[index].get_position().change(dx, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -276,11 +276,11 @@ impl LevelGenerator {
|
||||
// TODO randomize enemies here
|
||||
match rng.gen_range(1..=100) {
|
||||
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))));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
61
src/main.rs
61
src/main.rs
@@ -11,10 +11,11 @@ use ratatui::{
|
||||
widgets::Paragraph,
|
||||
};
|
||||
use ratatui::prelude::*;
|
||||
use ratatui::widgets::Block;
|
||||
use ratatui::widgets::{Block, Borders, BorderType, Wrap};
|
||||
use ratatui::widgets::block::{Position, Title};
|
||||
use whoami::realname;
|
||||
|
||||
use crate::game::Game;
|
||||
use crate::game::{Game, GameState};
|
||||
use crate::level_widget::LevelWidget;
|
||||
// use crate::level_widget::LevelWidget;
|
||||
use crate::player::Player;
|
||||
@@ -37,6 +38,7 @@ fn main() -> Result<()> {
|
||||
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
|
||||
terminal.clear()?;
|
||||
|
||||
let mut ticks: u64 = 0;
|
||||
loop {
|
||||
terminal.draw(|frame| {
|
||||
let mut area = frame.size();
|
||||
@@ -66,18 +68,21 @@ fn main() -> Result<()> {
|
||||
height: 25,
|
||||
};
|
||||
frame.render_widget(
|
||||
Paragraph::new(format!("{}\nHealth: {}/{}\nGold: {}\nLevel: {}",
|
||||
Paragraph::new(format!("{}\nHealth: {}/{}\nExp: {}\nGold: {}\nLevel: {}\nticks: {}",
|
||||
game.get_player().get_name(),
|
||||
game.get_player().get_life(),
|
||||
game.get_player().get_max_life(),
|
||||
game.get_player().get_experience(),
|
||||
game.get_player().get_gold(),
|
||||
game.get_player().get_immutable_position().get_level()))
|
||||
game.get_player().get_immutable_position().get_level(),
|
||||
ticks
|
||||
))
|
||||
.white()
|
||||
.on_blue(),
|
||||
stats_area,
|
||||
);
|
||||
})?;
|
||||
if event::poll(std::time::Duration::from_millis(16))? {
|
||||
if event::poll(std::time::Duration::from_millis(50))? {
|
||||
if let event::Event::Key(key) = event::read()? {
|
||||
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
||||
break;
|
||||
@@ -95,7 +100,51 @@ fn main() -> Result<()> {
|
||||
game.move_player(new_pos.0, new_pos.1);
|
||||
}
|
||||
game.player_collects_artifact();
|
||||
game.update_level();
|
||||
if game.get_game_state() != GameState::RUNNING {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
game.update_level(ticks);
|
||||
ticks+=1;
|
||||
}
|
||||
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 mut text = match game.get_game_state() {
|
||||
GameState::RUNNING => {
|
||||
"Quitting is for cowards! You'll better try again!".to_string()
|
||||
}
|
||||
GameState::LOST => {
|
||||
"Sorry, you died in the dungeon. Better luck next time!".to_string()
|
||||
}
|
||||
GameState::WON => {
|
||||
"Congratulation! You mastered your way through the dungeon and won the game.".to_string()
|
||||
}
|
||||
};
|
||||
text += format!("\nYou gained {} experience.", game.get_player().get_experience()).as_str();
|
||||
text += format!("\nYou collected {} gold.", game.get_player().get_gold()).as_str();
|
||||
let paragraph = Paragraph::new(text).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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ pub trait Monster {
|
||||
fn get_representation(&self) -> (&str, Color);
|
||||
fn decrease_life(&mut self, by: usize);
|
||||
fn get_immutable_position(&self) -> &Position;
|
||||
#[cfg(test)]
|
||||
fn get_experience_gain(&self) -> usize;
|
||||
fn get_position(&mut self) -> &mut Position;
|
||||
#[cfg(test)]
|
||||
fn get_life(&self) -> usize;
|
||||
@@ -17,6 +17,7 @@ macro_rules! default_monster {
|
||||
($($t:ty),+ $(,)?) => ($(
|
||||
impl Monster for $t {
|
||||
fn is_dead(&self) -> bool { self.life <= 0 }
|
||||
fn get_experience_gain(&self) -> usize { self.experience_gain }
|
||||
fn get_representation(&self) -> (&str, Color) { (&self.symbol, self.color) }
|
||||
fn decrease_life(&mut self, by: usize) {
|
||||
self.life = self.life.saturating_sub(by);
|
||||
@@ -24,7 +25,6 @@ macro_rules! default_monster {
|
||||
fn get_immutable_position(&self) -> &Position {
|
||||
&self.position
|
||||
}
|
||||
#[cfg(test)]
|
||||
fn get_position(&mut self) -> &mut Position {
|
||||
&mut self.position
|
||||
}
|
||||
@@ -39,6 +39,7 @@ pub struct Rat {
|
||||
position: Position,
|
||||
symbol: String,
|
||||
color: Color,
|
||||
experience_gain: usize,
|
||||
}
|
||||
|
||||
impl Rat {
|
||||
@@ -49,6 +50,7 @@ impl Rat {
|
||||
position: Position::new(0, 0, 0),
|
||||
symbol: String::from("R"),
|
||||
color: Color::Black,
|
||||
experience_gain: 5,
|
||||
}
|
||||
}
|
||||
pub fn new_with_position(position: Position) -> Self {
|
||||
@@ -57,6 +59,7 @@ impl Rat {
|
||||
position,
|
||||
symbol: String::from("R"),
|
||||
color: Color::Black,
|
||||
experience_gain: 5,
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
@@ -69,6 +72,7 @@ pub struct Orc {
|
||||
position: Position,
|
||||
symbol: String,
|
||||
color: Color,
|
||||
experience_gain: usize,
|
||||
}
|
||||
|
||||
impl Orc {
|
||||
@@ -79,6 +83,7 @@ impl Orc {
|
||||
position: Position::new(0, 0, 0),
|
||||
symbol: String::from("O"),
|
||||
color: Color::DarkGray,
|
||||
experience_gain: 10,
|
||||
}
|
||||
}
|
||||
pub fn new_with_position(position: Position) -> Self {
|
||||
@@ -87,6 +92,7 @@ impl Orc {
|
||||
position,
|
||||
symbol: String::from("O"),
|
||||
color: Color::DarkGray,
|
||||
experience_gain: 10,
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -8,6 +8,7 @@ pub struct Player {
|
||||
life: i16,
|
||||
max_life: i16,
|
||||
gold: usize,
|
||||
experience: usize,
|
||||
}
|
||||
|
||||
impl Player {
|
||||
@@ -18,6 +19,7 @@ impl Player {
|
||||
life: max_life,
|
||||
max_life,
|
||||
gold: 0,
|
||||
experience: 0,
|
||||
}
|
||||
}
|
||||
pub fn get_name(&self) -> String {
|
||||
@@ -44,6 +46,10 @@ impl Player {
|
||||
|
||||
/// return the size of the players gold stash
|
||||
pub fn get_gold(&self) -> usize { self.gold }
|
||||
|
||||
pub fn gain_experience(&mut self, amount: usize) { self.experience += amount }
|
||||
|
||||
pub fn get_experience(&self) -> usize { self.experience }
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -54,6 +60,7 @@ fn test_get_name() {
|
||||
life: 5,
|
||||
max_life: 10,
|
||||
gold: 0,
|
||||
experience: 0,
|
||||
};
|
||||
assert_eq!(p.get_name(), "Teddy Tester");
|
||||
}
|
||||
@@ -76,6 +83,7 @@ fn test_change_life() {
|
||||
life: 5,
|
||||
max_life: 10,
|
||||
gold: 0,
|
||||
experience: 0,
|
||||
};
|
||||
assert_eq!(p.get_life(), 5);
|
||||
p.change_life(-2);
|
||||
@@ -108,6 +116,7 @@ fn test_max_life() {
|
||||
life: 5,
|
||||
max_life: 10,
|
||||
gold: 0,
|
||||
experience: 0,
|
||||
};
|
||||
assert_eq!(p.get_max_life(), 10);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user