From 7a8fa84942c67a0c5951cff62a9d440e502a1188 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Wed, 22 Nov 2023 21:09:08 +0100 Subject: [PATCH] Implement chest experiment one --- src/game.rs | 41 +++++++++++++++++++++++++---------------- src/level.rs | 4 ++++ src/level_generator.rs | 14 ++++++++++++++ src/level_widget.rs | 9 +++++++++ src/main.rs | 3 ++- src/player.rs | 4 ++++ 6 files changed, 58 insertions(+), 17 deletions(-) diff --git a/src/game.rs b/src/game.rs index 9f78d58..6c59c6a 100644 --- a/src/game.rs +++ b/src/game.rs @@ -24,22 +24,31 @@ impl Game { } }; if can_go { - if level.get_element(new_x, new_y).unwrap() == LevelElement::StairDown { - let next_level = player_pos.get_level() + 1; - let start_x: usize = self.levels[next_level].start_x; - let start_y: usize = self.levels[next_level].start_y; - self.player.set_position(next_level, start_x, start_y); - self.levels[next_level].discover(self.player.get_position()); - } else if level.get_element(new_x, new_y).unwrap() == LevelElement::StairUp { - self.player.set_position( - player_pos.get_level() - 1, - self.levels[player_pos.get_level() - 1].end_x, - self.levels[player_pos.get_level() - 1].end_y, - ); - } else { - self.player.change_position(dx, dy); - self.levels[player_pos.get_level()].discover(self.player.get_position()); - } + match level.get_element(new_x, new_y).unwrap() { + LevelElement::StairDown => { + let next_level = player_pos.get_level() + 1; + let start_x: usize = self.levels[next_level].start_x; + let start_y: usize = self.levels[next_level].start_y; + self.player.set_position(next_level, start_x, start_y); + self.levels[next_level].discover(self.player.get_position()); + } + LevelElement::StairUp => { + self.player.set_position( + player_pos.get_level() - 1, + self.levels[player_pos.get_level() - 1].end_x, + self.levels[player_pos.get_level() - 1].end_y, + ); + } + LevelElement::Chest {gold} => { + self.player.add_gold(gold); + self.player.change_position(dx, dy); + self.levels[player_pos.get_level()].discover(self.player.get_position()); + } + _ => { + self.player.change_position(dx, dy); + self.levels[player_pos.get_level()].discover(self.player.get_position()); + } + }; } } } \ No newline at end of file diff --git a/src/level.rs b/src/level.rs index 1775b81..edbce98 100644 --- a/src/level.rs +++ b/src/level.rs @@ -10,6 +10,9 @@ pub enum LevelElement { Floor, StairDown, StairUp, + Start, + End, + Chest { gold: usize }, Unknown, } @@ -20,6 +23,7 @@ pub enum RoomType { StairUp, StairDown, BasicRoom, + TreasureRoom, EmptyRoom, } diff --git a/src/level_generator.rs b/src/level_generator.rs index 7722508..2d46665 100644 --- a/src/level_generator.rs +++ b/src/level_generator.rs @@ -91,6 +91,7 @@ impl LevelGenerator { } else { room_types.push(RoomType::StairDown); } + room_types.push(RoomType::TreasureRoom); // generate a random set of rooms and shuffle them for _ in room_types.len()..ROOMS_HORIZONTAL * ROOMS_VERTICAL { match rng.gen_range(1..=6) { @@ -239,11 +240,21 @@ impl LevelGenerator { s[left + room.offset_x + x][top + room.offset_y + y] = LevelElement::Floor; } } + if room.kind == RoomType::TreasureRoom { + let t_x = left + room.offset_x + rng.gen_range(0..room.width); + let t_y = top + room.offset_y + rng.gen_range(0..room.height); + s[t_x][t_y] = LevelElement::Chest{gold: rng.gen_range(2..100)}; + } if room.kind == RoomType::StairDown { end_x = left + room.offset_x + rng.gen_range(0..room.width); end_y = top + room.offset_y + rng.gen_range(0..room.height); + } + if room.kind == RoomType::StairDown { s[end_x][end_y] = LevelElement::StairDown; } + if room.kind == RoomType::End { + s[end_x][end_y] = LevelElement::End; + } if room.kind == RoomType::Start || room.kind == RoomType::StairUp { start_x = left + room.offset_x + rng.gen_range(0..room.width); start_y = top + room.offset_y + rng.gen_range(0..room.height); @@ -251,6 +262,9 @@ impl LevelGenerator { if room.kind == RoomType::StairUp { s[start_x][start_y] = LevelElement::StairUp; } + if room.kind == RoomType::Start { + s[start_x][start_y] = LevelElement::Start; + } } } // diff --git a/src/level_widget.rs b/src/level_widget.rs index d9703e5..f58652c 100644 --- a/src/level_widget.rs +++ b/src/level_widget.rs @@ -104,6 +104,15 @@ impl<'a> Widget for LevelWidget<'a> { LevelElement::Unknown => { self.set_cell(buf, x, y, "▒", Color::DarkGray, Color::Gray); } + LevelElement::Chest{gold} => { + self.set_cell(buf, x, y, "⌂", Color::Black, Color::Gray); + } + LevelElement::Start => { + self.set_cell(buf, x, y, "α", Color::Black, Color::Gray); + } + LevelElement::End => { + self.set_cell(buf, x, y, "Ω", Color::Black, Color::Gray); + } } } } diff --git a/src/main.rs b/src/main.rs index 3084423..b43bf3a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,10 +72,11 @@ fn main() -> Result<()> { height: 25, }; frame.render_widget( - Paragraph::new(format!("{}\nHealth: {}/{}\n{}/{}/{}", + Paragraph::new(format!("{}\nHealth: {}/{}\nGold: {}\n{}/{}/{}", g.player.get_name(), g.player.get_life(), g.player.get_max_life(), + g.player.get_gold(), g.player.get_position().get_level(), g.player.get_position().get_x(), g.player.get_position().get_y())) diff --git a/src/player.rs b/src/player.rs index 406a530..4f13976 100644 --- a/src/player.rs +++ b/src/player.rs @@ -5,6 +5,7 @@ use crate::position::Position; pub struct Player { name: String, position: Position, + gold: usize, life: usize, max_life: usize, } @@ -14,6 +15,7 @@ impl Player { Player { name: name.to_string(), position: Position::new(0, 0, 0), + gold: 0, life: max_life, max_life, } @@ -23,6 +25,8 @@ impl Player { } pub fn get_life(&self) -> usize { self.life } pub fn get_max_life(&self) -> usize { self.max_life } + pub fn get_gold(&self) -> usize { self.gold } + pub fn add_gold(&mut self, amount: usize) { self.gold += amount; } pub fn change_life(&mut self, by: i16) { self.life = max(0, min(self.max_life,