From c6492c28c2b12a4b2fc6c048df1a492fac34438f Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Wed, 30 Oct 2024 08:56:45 +0100 Subject: [PATCH] work on level generator --- src/constants.rs | 3 +- src/level.rs | 1 + src/level_generator.rs | 91 +++++++++++++++++++++++++++++++----------- src/level_widget.rs | 9 +++-- src/room.rs | 12 ++---- 5 files changed, 79 insertions(+), 37 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 9a4bc16..c465116 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -73,7 +73,8 @@ pub fn get_room_type_per_level() -> Vec Box { + let level = position.get_level(); + let value = rng.gen_range(1..=100); + + let t = get_monsters_per_level(); + if level < t.len() { + for (mtype, range) in &t[level] { + if range.contains(&value) { + return create_monster_by_type(mtype, position); + } + } + } + panic!("no monster selectable!"); } fn select_room_type(level: usize, rng: &mut ThreadRng) -> RoomType { @@ -216,18 +240,37 @@ impl LevelGenerator { let mut structure = [[StructureElement::Wall; LEVEL_HEIGHT]; LEVEL_WIDTH]; let mut start_pos = (0, 0); let mut end_pos = (0, 0); + let mut monsters: Vec> = Vec::with_capacity(10); + let mut artifacts: Vec> = Vec::with_capacity(10); for col in 0..ROOMS_HORIZONAL { for row in 0..ROOMS_VERTICAL { - let position = self.rooms[col][row].render(&mut structure, col, row); - if self.rooms[col][row].kind == RoomType::Start - || self.rooms[col][row].kind == RoomType::StairUp - { - start_pos = position; - } - if self.rooms[col][row].kind == RoomType::End - || self.rooms[col][row].kind == RoomType::StairDown - { - end_pos = position; + let room = self.rooms[col][row]; + let position = room.render(&mut structure, col, row); + match room.kind { + RoomType::Start => {start_pos=position}, + RoomType::End => {end_pos=position}, + RoomType::StairUp => {start_pos=position}, + RoomType::StairDown => {end_pos=position}, + RoomType::BasicRoom => {}, + RoomType::ArtifactRoom => { + match self.rng.gen_range(1..=100) { + 1..=50 => { + artifacts + .push(Box::new(Chest::new(Position::new(self.level, position.0, position.1)))); + } + _ => { + artifacts + .push(Box::new(Potion::new(Position::new(self.level, position.0, position.1)))); + } + }; + }, + RoomType::MonsterRoom => { + monsters.push(LevelGenerator::select_monster( + Position::new(self.level, position.0, position.1), + &mut self.rng, + )); + }, + RoomType::EmptyRoom => {}, } } } @@ -247,8 +290,8 @@ impl LevelGenerator { level: self.level, structure, discovered: [[false; LEVEL_HEIGHT]; LEVEL_WIDTH], - monsters: vec![], - artifacts: vec![], + monsters, + artifacts, start: start_pos, end: end_pos, rng: rand::thread_rng(), diff --git a/src/level_widget.rs b/src/level_widget.rs index 9204fa3..676e74d 100644 --- a/src/level_widget.rs +++ b/src/level_widget.rs @@ -1,6 +1,6 @@ use ratatui::buffer::Buffer; use ratatui::layout::Rect; -use ratatui::style::Color; +use ratatui::style::{Color, Modifier, Style}; use ratatui::widgets::{StatefulWidget, Widget}; use crate::game::Game; @@ -15,6 +15,9 @@ impl LevelWidget { fn set_cell(&self, buf: &mut Buffer, x: u16, y: u16, symbol: &str, fg: Color, bg: Color) { buf[(x, y)].set_symbol(symbol).set_bg(bg).set_fg(fg); } + fn set_bold_cell(&self, buf: &mut Buffer, x: u16, y: u16, symbol: &str, fg: Color, bg: Color) { + buf[(x, y)].set_symbol(symbol).set_bg(bg).set_fg(fg).set_style(Style::new().add_modifier(Modifier::BOLD)); + } } impl StatefulWidget for LevelWidget { @@ -46,7 +49,7 @@ impl StatefulWidget for LevelWidget { } StructureElement::Wall => { // TODO add fancy walls with https://en.wikipedia.org/wiki/Box-drawing_characters - self.set_cell(buf, x, y, "#", FG_BROWN, Color::Gray); + self.set_cell(buf, x, y, "#", FG_BROWN, FG_BROWN); } StructureElement::Floor => { self.set_cell(buf, x, y, " ", FG_BROWN, Color::Gray); @@ -68,7 +71,7 @@ impl StatefulWidget for LevelWidget { } (_, _, Some(t)) => { let (s, c) = t.get_representation(); - self.set_cell(buf, x, y, s, c, Color::Gray); + self.set_bold_cell(buf, x, y, s, c, Color::Gray); } (None, None, None) => {} }; diff --git a/src/room.rs b/src/room.rs index 231248e..e7ee3cc 100644 --- a/src/room.rs +++ b/src/room.rs @@ -119,25 +119,19 @@ impl Room { match self.kind { RoomType::Start => { tgt[left + self.special.0][top + self.special.1] = StructureElement::Start; - (left + self.special.0, top + self.special.1) } RoomType::End => { tgt[left + self.special.0][top + self.special.1] = StructureElement::End; - (left + self.special.0, top + self.special.1) } RoomType::StairUp => { tgt[left + self.special.0][top + self.special.1] = StructureElement::StairUp; - (left + self.special.0, top + self.special.1) } RoomType::StairDown => { tgt[left + self.special.0][top + self.special.1] = StructureElement::StairDown; - (left + self.special.0, top + self.special.1) } - RoomType::BasicRoom => (0, 0), - RoomType::ArtifactRoom => (0, 0), - RoomType::MonsterRoom => (0, 0), - RoomType::EmptyRoom => (0, 0), - } + _ => {} + }; + (left + self.special.0, top + self.special.1) } }