diff --git a/demo/src/lib.rs b/demo/src/lib.rs index a640610..badab17 100644 --- a/demo/src/lib.rs +++ b/demo/src/lib.rs @@ -1,7 +1,7 @@ use wasm_bindgen::prelude::*; use web_sys; use rand::prelude::*; -use mapgen::{Map, MapBuilder, TileType}; +use mapgen::{Map, MapBuilder, TileType, geometry::Point}; use mapgen::filter::*; @@ -18,16 +18,24 @@ pub struct World { width: u32, height: u32, tiles: Vec, + map: Map, } +#[wasm_bindgen] +pub struct Position { + col: usize, + row: usize, +} + + #[wasm_bindgen] impl World { - fn new(width: u32, height: u32, map: &Map) -> World { + fn new(width: u32, height: u32, map: Map) -> World { let tiles = (0..map.tiles.len()) .map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall}) .collect(); - World { width, height, tiles } + World { width, height, tiles, map } } pub fn new_cellular_automata(width: u32, height: u32, seed: u32) -> World { @@ -40,7 +48,7 @@ impl World { .with(CullUnreachable::new()) .with(DistantExit::new()) .build_with_rng(&mut rng); - World::new(width, height, &map) + World::new(width, height, map) } pub fn new_simple_rooms(width: u32, height: u32, seed: u32) -> World { @@ -49,8 +57,10 @@ impl World { let map = MapBuilder::new(80, 50) .with(SimpleRooms::new()) .with(NearestCorridors::new()) + .with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER)) + .with(DistantExit::new()) .build_with_rng(&mut rng); - World::new(width, height, &map) + World::new(width, height, map) } pub fn new_bsp_interior(width: u32, height: u32, seed: u32) -> World { @@ -58,8 +68,11 @@ impl World { let mut rng = StdRng::seed_from_u64(seed as u64); let map = MapBuilder::new(80, 50) .with(BspInterior::new()) + .with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER)) + .with(CullUnreachable::new()) + .with(DistantExit::new()) .build_with_rng(&mut rng); - World::new(width, height, &map) + World::new(width, height, map) } pub fn new_drunkard(width: u32, height: u32, seed: u32) -> World { @@ -69,8 +82,9 @@ impl World { .with(DrunkardsWalk::open_halls()) .with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER)) .with(CullUnreachable::new()) + .with(DistantExit::new()) .build_with_rng(&mut rng); - World::new(width, height, &map) + World::new(width, height, map) } pub fn new_random(width: u32, height: u32, seed: u32) -> World { @@ -99,10 +113,35 @@ impl World { self.tiles.as_ptr() } + pub fn player_pos(&self) -> Position { + let p = self.map.starting_point.unwrap_or(Point::new(0, 0)); + Position { col: p.x, row: p.y } + } + + pub fn exit_pos(&self) -> Position { + let p = self.map.exit_point.unwrap_or(Point::new(0, 0)); + Position { col: p.x, row: p.y } + } + 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); } +} + +#[wasm_bindgen] +impl Position { + pub fn new(col: usize, row: usize) -> Position { + Position { col, row } + } + + pub fn col(&self) -> usize { + self.col + } + + pub fn row(&self) -> usize { + self.row + } } \ No newline at end of file diff --git a/demo/www/index.js b/demo/www/index.js index fcbee6c..b571b59 100644 --- a/demo/www/index.js +++ b/demo/www/index.js @@ -87,13 +87,20 @@ const draw_tile = (ctx, row, col, tile_type) => { if (tile_type == "floor") { tile_x = 3; tile_y = 2; - } else if (tile_type == "inner_wall") { - tile_x = 18; - tile_y = 0; - } else { + } else if (tile_type == "wall") { tile_x = 0; tile_y = 3; + } else if (tile_type == "player") { + tile_x = 0; + tile_y = 8; + } else if (tile_type == "exit") { + tile_x = 10; + tile_y = 1; + } else { + tile_x = 18; + tile_y = 0; } + ctx.drawImage( tiles_image, tile_x * TILE_SIZE + 3, @@ -111,8 +118,7 @@ const drawCells = () => { const tilesPtr = world.tiles(); const tiles = new Uint8Array(memory.buffer, tilesPtr, GRID_COLS * GRID_ROWS); - ctx.beginPath(); - + // tiles for (let row = 0; row < GRID_ROWS; row++) { for (let col = 0; col < GRID_COLS; col++) { const idx = getIndex(row, col); @@ -126,7 +132,13 @@ const drawCells = () => { } } - ctx.stroke(); + // Player position + let player = world.player_pos(); + draw_tile(ctx, player.row(), player.col(), "player"); + + // Exit position + let exit = world.exit_pos(); + draw_tile(ctx, exit.row(), exit.col(), "exit"); }; newRandomGen();