try to improve monster handling

This commit is contained in:
Joachim Lusiardi 2024-10-24 08:57:52 +02:00
parent 6f4c37728f
commit 38dc7285c2
3 changed files with 48 additions and 21 deletions

View File

@ -3,7 +3,8 @@ use crate::level_generator::LevelGenerator;
use crate::player::Player; use crate::player::Player;
use crate::position::{HasPosition, Position}; use crate::position::{HasPosition, Position};
pub const LEVELS: usize = 10; /// How many levels does the dungeon have?
pub const LEVELS: usize = 2;
#[derive(PartialEq)] #[derive(PartialEq)]
/// represents a state of a game /// represents a state of a game

View File

@ -1,4 +1,5 @@
use std::cmp::{max, min}; use std::cmp::{max, min};
use std::collections::HashMap;
use std::ops::Range; use std::ops::Range;
use petgraph::algo::min_spanning_tree; use petgraph::algo::min_spanning_tree;
@ -11,13 +12,13 @@ use rand::Rng;
use crate::artifacts::{Artifact, Chest, Potion}; use crate::artifacts::{Artifact, Chest, Potion};
use crate::level::{Level, StructureElement}; 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; use crate::position::Position;
pub const ROOMS_VERTICAL: usize = 9; pub const ROOMS_VERTICAL: usize = 7;
pub const ROOMS_HORIZONTAL: 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; pub const ROOM_HEIGHT: usize = 6;
#[derive(PartialEq, Copy, Clone)] #[derive(PartialEq, Copy, Clone)]
@ -260,6 +261,33 @@ impl LevelGenerator {
} }
true true
} }
fn select_monster(position: Position, rng: &mut ThreadRng) -> Box<dyn Monster> {
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 { pub fn render(&self) -> Level {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let mut structure = [[StructureElement::Wall; 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT]; 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_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);
// TODO randomize enemies here // TODO randomize enemies here
match rng.gen_range(1..=100) { enemies.push(LevelGenerator::select_monster(
1..=30 => { Position::new(self.level, t_x, t_y),
enemies.push(Box::new(Orc::new_with_position(Position::new( &mut rng,
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,
))));
}
};
} }
if room.kind == RoomType::End || room.kind == RoomType::StairDown { if room.kind == RoomType::End || room.kind == RoomType::StairDown {

View File

@ -103,6 +103,17 @@ impl Snake {
} }
create_monster!(Snake); create_monster!(Snake);
pub fn create_monster_by_type(mtype: &str, position: Position) -> Box<dyn Monster> {
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] #[test]
fn monsters_can_move() { fn monsters_can_move() {
let mut m = Rat::new_with_position(Position::new(0, 0, 0)); let mut m = Rat::new_with_position(Position::new(0, 0, 0));