Extract constants and make monsters variable per level

This commit is contained in:
2024-10-26 17:00:51 +02:00
parent e490011b4e
commit b833b43c7c
6 changed files with 94 additions and 40 deletions

58
src/constants.rs Normal file
View File

@@ -0,0 +1,58 @@
use std::{collections::HashMap, ops::RangeInclusive};
use crate::monster::MonsterTypes;
/// the number of rooms in vertical direction
pub const ROOMS_VERTICAL: usize = 7;
/// the number of rooms in horizontal direction
pub const ROOMS_HORIZONTAL: usize = 7;
/// the width of a room in the grid of rooms (number of characters)
pub const ROOM_WIDTH: usize = 9;
/// the height of a room in the grid of rooms (number of characters)
pub const ROOM_HEIGHT: usize = 6;
/// How many levels does the dungeon have?
pub const LEVELS: usize = 3;
/// length of a game frame in ms
pub const FRAME_LENGTH: u64 = 100;
/// define the minimum width of a terminal to run the game, must be at least the level width plus some space for stats and messages
pub const MIN_WIDTH: u16 = 120;
/// define the minimum height of a terminal to run the game, this must be at least the level height!
pub const MIN_HEIGHT: u16 = LEVEL_HEIGHT as u16;
/// the calculated width of a level
pub const LEVEL_WIDTH: usize = 1 + ROOMS_VERTICAL * ROOM_WIDTH;
/// the calculated height of a level
pub const LEVEL_HEIGHT: usize = 1 + ROOMS_HORIZONTAL * ROOM_HEIGHT;
pub fn get_monsters_per_level() -> Vec<HashMap<MonsterTypes, std::ops::RangeInclusive<u8>>> {
let tmp = vec![
// level 1
vec![(MonsterTypes::Rat, 50), (MonsterTypes::Spider, 50)],
// level 2
vec![(MonsterTypes::Rat, 50), (MonsterTypes::Snake, 50)],
// level 3
vec![(MonsterTypes::Orc, 33), (MonsterTypes::Skeleton, 33), (MonsterTypes::Snake, 33)],
];
if tmp.len() != LEVELS {
panic!("Only {} monster sets defined for {} levels!", tmp.len(), LEVELS);
}
let mut result: Vec<HashMap<MonsterTypes, std::ops::RangeInclusive<u8>>> = vec![];
for (idx, level) in tmp.iter().enumerate() {
let mut sum = 0;
let mut map: HashMap<MonsterTypes, RangeInclusive<u8>> = HashMap::new();
for monster in level {
map.insert(monster.0, RangeInclusive::new(sum+1, sum+monster.1));
sum += monster.1;
}
if sum != 100 {
panic!("all percentages must add to 100 (was {}) per level, error in level {}!", sum, idx+1);
}
result.push(map);
}
result
}