here_be_dragons/src/dungeon/mod.rs

61 lines
1.5 KiB
Rust
Raw Normal View History

2020-09-01 12:46:31 +00:00
//! Generators for dungeon type maps.
//!
//! Generators can bu used directly or they can be combined with
//! `MapGenerator`s and `MapModifier`s
//!
//! * MapGenerators are use to create initial map.
//! * MapModifiers modify existing map.
//!
2020-08-31 09:45:59 +00:00
pub mod map;
2020-08-31 20:03:48 +00:00
pub mod cellular_automata;
use rand::prelude::*;
use map::Map;
2020-09-01 12:46:31 +00:00
/// Trait which should be implemented by any map generator which want to be used
/// by MapBuilder
2020-08-31 20:03:48 +00:00
pub trait MapGenerator {
fn generate_map(&self, rng: &mut StdRng) -> Map;
}
2020-09-01 12:46:31 +00:00
/// Trait which should be implemented by map modifier.
/// Modifier takes initiall map and apply changes to it.
2020-08-31 20:03:48 +00:00
pub trait MapModifier {
fn modify_map(&self, rng: &mut StdRng, map: &Map) -> Map;
}
2020-09-01 12:46:31 +00:00
/// Used to chain MapBuilder and MapModifiers to create the final map.
2020-08-31 20:03:48 +00:00
pub struct MapBuilder {
generator: Box<dyn MapGenerator>,
modifiers: Vec<Box<dyn MapModifier>>,
rng: StdRng,
}
impl MapBuilder {
pub fn new(generator : Box<dyn MapGenerator>) -> MapBuilder {
MapBuilder {
generator,
modifiers: Vec::new(),
rng: StdRng::seed_from_u64(0)
}
}
pub fn with(&mut self, modifier : Box<dyn MapModifier>) {
self.modifiers.push(modifier);
}
pub fn build_map(&mut self) -> Map {
let mut map = self.generator.generate_map(&mut self.rng);
// Build additional layers in turn
for modifier in self.modifiers.iter() {
modifier.modify_map(&mut self.rng, &mut map);
}
map
}
}