here_be_dragons/demo/src/lib.rs

130 lines
4.2 KiB
Rust
Raw Normal View History

2020-09-12 12:44:37 +00:00
use wasm_bindgen::prelude::*;
2020-09-15 06:45:40 +00:00
use web_sys;
2020-09-13 11:26:36 +00:00
use rand::prelude::*;
2020-09-16 07:57:59 +00:00
use mapgen::{
map_builder::{
MapBuilder,
cellular_automata::CellularAutomataGen,
simple_rooms::SimpleRoomsGen,
bsp_interior::BspInteriorGen,
starting_point::{AreaStartingPosition, XStart, YStart},
cull_unreachable::CullUnreachable,
distant_exit::DistantExit,
rooms_corridors_nearest::NearestCorridors,
2020-09-16 09:42:59 +00:00
drunkard::DrunkardsWalkGen,
2020-09-16 07:57:59 +00:00
},
2020-09-13 12:43:59 +00:00
map::TileType,
2020-09-13 11:26:36 +00:00
};
2020-09-12 12:44:37 +00:00
2020-09-14 09:46:48 +00:00
#[wasm_bindgen]
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Cell {
Floor = 0,
Wall = 1,
}
2020-09-13 11:26:36 +00:00
#[wasm_bindgen]
pub struct World {
2020-09-13 12:43:59 +00:00
width: u32,
height: u32,
2020-09-14 09:46:48 +00:00
tiles: Vec<Cell>,
2020-09-13 11:26:36 +00:00
}
#[wasm_bindgen]
impl World {
2020-09-15 06:45:40 +00:00
2020-09-14 09:46:48 +00:00
pub fn new_cellular_automata(width: u32, height: u32, seed: u32) -> World {
2020-09-15 06:45:40 +00:00
World::print_map_info(format!("Cellular Automata with the seed: {}", seed));
2020-09-14 09:46:48 +00:00
let mut rng = StdRng::seed_from_u64(seed as u64);
2020-09-15 05:37:20 +00:00
let map = MapBuilder::new(CellularAutomataGen::new())
2020-09-13 11:26:36 +00:00
.with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
.with(CullUnreachable::new())
.with(DistantExit::new())
2020-09-14 18:29:36 +00:00
.build_map_with_rng(width as usize, height as usize, &mut rng);
let tiles = (0..map.tiles.len())
.map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall})
.collect();
World {
width,
height,
tiles }
}
2020-09-15 06:45:40 +00:00
pub fn new_simple_rooms(width: u32, height: u32, seed: u32) -> World {
World::print_map_info(format!("Simple Rooms with the seed: {}", seed));
2020-09-14 18:29:36 +00:00
let mut rng = StdRng::seed_from_u64(seed as u64);
2020-09-15 06:45:40 +00:00
let map = MapBuilder::new(SimpleRoomsGen::new())
2020-09-14 20:54:39 +00:00
.with(NearestCorridors::new())
2020-09-14 18:29:36 +00:00
.build_map_with_rng(width as usize, height as usize, &mut rng);
2020-09-14 09:46:48 +00:00
let tiles = (0..map.tiles.len())
.map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall})
.collect();
2020-09-13 12:43:59 +00:00
World {
width,
height,
tiles }
2020-09-13 11:26:36 +00:00
}
2020-09-12 12:44:37 +00:00
2020-09-15 20:33:18 +00:00
pub fn new_bsp_interior(width: u32, height: u32, seed: u32) -> World {
World::print_map_info(format!("BSP Interior with the seed: {}", seed));
2020-09-15 14:23:47 +00:00
let mut rng = StdRng::seed_from_u64(seed as u64);
2020-09-15 20:33:18 +00:00
let map = MapBuilder::new(BspInteriorGen::new())
2020-09-15 14:23:47 +00:00
.build_map_with_rng(width as usize, height as usize, &mut rng);
let tiles = (0..map.tiles.len())
.map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall})
.collect();
World {
width,
height,
tiles }
}
2020-09-16 09:42:59 +00:00
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(DrunkardsWalkGen::open_halls())
.build_map_with_rng(width as usize, height as usize, &mut rng);
let tiles = (0..map.tiles.len())
.map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall})
.collect();
World {
width,
height,
tiles }
}
2020-09-15 06:45:40 +00:00
pub fn new_random(width: u32, height: u32, seed: u32) -> World {
let mut rng = rand::thread_rng();
2020-09-15 14:23:47 +00:00
let px = rng.gen::<f32>();
2020-09-16 09:42:59 +00:00
if px < 0.25 {
2020-09-15 06:45:40 +00:00
World::new_cellular_automata(width, height, seed)
2020-09-16 09:42:59 +00:00
} else if px < 0.5 {
2020-09-15 06:45:40 +00:00
World::new_simple_rooms(width, height, seed)
2020-09-16 09:42:59 +00:00
} else if px < 0.75 {
World::new_drunkard(width, height, seed)
2020-09-15 14:23:47 +00:00
} else {
2020-09-15 20:33:18 +00:00
World::new_bsp_interior(width, height, seed)
2020-09-15 06:45:40 +00:00
}
}
2020-09-13 11:26:36 +00:00
pub fn width(&self) -> u32 {
2020-09-13 12:43:59 +00:00
self.width
2020-09-13 11:26:36 +00:00
}
2020-09-12 12:44:37 +00:00
2020-09-13 11:26:36 +00:00
pub fn height(&self) -> u32 {
2020-09-13 12:43:59 +00:00
self.height
}
2020-09-14 09:46:48 +00:00
pub fn tiles(&self) -> *const Cell {
2020-09-13 12:43:59 +00:00
self.tiles.as_ptr()
2020-09-13 11:26:36 +00:00
}
2020-09-15 06:45:40 +00:00
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);
}
2020-09-14 09:46:48 +00:00
}