Random Rooms renamed to Simple Rooms

This commit is contained in:
klangner 2020-09-15 08:45:40 +02:00
parent 113c1837df
commit 36a72c5789
6 changed files with 45 additions and 18 deletions

View File

@ -19,7 +19,7 @@ Generate procedural maps for games. [Try it in the browser](https://klangner.git
* [ ] Drunkard's walk
* [ ] Maze
* [ ] Prefabs
* [x] Random rooms
* [x] Simple rooms
* [ ] Voronoi hive
* [ ] Wave Function Collapse
* Map modifiers (filters)

View File

@ -1,10 +1,11 @@
use wasm_bindgen::prelude::*;
use web_sys;
use rand::prelude::*;
use mapgen::dungeon::{
MapBuilder,
map::TileType,
cellular_automata::CellularAutomataGen,
random_rooms::RandomRoomsGen,
simple_rooms::SimpleRoomsGen,
starting_point::{AreaStartingPosition, XStart, YStart},
cull_unreachable::CullUnreachable,
distant_exit::DistantExit,
@ -29,7 +30,9 @@ pub struct World {
#[wasm_bindgen]
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(CellularAutomataGen::new())
.with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
@ -45,9 +48,10 @@ impl World {
tiles }
}
pub fn new_random_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));
let mut rng = StdRng::seed_from_u64(seed as u64);
let map = MapBuilder::new(RandomRoomsGen::new())
let map = MapBuilder::new(SimpleRoomsGen::new())
.with(NearestCorridors::new())
.build_map_with_rng(width as usize, height as usize, &mut rng);
let tiles = (0..map.tiles.len())
@ -59,6 +63,15 @@ impl World {
tiles }
}
pub fn new_random(width: u32, height: u32, seed: u32) -> World {
let mut rng = rand::thread_rng();
if rng.gen::<f32>() < 0.5 {
World::new_cellular_automata(width, height, seed)
} else {
World::new_simple_rooms(width, height, seed)
}
}
pub fn width(&self) -> u32 {
self.width
}
@ -70,4 +83,11 @@ impl World {
pub fn tiles(&self) -> *const Cell {
self.tiles.as_ptr()
}
fn print_map_info(info: String) {
let window = web_sys::window().expect("no global `window` exists");
let document = window.document().expect("should have a document on window");
let div = document.get_element_by_id("map-info").expect("Need div with id: map-info");
div.set_inner_html(&info);
}
}

View File

@ -35,8 +35,9 @@
Select generator
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#" id="cellular-automata-option">Cellular Automata</a>
<a class="dropdown-item" id="cellular-automata-option">Cellular Automata</a>
<a class="dropdown-item" id="simple-rooms-option">Simple Rooms</a>
<a class="dropdown-item" id="random-option">Random Generator</a>
</div>
</li>
</ul>

View File

@ -24,14 +24,18 @@ function newCellularAutomata() {
var seed = Date.now();
world = World.new_cellular_automata(width, height, seed);
requestAnimationFrame(renderLoop);
infoDiv.textContent = "Cellular Automata with the seed: " + seed;
}
function newSimpleRooms() {
var seed = Date.now();
world = World.new_random_rooms(width, height, seed);
world = World.new_simple_rooms(width, height, seed);
requestAnimationFrame(renderLoop);
}
function newRandomGen() {
var seed = Date.now();
world = World.new_random(width, height, seed);
requestAnimationFrame(renderLoop);
infoDiv.textContent = "Random Rooms with the seed: " + seed;
}
const renderLoop = () => {
@ -92,8 +96,9 @@ const drawCells = () => {
ctx.stroke();
};
newCellularAutomata();
newRandomGen();
// Connect UI element
document.getElementById('cellular-automata-option').addEventListener('click', newCellularAutomata);
document.getElementById('simple-rooms-option').addEventListener('click', newSimpleRooms);
document.getElementById('random-option').addEventListener('click', newRandomGen);

View File

@ -30,7 +30,7 @@ pub mod map;
pub mod cellular_automata;
pub mod cull_unreachable;
pub mod distant_exit;
pub mod random_rooms;
pub mod simple_rooms;
pub mod rooms_corridors_nearest;
pub mod starting_point;
mod dijkstra;

View File

@ -1,17 +1,18 @@
//! Random rooms map generator.
//!
//! Try to generate rooms of different size to fille the whole map area.
//! Try to generate rooms of different size to fill the map area.
//! Rooms will not overlap.
//!
//! Example generator usage:
//! ```
//! use rand::prelude::*;
//! use mapgen::dungeon::{
//! MapGenerator,
//! random_rooms::RandomRoomsGen
//! simple_rooms::SimpleRoomsGen
//! };
//!
//! let mut rng = StdRng::seed_from_u64(100);
//! let gen = RandomRoomsGen::new();
//! let gen = SimpleRoomsGen::new();
//! let map = gen.generate_map(80, 50, &mut rng);
//!
//! assert_eq!(map.width, 80);
@ -26,22 +27,22 @@ use crate::common::random;
use super::map::{Map};
pub struct RandomRoomsGen {
pub struct SimpleRoomsGen {
max_rooms: usize,
min_room_size: usize,
max_room_size: usize,
}
impl MapGenerator for RandomRoomsGen {
impl MapGenerator for SimpleRoomsGen {
fn generate_map(&self, width: usize, height: usize, rng : &mut StdRng) -> Map {
self.build_rooms(width, height, rng)
}
}
impl RandomRoomsGen {
pub fn new() -> Box<RandomRoomsGen> {
Box::new(RandomRoomsGen{
impl SimpleRoomsGen {
pub fn new() -> Box<SimpleRoomsGen> {
Box::new(SimpleRoomsGen{
max_rooms: 30,
min_room_size: 6,
max_room_size: 10