diff --git a/src/game.rs b/src/game.rs index 6c59c6a..6cfdd53 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,4 +1,4 @@ -use crate::level::{Level, LevelElement}; +use crate::level::{Artifact, Level, StructureElement}; use crate::player::Player; pub struct Game { @@ -15,40 +15,43 @@ impl Game { let new_x: i16 = player_pos.get_x() as i16 + dx; let new_y: i16 = player_pos.get_y() as i16 + dy; let can_go: bool = match level.get_element(new_x, new_y) { - None => { false } - Some(t) => { + (None, _) => { false } + (Some(t), _) => { match t { - LevelElement::Wall => { false } + StructureElement::Wall => { false } _ => { true } } } }; if can_go { - match level.get_element(new_x, new_y).unwrap() { - LevelElement::StairDown => { + let tmp = level.get_element(new_x, new_y); + match tmp.0.unwrap() { + StructureElement::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 => { + StructureElement::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()); } }; + match tmp.1 { + Some(Artifact::Chest {gold}) => { + self.levels[player_pos.get_level()].remove_artifact(new_x, new_y); + self.player.add_gold(gold); + } + _ => {} + } } } } \ No newline at end of file diff --git a/src/level.rs b/src/level.rs index edbce98..fd88ec4 100644 --- a/src/level.rs +++ b/src/level.rs @@ -5,32 +5,27 @@ pub const LEVEL_WIDTH: usize = 50; pub const LEVEL_HEIGHT: usize = 25; #[derive(Copy, Clone, Debug, PartialEq)] -pub enum LevelElement { +pub enum StructureElement { Wall, Floor, StairDown, StairUp, Start, End, - Chest { gold: usize }, Unknown, } #[derive(Copy, Clone, Debug, PartialEq)] -pub enum RoomType { - Start, - End, - StairUp, - StairDown, - BasicRoom, - TreasureRoom, - EmptyRoom, +pub enum Artifact { + Chest { gold: usize }, } + #[derive(Copy, Clone, Debug, PartialEq)] pub struct Level { - pub(crate) structure: [[LevelElement; LEVEL_HEIGHT]; LEVEL_WIDTH], + pub(crate) structure: [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH], pub(crate) discovered: [[bool; LEVEL_HEIGHT]; LEVEL_WIDTH], + pub(crate) artifacts: [[Option; LEVEL_HEIGHT]; LEVEL_WIDTH], pub(crate) start_x: usize, pub(crate) start_y: usize, pub(crate) end_x: usize, @@ -39,29 +34,41 @@ pub struct Level { impl Level { pub fn new() -> Level { - let s = [[LevelElement::Wall; LEVEL_HEIGHT]; LEVEL_WIDTH]; + let s = [[StructureElement::Wall; LEVEL_HEIGHT]; LEVEL_WIDTH]; Level { structure: s, discovered: [[false; LEVEL_HEIGHT]; LEVEL_WIDTH], + artifacts: [[None; LEVEL_HEIGHT]; LEVEL_WIDTH], start_x: 1, start_y: 1, end_x: 1, end_y: 1, } } - pub fn get_element(&self, x: i16, y: i16) -> Option { + pub fn remove_artifact(&mut self, x: i16, y: i16) { if x < 0 || y < 0 { - return None; + return; } let x = x as usize; let y = y as usize; if x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT { - return None; + return; } - if self.discovered[x][y] { - return Some(self.structure[x][y]); + self.artifacts[x][y] = None; + } + pub fn get_element(&self, x: i16, y: i16) -> (Option, Option) { + if x < 0 || y < 0 { + return (None, None); } - return Some(LevelElement::Unknown); + let x = x as usize; + let y = y as usize; + if x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT { + return (None, None); + } + if !self.discovered[x][y] { + return (Some(StructureElement::Unknown), None); + } + return (Some(self.structure[x][y]), self.artifacts[x][y]); } /// discover the area with in the level around the given position pub fn discover(&mut self, pos: Position) { @@ -74,25 +81,25 @@ impl Level { } for x_r in x..=min(x + 2, LEVEL_WIDTH - 1) { self.discovered[x_r][y] = true; - if self.structure[x_r][y] == LevelElement::Wall { + if self.structure[x_r][y] == StructureElement::Wall { break; } } for y_r in y..=min(y + 2, LEVEL_HEIGHT - 1) { self.discovered[x][y_r] = true; - if self.structure[x][y_r] == LevelElement::Wall { + if self.structure[x][y_r] == StructureElement::Wall { break; } } for x_r in (max(x, 2)..x + 2).rev() { self.discovered[x_r - 2][y] = true; - if self.structure[x_r - 2][y] == LevelElement::Wall { + if self.structure[x_r - 2][y] == StructureElement::Wall { break; } } for y_r in (max(y, 2)..y + 2).rev() { self.discovered[x][y_r - 2] = true; - if self.structure[x][y_r - 2] == LevelElement::Wall { + if self.structure[x][y_r - 2] == StructureElement::Wall { break; } } @@ -102,8 +109,8 @@ impl Level { #[test] fn test_get_element() { let l = Level::new(); - assert_eq!(l.get_element(0, 0).unwrap(), LevelElement::Wall); - assert_eq!(l.get_element(4, 4).unwrap(), LevelElement::StairDown); - assert_eq!(l.get_element(49, 24).unwrap(), LevelElement::Wall); - assert_eq!(l.get_element(50, 25), None); + assert_eq!(l.get_element(0, 0).0.unwrap(), StructureElement::Wall); + assert_eq!(l.get_element(4, 4).0.unwrap(), StructureElement::StairDown); + assert_eq!(l.get_element(49, 24).0.unwrap(), StructureElement::Wall); + assert_eq!(l.get_element(50, 25).0, None); } diff --git a/src/level_generator.rs b/src/level_generator.rs index 2d46665..03ab335 100644 --- a/src/level_generator.rs +++ b/src/level_generator.rs @@ -9,13 +9,24 @@ use rand::prelude::SliceRandom; use rand::Rng; use rand::rngs::ThreadRng; -use crate::level::{Level, LevelElement, RoomType}; +use crate::level::{Artifact, Level, StructureElement}; const ROOMS_VERTICAL: usize = 7; const ROOMS_HORIZONTAL: usize = 4; const ROOM_WIDTH: usize = 7; const ROOM_HEIGHT: usize = 6; +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum RoomType { + Start, + End, + StairUp, + StairDown, + BasicRoom, + TreasureRoom, + EmptyRoom, +} + #[derive(Copy, Clone, Debug, PartialEq)] struct ConnectionInfo { offset: usize, @@ -225,7 +236,8 @@ impl LevelGenerator { } pub fn render(&self) -> Level { let mut rng = rand::thread_rng(); - let mut s = [[LevelElement::Wall; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH]; + let mut s = [[StructureElement::Wall; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH]; + let mut d = [[None; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH]; let mut start_x: usize = 0; let mut start_y: usize = 0; let mut end_x: usize = 0; @@ -237,33 +249,33 @@ impl LevelGenerator { let room = self.rooms[c][r]; for x in 0..room.width { for y in 0..room.height { - s[left + room.offset_x + x][top + room.offset_y + y] = LevelElement::Floor; + s[left + room.offset_x + x][top + room.offset_y + y] = StructureElement::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)}; + d[t_x][t_y] = Some(Artifact::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; + s[end_x][end_y] = StructureElement::StairDown; } if room.kind == RoomType::End { - s[end_x][end_y] = LevelElement::End; + s[end_x][end_y] = StructureElement::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); } if room.kind == RoomType::StairUp { - s[start_x][start_y] = LevelElement::StairUp; + s[start_x][start_y] = StructureElement::StairUp; } if room.kind == RoomType::Start { - s[start_x][start_y] = LevelElement::Start; + s[start_x][start_y] = StructureElement::Start; } } } @@ -277,8 +289,8 @@ impl LevelGenerator { let left = 1 + c * ROOM_WIDTH + x_conn.offset; let bottom = 1 + (r + x_conn.distance) * ROOM_HEIGHT + tgt_room.offset_y + tgt_room.height; for i in top..bottom { - if s[left][i] == LevelElement::Wall { - s[left][i] = LevelElement::Floor; + if s[left][i] == StructureElement::Wall { + s[left][i] = StructureElement::Floor; } } } @@ -289,8 +301,8 @@ impl LevelGenerator { let left = 1 + c * ROOM_WIDTH + src_room.offset_x; let right = 1 + (c + y_conn.distance) * ROOM_WIDTH + tgt_room.offset_x + tgt_room.width; for i in left..right { - if s[i][top] == LevelElement::Wall { - s[i][top] = LevelElement::Floor; + if s[i][top] == StructureElement::Wall { + s[i][top] = StructureElement::Floor; } } } @@ -299,6 +311,7 @@ impl LevelGenerator { Level { structure: s, discovered: [[false; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH], + artifacts: d, start_x, start_y, end_x, diff --git a/src/level_widget.rs b/src/level_widget.rs index f58652c..bad251e 100644 --- a/src/level_widget.rs +++ b/src/level_widget.rs @@ -2,10 +2,11 @@ use ratatui::prelude::*; use ratatui::widgets::Widget; use crate::game::Game; -use crate::level::{Level, LevelElement}; +use crate::level::{Artifact, Level, StructureElement}; const FG_BROWN: Color = Color::Rgb(186, 74, 0); +//⌂ α Ω pub struct LevelWidget<'a> { pub game: &'a Game, } @@ -19,12 +20,12 @@ impl<'a> LevelWidget<'a> { set_fg(fg); } - fn calculate_wall(top: Option, right: Option, bottom: Option, left: Option) -> String { + fn calculate_wall(top: Option, right: Option, bottom: Option, left: Option) -> String { let mut val = 0; val += match top { Some(n) => { match n { - LevelElement::Wall => 1, + StructureElement::Wall => 1, _ => 0 } } @@ -33,7 +34,7 @@ impl<'a> LevelWidget<'a> { val += match right { Some(n) => { match n { - LevelElement::Wall => 2, + StructureElement::Wall => 2, _ => 0 } } @@ -42,7 +43,7 @@ impl<'a> LevelWidget<'a> { val += match bottom { Some(n) => { match n { - LevelElement::Wall => 4, + StructureElement::Wall => 4, _ => 0 } } @@ -51,7 +52,7 @@ impl<'a> LevelWidget<'a> { val += match left { Some(n) => { match n { - LevelElement::Wall => 8, + StructureElement::Wall => 8, _ => 0 } } @@ -81,36 +82,40 @@ impl<'a> Widget for LevelWidget<'a> { let level_x = (x - al) as i16; let level_y = (y - at) as i16; match level.get_element(level_x, level_y) { - None => {} - Some(e) => { + (None, _) => {} + (_, Some(e)) => { match e { - LevelElement::Wall => { - let top = level.get_element(level_x, level_y - 1); - let right = level.get_element(level_x + 1, level_y); - let bottom = level.get_element(level_x, level_y + 1); - let left = level.get_element(level_x - 1, level_y); + Artifact::Chest { gold: _ } => { + self.set_cell(buf, x, y, "⌂", Color::Black, Color::Gray); + } + } + } + (Some(e), _) => { + match e { + StructureElement::Wall => { + let top = level.get_element(level_x, level_y - 1).0; + let right = level.get_element(level_x + 1, level_y).0; + let bottom = level.get_element(level_x, level_y + 1).0; + let left = level.get_element(level_x - 1, level_y).0; let symbol = LevelWidget::calculate_wall(top, right, bottom, left); self.set_cell(buf, x, y, symbol.as_str(), FG_BROWN, Color::Gray); } - LevelElement::Floor => { + StructureElement::Floor => { self.set_cell(buf, x, y, " ", FG_BROWN, Color::Gray); } - LevelElement::StairDown => { + StructureElement::StairDown => { self.set_cell(buf, x, y, ">", Color::Black, Color::Gray); } - LevelElement::StairUp => { + StructureElement::StairUp => { self.set_cell(buf, x, y, "<", Color::Black, Color::Gray); } - LevelElement::Unknown => { + StructureElement::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 => { + StructureElement::Start => { self.set_cell(buf, x, y, "α", Color::Black, Color::Gray); } - LevelElement::End => { + StructureElement::End => { self.set_cell(buf, x, y, "Ω", Color::Black, Color::Gray); } } @@ -128,7 +133,7 @@ impl<'a> Widget for LevelWidget<'a> { #[test] fn test_get_wall_symbol() { - let wall = Some(LevelElement::Wall); + let wall = Some(StructureElement::Wall); // 0 let s = LevelWidget::calculate_wall(None, None, None, None); assert_eq!(s, "╋"); diff --git a/src/player.rs b/src/player.rs index 4f13976..152dd80 100644 --- a/src/player.rs +++ b/src/player.rs @@ -51,6 +51,7 @@ fn test_get_name() { let p = Player { name: "Teddy Tester".to_string(), position: Position::new(0, 1, 1), + gold: 0, life: 5, max_life: 10, }; @@ -62,6 +63,7 @@ fn test_change_life() { let mut p = Player { name: "Teddy Tester".to_string(), position: Position::new(0, 1, 1), + gold: 0, life: 5, max_life: 10, };