refactored MapBuilder
This commit is contained in:
parent
be1d9e95bb
commit
4b8bb89110
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "mapgen"
|
name = "mapgen"
|
||||||
version = "0.3.0"
|
version = "0.4.0"
|
||||||
authors = ["Krzysztof Langner <klangner@gmail.com>"]
|
authors = ["Krzysztof Langner <klangner@gmail.com>"]
|
||||||
description = "Map generator for games (dungeons, worlds etc.)"
|
description = "Map generator for games (dungeons, worlds etc.)"
|
||||||
keywords = ["game", "map", "map-generator"]
|
keywords = ["game", "map", "map-generator"]
|
||||||
|
|
|
@ -26,13 +26,13 @@ impl World {
|
||||||
pub fn new_cellular_automata(width: u32, height: u32, seed: u32) -> World {
|
pub fn new_cellular_automata(width: u32, height: u32, seed: u32) -> World {
|
||||||
World::print_map_info(format!("Cellular Automata with the seed: {}", seed));
|
World::print_map_info(format!("Cellular Automata with the seed: {}", seed));
|
||||||
let mut rng = StdRng::seed_from_u64(seed as u64);
|
let mut rng = StdRng::seed_from_u64(seed as u64);
|
||||||
let map = MapBuilder::new()
|
let map = MapBuilder::new(80, 50)
|
||||||
.with(NoiseGenerator::uniform())
|
.with(NoiseGenerator::uniform())
|
||||||
.with(CellularAutomata::new())
|
.with(CellularAutomata::new())
|
||||||
.with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
|
.with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
|
||||||
.with(CullUnreachable::new())
|
.with(CullUnreachable::new())
|
||||||
.with(DistantExit::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())
|
let tiles = (0..map.tiles.len())
|
||||||
.map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall})
|
.map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall})
|
||||||
.collect();
|
.collect();
|
||||||
|
@ -45,10 +45,10 @@ impl World {
|
||||||
pub fn new_simple_rooms(width: u32, height: u32, seed: u32) -> World {
|
pub fn new_simple_rooms(width: u32, height: u32, seed: u32) -> World {
|
||||||
World::print_map_info(format!("Simple Rooms with the seed: {}", seed));
|
World::print_map_info(format!("Simple Rooms with the seed: {}", seed));
|
||||||
let mut rng = StdRng::seed_from_u64(seed as u64);
|
let mut rng = StdRng::seed_from_u64(seed as u64);
|
||||||
let map = MapBuilder::new()
|
let map = MapBuilder::new(80, 50)
|
||||||
.with(SimpleRooms::new())
|
.with(SimpleRooms::new())
|
||||||
.with(NearestCorridors::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())
|
let tiles = (0..map.tiles.len())
|
||||||
.map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall})
|
.map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall})
|
||||||
.collect();
|
.collect();
|
||||||
|
@ -61,9 +61,9 @@ impl World {
|
||||||
pub fn new_bsp_interior(width: u32, height: u32, seed: u32) -> World {
|
pub fn new_bsp_interior(width: u32, height: u32, seed: u32) -> World {
|
||||||
World::print_map_info(format!("BSP Interior with the seed: {}", seed));
|
World::print_map_info(format!("BSP Interior with the seed: {}", seed));
|
||||||
let mut rng = StdRng::seed_from_u64(seed as u64);
|
let mut rng = StdRng::seed_from_u64(seed as u64);
|
||||||
let map = MapBuilder::new()
|
let map = MapBuilder::new(80, 50)
|
||||||
.with(BspInterior::new())
|
.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())
|
let tiles = (0..map.tiles.len())
|
||||||
.map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall})
|
.map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall})
|
||||||
.collect();
|
.collect();
|
||||||
|
@ -76,9 +76,9 @@ impl World {
|
||||||
pub fn new_drunkard(width: u32, height: u32, seed: u32) -> World {
|
pub fn new_drunkard(width: u32, height: u32, seed: u32) -> World {
|
||||||
World::print_map_info(format!("Drunkard with the seed: {}", seed));
|
World::print_map_info(format!("Drunkard with the seed: {}", seed));
|
||||||
let mut rng = StdRng::seed_from_u64(seed as u64);
|
let mut rng = StdRng::seed_from_u64(seed as u64);
|
||||||
let map = MapBuilder::new()
|
let map = MapBuilder::new(80, 50)
|
||||||
.with(DrunkardsWalk::open_halls())
|
.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())
|
let tiles = (0..map.tiles.len())
|
||||||
.map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall})
|
.map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
|
@ -1,32 +1,5 @@
|
||||||
//! Generators for dungeon type maps.
|
//! 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_interior;
|
||||||
pub mod bsp_rooms;
|
pub mod bsp_rooms;
|
||||||
|
|
22
src/lib.rs
22
src/lib.rs
|
@ -16,11 +16,11 @@
|
||||||
//! };
|
//! };
|
||||||
//! use mapgen::geometry::Point;
|
//! use mapgen::geometry::Point;
|
||||||
//!
|
//!
|
||||||
//! let map = MapBuilder::new()
|
//! let map = MapBuilder::new(80, 50)
|
||||||
//! .with(NoiseGenerator::uniform())
|
//! .with(NoiseGenerator::uniform())
|
||||||
//! .with(CellularAutomata::new())
|
//! .with(CellularAutomata::new())
|
||||||
//! .with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
|
//! .with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
|
||||||
//! .build_map(80, 50);
|
//! .build();
|
||||||
//!
|
//!
|
||||||
//! assert_eq!(map.width, 80);
|
//! assert_eq!(map.width, 80);
|
||||||
//! assert_eq!(map.height, 50);
|
//! assert_eq!(map.height, 50);
|
||||||
|
@ -50,13 +50,17 @@ pub trait MapFilter {
|
||||||
|
|
||||||
/// Used to chain MapBuilder and MapModifiers to create the final map.
|
/// Used to chain MapBuilder and MapModifiers to create the final map.
|
||||||
pub struct MapBuilder {
|
pub struct MapBuilder {
|
||||||
|
width: usize,
|
||||||
|
height: usize,
|
||||||
modifiers: Vec<Box<dyn MapFilter>>,
|
modifiers: Vec<Box<dyn MapFilter>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MapBuilder {
|
impl MapBuilder {
|
||||||
/// Create Map Builder with initial map generator
|
/// Create Map Builder with initial map generator
|
||||||
pub fn new() -> MapBuilder {
|
pub fn new(width: usize, height: usize) -> MapBuilder {
|
||||||
MapBuilder {
|
MapBuilder {
|
||||||
|
width,
|
||||||
|
height,
|
||||||
modifiers: Vec::new(),
|
modifiers: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,15 +71,15 @@ impl MapBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build map using random number seeded with system time
|
/// 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 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);
|
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
|
/// Build map using provided random number generator
|
||||||
pub fn build_map_with_rng(&mut self, width: usize, height: usize, rng: &mut StdRng) -> Map {
|
pub fn build_with_rng(&mut self, rng: &mut StdRng) -> Map {
|
||||||
let mut map = Map::new(width, height);
|
let mut map = Map::new(self.width, self.height);
|
||||||
|
|
||||||
// Build additional layers in turn
|
// Build additional layers in turn
|
||||||
for modifier in self.modifiers.iter() {
|
for modifier in self.modifiers.iter() {
|
||||||
|
@ -101,11 +105,11 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ca_map() {
|
fn test_ca_map() {
|
||||||
let map = MapBuilder::new()
|
let map = MapBuilder::new(80, 50)
|
||||||
.with(NoiseGenerator::new(0.55))
|
.with(NoiseGenerator::new(0.55))
|
||||||
.with(CellularAutomata::new())
|
.with(CellularAutomata::new())
|
||||||
.with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
|
.with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
|
||||||
.build_map(80, 50);
|
.build();
|
||||||
|
|
||||||
assert_eq!(map.width, 80);
|
assert_eq!(map.width, 80);
|
||||||
assert_eq!(map.height, 50);
|
assert_eq!(map.height, 50);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user