Implement chest experiment one

This commit is contained in:
Joachim Lusiardi 2023-11-22 21:09:08 +01:00
parent 849749b6bb
commit 7a8fa84942
6 changed files with 58 additions and 17 deletions

View File

@ -24,22 +24,31 @@ impl Game {
} }
}; };
if can_go { if can_go {
if level.get_element(new_x, new_y).unwrap() == LevelElement::StairDown { match level.get_element(new_x, new_y).unwrap() {
let next_level = player_pos.get_level() + 1; LevelElement::StairDown => {
let start_x: usize = self.levels[next_level].start_x; let next_level = player_pos.get_level() + 1;
let start_y: usize = self.levels[next_level].start_y; let start_x: usize = self.levels[next_level].start_x;
self.player.set_position(next_level, start_x, start_y); let start_y: usize = self.levels[next_level].start_y;
self.levels[next_level].discover(self.player.get_position()); self.player.set_position(next_level, start_x, start_y);
} else if level.get_element(new_x, new_y).unwrap() == LevelElement::StairUp { self.levels[next_level].discover(self.player.get_position());
self.player.set_position( }
player_pos.get_level() - 1, LevelElement::StairUp => {
self.levels[player_pos.get_level() - 1].end_x, self.player.set_position(
self.levels[player_pos.get_level() - 1].end_y, player_pos.get_level() - 1,
); self.levels[player_pos.get_level() - 1].end_x,
} else { self.levels[player_pos.get_level() - 1].end_y,
self.player.change_position(dx, dy); );
self.levels[player_pos.get_level()].discover(self.player.get_position()); }
} 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());
}
};
} }
} }
} }

View File

@ -10,6 +10,9 @@ pub enum LevelElement {
Floor, Floor,
StairDown, StairDown,
StairUp, StairUp,
Start,
End,
Chest { gold: usize },
Unknown, Unknown,
} }
@ -20,6 +23,7 @@ pub enum RoomType {
StairUp, StairUp,
StairDown, StairDown,
BasicRoom, BasicRoom,
TreasureRoom,
EmptyRoom, EmptyRoom,
} }

View File

@ -91,6 +91,7 @@ impl LevelGenerator {
} else { } else {
room_types.push(RoomType::StairDown); room_types.push(RoomType::StairDown);
} }
room_types.push(RoomType::TreasureRoom);
// generate a random set of rooms and shuffle them // generate a random set of rooms and shuffle them
for _ in room_types.len()..ROOMS_HORIZONTAL * ROOMS_VERTICAL { for _ in room_types.len()..ROOMS_HORIZONTAL * ROOMS_VERTICAL {
match rng.gen_range(1..=6) { 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; 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 { 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 {
s[end_x][end_y] = LevelElement::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 { 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);
@ -251,6 +262,9 @@ impl LevelGenerator {
if room.kind == RoomType::StairUp { if room.kind == RoomType::StairUp {
s[start_x][start_y] = LevelElement::StairUp; s[start_x][start_y] = LevelElement::StairUp;
} }
if room.kind == RoomType::Start {
s[start_x][start_y] = LevelElement::Start;
}
} }
} }
// //

View File

@ -104,6 +104,15 @@ impl<'a> Widget for LevelWidget<'a> {
LevelElement::Unknown => { LevelElement::Unknown => {
self.set_cell(buf, x, y, "", Color::DarkGray, Color::Gray); 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);
}
} }
} }
} }

View File

@ -72,10 +72,11 @@ fn main() -> Result<()> {
height: 25, height: 25,
}; };
frame.render_widget( frame.render_widget(
Paragraph::new(format!("{}\nHealth: {}/{}\n{}/{}/{}", Paragraph::new(format!("{}\nHealth: {}/{}\nGold: {}\n{}/{}/{}",
g.player.get_name(), g.player.get_name(),
g.player.get_life(), g.player.get_life(),
g.player.get_max_life(), g.player.get_max_life(),
g.player.get_gold(),
g.player.get_position().get_level(), g.player.get_position().get_level(),
g.player.get_position().get_x(), g.player.get_position().get_x(),
g.player.get_position().get_y())) g.player.get_position().get_y()))

View File

@ -5,6 +5,7 @@ use crate::position::Position;
pub struct Player { pub struct Player {
name: String, name: String,
position: Position, position: Position,
gold: usize,
life: usize, life: usize,
max_life: usize, max_life: usize,
} }
@ -14,6 +15,7 @@ impl Player {
Player { Player {
name: name.to_string(), name: name.to_string(),
position: Position::new(0, 0, 0), position: Position::new(0, 0, 0),
gold: 0,
life: max_life, life: max_life,
max_life, max_life,
} }
@ -23,6 +25,8 @@ impl Player {
} }
pub fn get_life(&self) -> usize { self.life } pub fn get_life(&self) -> usize { self.life }
pub fn get_max_life(&self) -> usize { self.max_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) { pub fn change_life(&mut self, by: i16) {
self.life = max(0, self.life = max(0,
min(self.max_life, min(self.max_life,