finish chests

This commit is contained in:
Joachim Lusiardi 2023-11-23 09:29:13 +01:00
parent 7a8fa84942
commit cf1837a46e
5 changed files with 103 additions and 73 deletions

View File

@ -1,4 +1,4 @@
use crate::level::{Level, LevelElement}; use crate::level::{Artifact, Level, StructureElement};
use crate::player::Player; use crate::player::Player;
pub struct Game { pub struct Game {
@ -15,40 +15,43 @@ impl Game {
let new_x: i16 = player_pos.get_x() as i16 + dx; let new_x: i16 = player_pos.get_x() as i16 + dx;
let new_y: i16 = player_pos.get_y() as i16 + dy; let new_y: i16 = player_pos.get_y() as i16 + dy;
let can_go: bool = match level.get_element(new_x, new_y) { let can_go: bool = match level.get_element(new_x, new_y) {
None => { false } (None, _) => { false }
Some(t) => { (Some(t), _) => {
match t { match t {
LevelElement::Wall => { false } StructureElement::Wall => { false }
_ => { true } _ => { true }
} }
} }
}; };
if can_go { if can_go {
match level.get_element(new_x, new_y).unwrap() { let tmp = level.get_element(new_x, new_y);
LevelElement::StairDown => { match tmp.0.unwrap() {
StructureElement::StairDown => {
let next_level = player_pos.get_level() + 1; let next_level = player_pos.get_level() + 1;
let start_x: usize = self.levels[next_level].start_x; let start_x: usize = self.levels[next_level].start_x;
let start_y: usize = self.levels[next_level].start_y; let start_y: usize = self.levels[next_level].start_y;
self.player.set_position(next_level, start_x, start_y); self.player.set_position(next_level, start_x, start_y);
self.levels[next_level].discover(self.player.get_position()); self.levels[next_level].discover(self.player.get_position());
} }
LevelElement::StairUp => { StructureElement::StairUp => {
self.player.set_position( self.player.set_position(
player_pos.get_level() - 1, player_pos.get_level() - 1,
self.levels[player_pos.get_level() - 1].end_x, self.levels[player_pos.get_level() - 1].end_x,
self.levels[player_pos.get_level() - 1].end_y, 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.player.change_position(dx, dy);
self.levels[player_pos.get_level()].discover(self.player.get_position()); 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);
}
_ => {}
}
} }
} }
} }

View File

@ -5,32 +5,27 @@ pub const LEVEL_WIDTH: usize = 50;
pub const LEVEL_HEIGHT: usize = 25; pub const LEVEL_HEIGHT: usize = 25;
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub enum LevelElement { pub enum StructureElement {
Wall, Wall,
Floor, Floor,
StairDown, StairDown,
StairUp, StairUp,
Start, Start,
End, End,
Chest { gold: usize },
Unknown, Unknown,
} }
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub enum RoomType { pub enum Artifact {
Start, Chest { gold: usize },
End,
StairUp,
StairDown,
BasicRoom,
TreasureRoom,
EmptyRoom,
} }
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub struct Level { 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) discovered: [[bool; LEVEL_HEIGHT]; LEVEL_WIDTH],
pub(crate) artifacts: [[Option<Artifact>; LEVEL_HEIGHT]; LEVEL_WIDTH],
pub(crate) start_x: usize, pub(crate) start_x: usize,
pub(crate) start_y: usize, pub(crate) start_y: usize,
pub(crate) end_x: usize, pub(crate) end_x: usize,
@ -39,29 +34,41 @@ pub struct Level {
impl Level { impl Level {
pub fn new() -> Level { pub fn new() -> Level {
let s = [[LevelElement::Wall; LEVEL_HEIGHT]; LEVEL_WIDTH]; let s = [[StructureElement::Wall; LEVEL_HEIGHT]; LEVEL_WIDTH];
Level { Level {
structure: s, structure: s,
discovered: [[false; LEVEL_HEIGHT]; LEVEL_WIDTH], discovered: [[false; LEVEL_HEIGHT]; LEVEL_WIDTH],
artifacts: [[None; LEVEL_HEIGHT]; LEVEL_WIDTH],
start_x: 1, start_x: 1,
start_y: 1, start_y: 1,
end_x: 1, end_x: 1,
end_y: 1, end_y: 1,
} }
} }
pub fn get_element(&self, x: i16, y: i16) -> Option<LevelElement> { pub fn remove_artifact(&mut self, x: i16, y: i16) {
if x < 0 || y < 0 { if x < 0 || y < 0 {
return None; return;
} }
let x = x as usize; let x = x as usize;
let y = y as usize; let y = y as usize;
if x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT { if x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT {
return None; return;
} }
if self.discovered[x][y] { self.artifacts[x][y] = None;
return Some(self.structure[x][y]); }
pub fn get_element(&self, x: i16, y: i16) -> (Option<StructureElement>, Option<Artifact>) {
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 /// discover the area with in the level around the given position
pub fn discover(&mut self, pos: 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) { for x_r in x..=min(x + 2, LEVEL_WIDTH - 1) {
self.discovered[x_r][y] = true; self.discovered[x_r][y] = true;
if self.structure[x_r][y] == LevelElement::Wall { if self.structure[x_r][y] == StructureElement::Wall {
break; break;
} }
} }
for y_r in y..=min(y + 2, LEVEL_HEIGHT - 1) { for y_r in y..=min(y + 2, LEVEL_HEIGHT - 1) {
self.discovered[x][y_r] = true; self.discovered[x][y_r] = true;
if self.structure[x][y_r] == LevelElement::Wall { if self.structure[x][y_r] == StructureElement::Wall {
break; break;
} }
} }
for x_r in (max(x, 2)..x + 2).rev() { for x_r in (max(x, 2)..x + 2).rev() {
self.discovered[x_r - 2][y] = true; 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; break;
} }
} }
for y_r in (max(y, 2)..y + 2).rev() { for y_r in (max(y, 2)..y + 2).rev() {
self.discovered[x][y_r - 2] = true; 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; break;
} }
} }
@ -102,8 +109,8 @@ impl Level {
#[test] #[test]
fn test_get_element() { fn test_get_element() {
let l = Level::new(); let l = Level::new();
assert_eq!(l.get_element(0, 0).unwrap(), LevelElement::Wall); assert_eq!(l.get_element(0, 0).0.unwrap(), StructureElement::Wall);
assert_eq!(l.get_element(4, 4).unwrap(), LevelElement::StairDown); assert_eq!(l.get_element(4, 4).0.unwrap(), StructureElement::StairDown);
assert_eq!(l.get_element(49, 24).unwrap(), LevelElement::Wall); assert_eq!(l.get_element(49, 24).0.unwrap(), StructureElement::Wall);
assert_eq!(l.get_element(50, 25), None); assert_eq!(l.get_element(50, 25).0, None);
} }

View File

@ -9,13 +9,24 @@ use rand::prelude::SliceRandom;
use rand::Rng; use rand::Rng;
use rand::rngs::ThreadRng; use rand::rngs::ThreadRng;
use crate::level::{Level, LevelElement, RoomType}; use crate::level::{Artifact, Level, StructureElement};
const ROOMS_VERTICAL: usize = 7; const ROOMS_VERTICAL: usize = 7;
const ROOMS_HORIZONTAL: usize = 4; const ROOMS_HORIZONTAL: usize = 4;
const ROOM_WIDTH: usize = 7; const ROOM_WIDTH: usize = 7;
const ROOM_HEIGHT: usize = 6; 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)] #[derive(Copy, Clone, Debug, PartialEq)]
struct ConnectionInfo { struct ConnectionInfo {
offset: usize, offset: usize,
@ -225,7 +236,8 @@ impl LevelGenerator {
} }
pub fn render(&self) -> Level { pub fn render(&self) -> Level {
let mut rng = rand::thread_rng(); 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_x: usize = 0;
let mut start_y: usize = 0; let mut start_y: usize = 0;
let mut end_x: usize = 0; let mut end_x: usize = 0;
@ -237,33 +249,33 @@ impl LevelGenerator {
let room = self.rooms[c][r]; let room = self.rooms[c][r];
for x in 0..room.width { for x in 0..room.width {
for y in 0..room.height { 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 { if room.kind == RoomType::TreasureRoom {
let t_x = left + room.offset_x + rng.gen_range(0..room.width); 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); 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 { if room.kind == RoomType::StairDown {
end_x = left + room.offset_x + rng.gen_range(0..room.width); end_x = left + room.offset_x + rng.gen_range(0..room.width);
end_y = top + room.offset_y + rng.gen_range(0..room.height); end_y = top + room.offset_y + rng.gen_range(0..room.height);
} }
if room.kind == RoomType::StairDown { if room.kind == RoomType::StairDown {
s[end_x][end_y] = LevelElement::StairDown; s[end_x][end_y] = StructureElement::StairDown;
} }
if room.kind == RoomType::End { 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 { if room.kind == RoomType::Start || room.kind == RoomType::StairUp {
start_x = left + room.offset_x + rng.gen_range(0..room.width); start_x = left + room.offset_x + rng.gen_range(0..room.width);
start_y = top + room.offset_y + rng.gen_range(0..room.height); start_y = top + room.offset_y + rng.gen_range(0..room.height);
} }
if room.kind == RoomType::StairUp { if room.kind == RoomType::StairUp {
s[start_x][start_y] = LevelElement::StairUp; s[start_x][start_y] = StructureElement::StairUp;
} }
if room.kind == RoomType::Start { 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 left = 1 + c * ROOM_WIDTH + x_conn.offset;
let bottom = 1 + (r + x_conn.distance) * ROOM_HEIGHT + tgt_room.offset_y + tgt_room.height; let bottom = 1 + (r + x_conn.distance) * ROOM_HEIGHT + tgt_room.offset_y + tgt_room.height;
for i in top..bottom { for i in top..bottom {
if s[left][i] == LevelElement::Wall { if s[left][i] == StructureElement::Wall {
s[left][i] = LevelElement::Floor; s[left][i] = StructureElement::Floor;
} }
} }
} }
@ -289,8 +301,8 @@ impl LevelGenerator {
let left = 1 + c * ROOM_WIDTH + src_room.offset_x; 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; let right = 1 + (c + y_conn.distance) * ROOM_WIDTH + tgt_room.offset_x + tgt_room.width;
for i in left..right { for i in left..right {
if s[i][top] == LevelElement::Wall { if s[i][top] == StructureElement::Wall {
s[i][top] = LevelElement::Floor; s[i][top] = StructureElement::Floor;
} }
} }
} }
@ -299,6 +311,7 @@ impl LevelGenerator {
Level { Level {
structure: s, structure: s,
discovered: [[false; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH], discovered: [[false; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 1 + ROOMS_VERTICAL * ROOM_WIDTH],
artifacts: d,
start_x, start_x,
start_y, start_y,
end_x, end_x,

View File

@ -2,10 +2,11 @@ use ratatui::prelude::*;
use ratatui::widgets::Widget; use ratatui::widgets::Widget;
use crate::game::Game; use crate::game::Game;
use crate::level::{Level, LevelElement}; use crate::level::{Artifact, Level, StructureElement};
const FG_BROWN: Color = Color::Rgb(186, 74, 0); const FG_BROWN: Color = Color::Rgb(186, 74, 0);
//⌂ α Ω
pub struct LevelWidget<'a> { pub struct LevelWidget<'a> {
pub game: &'a Game, pub game: &'a Game,
} }
@ -19,12 +20,12 @@ impl<'a> LevelWidget<'a> {
set_fg(fg); set_fg(fg);
} }
fn calculate_wall(top: Option<LevelElement>, right: Option<LevelElement>, bottom: Option<LevelElement>, left: Option<LevelElement>) -> String { fn calculate_wall(top: Option<StructureElement>, right: Option<StructureElement>, bottom: Option<StructureElement>, left: Option<StructureElement>) -> String {
let mut val = 0; let mut val = 0;
val += match top { val += match top {
Some(n) => { Some(n) => {
match n { match n {
LevelElement::Wall => 1, StructureElement::Wall => 1,
_ => 0 _ => 0
} }
} }
@ -33,7 +34,7 @@ impl<'a> LevelWidget<'a> {
val += match right { val += match right {
Some(n) => { Some(n) => {
match n { match n {
LevelElement::Wall => 2, StructureElement::Wall => 2,
_ => 0 _ => 0
} }
} }
@ -42,7 +43,7 @@ impl<'a> LevelWidget<'a> {
val += match bottom { val += match bottom {
Some(n) => { Some(n) => {
match n { match n {
LevelElement::Wall => 4, StructureElement::Wall => 4,
_ => 0 _ => 0
} }
} }
@ -51,7 +52,7 @@ impl<'a> LevelWidget<'a> {
val += match left { val += match left {
Some(n) => { Some(n) => {
match n { match n {
LevelElement::Wall => 8, StructureElement::Wall => 8,
_ => 0 _ => 0
} }
} }
@ -81,36 +82,40 @@ impl<'a> Widget for LevelWidget<'a> {
let level_x = (x - al) as i16; let level_x = (x - al) as i16;
let level_y = (y - at) as i16; let level_y = (y - at) as i16;
match level.get_element(level_x, level_y) { match level.get_element(level_x, level_y) {
None => {} (None, _) => {}
Some(e) => { (_, Some(e)) => {
match e { match e {
LevelElement::Wall => { Artifact::Chest { gold: _ } => {
let top = level.get_element(level_x, level_y - 1); self.set_cell(buf, x, y, "", Color::Black, Color::Gray);
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); }
(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); let symbol = LevelWidget::calculate_wall(top, right, bottom, left);
self.set_cell(buf, x, y, symbol.as_str(), FG_BROWN, Color::Gray); 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); self.set_cell(buf, x, y, " ", FG_BROWN, Color::Gray);
} }
LevelElement::StairDown => { StructureElement::StairDown => {
self.set_cell(buf, x, y, ">", Color::Black, Color::Gray); self.set_cell(buf, x, y, ">", Color::Black, Color::Gray);
} }
LevelElement::StairUp => { StructureElement::StairUp => {
self.set_cell(buf, x, y, "<", Color::Black, Color::Gray); self.set_cell(buf, x, y, "<", Color::Black, Color::Gray);
} }
LevelElement::Unknown => { StructureElement::Unknown => {
self.set_cell(buf, x, y, "", Color::DarkGray, Color::Gray); self.set_cell(buf, x, y, "", Color::DarkGray, Color::Gray);
} }
LevelElement::Chest{gold} => { StructureElement::Start => {
self.set_cell(buf, x, y, "", Color::Black, Color::Gray);
}
LevelElement::Start => {
self.set_cell(buf, x, y, "α", Color::Black, Color::Gray); self.set_cell(buf, x, y, "α", Color::Black, Color::Gray);
} }
LevelElement::End => { StructureElement::End => {
self.set_cell(buf, x, y, "Ω", Color::Black, Color::Gray); self.set_cell(buf, x, y, "Ω", Color::Black, Color::Gray);
} }
} }
@ -128,7 +133,7 @@ impl<'a> Widget for LevelWidget<'a> {
#[test] #[test]
fn test_get_wall_symbol() { fn test_get_wall_symbol() {
let wall = Some(LevelElement::Wall); let wall = Some(StructureElement::Wall);
// 0 // 0
let s = LevelWidget::calculate_wall(None, None, None, None); let s = LevelWidget::calculate_wall(None, None, None, None);
assert_eq!(s, ""); assert_eq!(s, "");

View File

@ -51,6 +51,7 @@ fn test_get_name() {
let p = Player { let p = Player {
name: "Teddy Tester".to_string(), name: "Teddy Tester".to_string(),
position: Position::new(0, 1, 1), position: Position::new(0, 1, 1),
gold: 0,
life: 5, life: 5,
max_life: 10, max_life: 10,
}; };
@ -62,6 +63,7 @@ fn test_change_life() {
let mut p = Player { let mut p = Player {
name: "Teddy Tester".to_string(), name: "Teddy Tester".to_string(),
position: Position::new(0, 1, 1), position: Position::new(0, 1, 1),
gold: 0,
life: 5, life: 5,
max_life: 10, max_life: 10,
}; };