refactored MapBuilder

This commit is contained in:
klangner 2020-09-25 15:09:08 +02:00
parent be1d9e95bb
commit 4b8bb89110
4 changed files with 22 additions and 45 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "mapgen"
version = "0.3.0"
version = "0.4.0"
authors = ["Krzysztof Langner <klangner@gmail.com>"]
description = "Map generator for games (dungeons, worlds etc.)"
keywords = ["game", "map", "map-generator"]

View File

@ -26,13 +26,13 @@ impl World {
pub fn new_cellular_automata(width: u32, height: u32, seed: u32) -> World {
World::print_map_info(format!("Cellular Automata with the seed: {}", seed));
let mut rng = StdRng::seed_from_u64(seed as u64);
let map = MapBuilder::new()
let map = MapBuilder::new(80, 50)
.with(NoiseGenerator::uniform())
.with(CellularAutomata::new())
.with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
.with(CullUnreachable::new())
.with(DistantExit::new())
.build_map_with_rng(width as usize, height as usize, &mut rng);
.build_with_rng(&mut rng);
let tiles = (0..map.tiles.len())
.map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall})
.collect();
@ -45,10 +45,10 @@ impl World {
pub fn new_simple_rooms(width: u32, height: u32, seed: u32) -> World {
World::print_map_info(format!("Simple Rooms with the seed: {}", seed));
let mut rng = StdRng::seed_from_u64(seed as u64);
let map = MapBuilder::new()
let map = MapBuilder::new(80, 50)
.with(SimpleRooms::new())
.with(NearestCorridors::new())
.build_map_with_rng(width as usize, height as usize, &mut rng);
.build_with_rng(&mut rng);
let tiles = (0..map.tiles.len())
.map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall})
.collect();
@ -61,9 +61,9 @@ impl World {
pub fn new_bsp_interior(width: u32, height: u32, seed: u32) -> World {
World::print_map_info(format!("BSP Interior with the seed: {}", seed));
let mut rng = StdRng::seed_from_u64(seed as u64);
let map = MapBuilder::new()
let map = MapBuilder::new(80, 50)
.with(BspInterior::new())
.build_map_with_rng(width as usize, height as usize, &mut rng);
.build_with_rng(&mut rng);
let tiles = (0..map.tiles.len())
.map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall})
.collect();
@ -76,9 +76,9 @@ impl World {
pub fn new_drunkard(width: u32, height: u32, seed: u32) -> World {
World::print_map_info(format!("Drunkard with the seed: {}", seed));
let mut rng = StdRng::seed_from_u64(seed as u64);
let map = MapBuilder::new()
let map = MapBuilder::new(80, 50)
.with(DrunkardsWalk::open_halls())
.build_map_with_rng(width as usize, height as usize, &mut rng);
.build_with_rng(&mut rng);
let tiles = (0..map.tiles.len())
.map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall})
.collect();

View File

@ -1,32 +1,5 @@
//! 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.
//!
//! Example
//! ```
//! use mapgen::{MapFilter, MapBuilder, Map, TileType};
//! use mapgen::filter::{
//! NoiseGenerator,
//! CellularAutomata,
//! starting_point::{AreaStartingPosition, XStart, YStart}
//! };
//! use mapgen::geometry::Point;
//!
//! let map = MapBuilder::new()
//! .with(NoiseGenerator::uniform())
//! .with(CellularAutomata::new())
//! .with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
//! .build_map(80, 50);
//!
//! assert_eq!(map.width, 80);
//! assert_eq!(map.height, 50);
//! assert_eq!(map.starting_point.is_some(), true);
//! ```
//!
pub mod bsp_interior;
pub mod bsp_rooms;

View File

@ -16,11 +16,11 @@
//! };
//! use mapgen::geometry::Point;
//!
//! let map = MapBuilder::new()
//! let map = MapBuilder::new(80, 50)
//! .with(NoiseGenerator::uniform())
//! .with(CellularAutomata::new())
//! .with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
//! .build_map(80, 50);
//! .build();
//!
//! assert_eq!(map.width, 80);
//! assert_eq!(map.height, 50);
@ -50,13 +50,17 @@ pub trait MapFilter {
/// Used to chain MapBuilder and MapModifiers to create the final map.
pub struct MapBuilder {
width: usize,
height: usize,
modifiers: Vec<Box<dyn MapFilter>>,
}
impl MapBuilder {
/// Create Map Builder with initial map generator
pub fn new() -> MapBuilder {
pub fn new(width: usize, height: usize) -> MapBuilder {
MapBuilder {
width,
height,
modifiers: Vec::new(),
}
}
@ -67,15 +71,15 @@ impl MapBuilder {
}
/// Build map using random number seeded with system time
pub fn build_map(&mut self, width: usize, height: usize) -> Map {
pub fn build(&mut self) -> Map {
let system_time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Can't access system time");
let mut rng = StdRng::seed_from_u64(system_time.as_millis() as u64);
self.build_map_with_rng(width, height, &mut rng)
self.build_with_rng(&mut rng)
}
/// Build map using provided random number generator
pub fn build_map_with_rng(&mut self, width: usize, height: usize, rng: &mut StdRng) -> Map {
let mut map = Map::new(width, height);
pub fn build_with_rng(&mut self, rng: &mut StdRng) -> Map {
let mut map = Map::new(self.width, self.height);
// Build additional layers in turn
for modifier in self.modifiers.iter() {
@ -101,11 +105,11 @@ mod tests {
#[test]
fn test_ca_map() {
let map = MapBuilder::new()
let map = MapBuilder::new(80, 50)
.with(NoiseGenerator::new(0.55))
.with(CellularAutomata::new())
.with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
.build_map(80, 50);
.build();
assert_eq!(map.width, 80);
assert_eq!(map.height, 50);