From 38dc7285c2b599d9913d3686fdc4e2baa7bbddad Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Thu, 24 Oct 2024 08:57:52 +0200 Subject: [PATCH] try to improve monster handling --- src/game.rs | 3 ++- src/level_generator.rs | 55 +++++++++++++++++++++++++++--------------- src/monster.rs | 11 +++++++++ 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/src/game.rs b/src/game.rs index cc7dcf8..5918854 100644 --- a/src/game.rs +++ b/src/game.rs @@ -3,7 +3,8 @@ use crate::level_generator::LevelGenerator; use crate::player::Player; use crate::position::{HasPosition, Position}; -pub const LEVELS: usize = 10; +/// How many levels does the dungeon have? +pub const LEVELS: usize = 2; #[derive(PartialEq)] /// represents a state of a game diff --git a/src/level_generator.rs b/src/level_generator.rs index e491a5d..cae1a5b 100644 --- a/src/level_generator.rs +++ b/src/level_generator.rs @@ -1,4 +1,5 @@ use std::cmp::{max, min}; +use std::collections::HashMap; use std::ops::Range; use petgraph::algo::min_spanning_tree; @@ -11,13 +12,13 @@ use rand::Rng; use crate::artifacts::{Artifact, Chest, Potion}; use crate::level::{Level, StructureElement}; -use crate::monster::{Monster, Orc, Rat, Snake}; +use crate::monster::{create_monster_by_type, Monster, Orc, Rat, Snake}; use crate::position::Position; -pub const ROOMS_VERTICAL: usize = 9; +pub const ROOMS_VERTICAL: usize = 7; pub const ROOMS_HORIZONTAL: usize = 7; -pub const ROOM_WIDTH: usize = 7; +pub const ROOM_WIDTH: usize = 9; pub const ROOM_HEIGHT: usize = 6; #[derive(PartialEq, Copy, Clone)] @@ -260,6 +261,33 @@ impl LevelGenerator { } true } + + fn select_monster(position: Position, rng: &mut ThreadRng) -> Box { + let level = position.get_level(); + let value = rng.gen_range(1..=100); + + let t = [ + // level 0 + HashMap::from([("Rat", 1..=100)]), + // level 1 + // HashMap::from([("Rat", 1..=50), ("Snake", 51..=100)]), + // // level 2 + // HashMap::from([("Rat", 1..=40)]), + ]; + if level < t.len() { + for (mtype, range) in &t[level] { + if range.contains(&value) { + return create_monster_by_type(mtype, position); + } + } + } + match rng.gen_range(1..=100) { + 1..=30 => Box::new(Orc::new_with_position(position)), + 31..=60 => Box::new(Snake::new_with_position(position)), + _ => Box::new(Rat::new_with_position(position)), + } + } + pub fn render(&self) -> Level { let mut rng = rand::thread_rng(); let mut structure = [[StructureElement::Wall; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; @@ -300,23 +328,10 @@ impl LevelGenerator { 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); // TODO randomize enemies here - match rng.gen_range(1..=100) { - 1..=30 => { - enemies.push(Box::new(Orc::new_with_position(Position::new( - self.level, t_x, t_y, - )))); - } - 31..=60 => { - enemies.push(Box::new(Snake::new_with_position(Position::new( - self.level, t_x, t_y, - )))); - } - _ => { - enemies.push(Box::new(Rat::new_with_position(Position::new( - self.level, t_x, t_y, - )))); - } - }; + enemies.push(LevelGenerator::select_monster( + Position::new(self.level, t_x, t_y), + &mut rng, + )); } if room.kind == RoomType::End || room.kind == RoomType::StairDown { diff --git a/src/monster.rs b/src/monster.rs index ecee504..cf1f4b2 100644 --- a/src/monster.rs +++ b/src/monster.rs @@ -103,6 +103,17 @@ impl Snake { } create_monster!(Snake); +pub fn create_monster_by_type(mtype: &str, position: Position) -> Box { + match mtype { + "Rat" => Box::new(Rat::new_with_position(position)), + "Orc" => Box::new(Orc::new_with_position(position)), + "Snake" => Box::new(Snake::new_with_position(position)), + &_ => { + panic!("Unknown species: {}", mtype) + } + } +} + #[test] fn monsters_can_move() { let mut m = Rat::new_with_position(Position::new(0, 0, 0));