here_be_dragons/demo/src/lib.rs

56 lines
1.4 KiB
Rust
Raw Normal View History

2020-09-12 12:44:37 +00:00
use wasm_bindgen::prelude::*;
2020-09-13 11:26:36 +00:00
use rand::prelude::*;
use js_sys::Date;
use mapgen::dungeon::{
MapBuilder,
2020-09-13 12:43:59 +00:00
map::TileType,
2020-09-13 11:26:36 +00:00
cellular_automata::CellularAutomataGen,
starting_point::{AreaStartingPosition, XStart, YStart},
cull_unreachable::CullUnreachable,
distant_exit::DistantExit,
};
2020-09-12 12:44:37 +00:00
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,
tiles: Vec<bool>,
2020-09-13 11:26:36 +00:00
}
#[wasm_bindgen]
impl World {
pub fn new(width: u32, height: u32) -> World {
let seed = Date::new_0().get_time() as u64;
let mut rng = StdRng::seed_from_u64(seed);
let map = MapBuilder::new(Box::new(CellularAutomataGen::new(width as usize, height as usize)))
.with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
.with(CullUnreachable::new())
.with(DistantExit::new())
.build_map_with_rng(&mut rng);
2020-09-13 12:43:59 +00:00
let tiles = (0..map.tiles.len()).map(|i| map.tiles[i] == TileType::Floor).collect();
World {
width,
height,
tiles }
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 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
}
pub fn tiles(&self) -> *const bool {
self.tiles.as_ptr()
2020-09-13 11:26:36 +00:00
}
2020-09-12 12:44:37 +00:00
}
2020-09-13 11:26:36 +00:00
// Called when the wasm module is instantiated
// #[wasm_bindgen(start)]
// pub fn main() -> Result<(), JsValue> {
// Ok(())
// }