Fixed #13. Show starting end exit point

This commit is contained in:
klangner 2020-10-18 21:40:40 +02:00
parent ee8f9aa273
commit 45c76e18c7
2 changed files with 65 additions and 14 deletions

View File

@ -1,7 +1,7 @@
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use web_sys; use web_sys;
use rand::prelude::*; use rand::prelude::*;
use mapgen::{Map, MapBuilder, TileType}; use mapgen::{Map, MapBuilder, TileType, geometry::Point};
use mapgen::filter::*; use mapgen::filter::*;
@ -18,16 +18,24 @@ pub struct World {
width: u32, width: u32,
height: u32, height: u32,
tiles: Vec<Cell>, tiles: Vec<Cell>,
map: Map,
} }
#[wasm_bindgen]
pub struct Position {
col: usize,
row: usize,
}
#[wasm_bindgen] #[wasm_bindgen]
impl World { 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()) 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();
World { width, height, tiles } World { width, height, tiles, map }
} }
pub fn new_cellular_automata(width: u32, height: u32, seed: u32) -> World { pub fn new_cellular_automata(width: u32, height: u32, seed: u32) -> World {
@ -40,7 +48,7 @@ impl World {
.with(CullUnreachable::new()) .with(CullUnreachable::new())
.with(DistantExit::new()) .with(DistantExit::new())
.build_with_rng(&mut rng); .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 { pub fn new_simple_rooms(width: u32, height: u32, seed: u32) -> World {
@ -49,8 +57,10 @@ impl World {
let map = MapBuilder::new(80, 50) let map = MapBuilder::new(80, 50)
.with(SimpleRooms::new()) .with(SimpleRooms::new())
.with(NearestCorridors::new()) .with(NearestCorridors::new())
.with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
.with(DistantExit::new())
.build_with_rng(&mut rng); .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 { 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 mut rng = StdRng::seed_from_u64(seed as u64);
let map = MapBuilder::new(80, 50) let map = MapBuilder::new(80, 50)
.with(BspInterior::new()) .with(BspInterior::new())
.with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
.with(CullUnreachable::new())
.with(DistantExit::new())
.build_with_rng(&mut rng); .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 { pub fn new_drunkard(width: u32, height: u32, seed: u32) -> World {
@ -69,8 +82,9 @@ impl World {
.with(DrunkardsWalk::open_halls()) .with(DrunkardsWalk::open_halls())
.with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER)) .with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
.with(CullUnreachable::new()) .with(CullUnreachable::new())
.with(DistantExit::new())
.build_with_rng(&mut rng); .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 { pub fn new_random(width: u32, height: u32, seed: u32) -> World {
@ -99,6 +113,16 @@ impl World {
self.tiles.as_ptr() 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) { fn print_map_info(info: String) {
let window = web_sys::window().expect("no global `window` exists"); let window = web_sys::window().expect("no global `window` exists");
let document = window.document().expect("should have a document on window"); let document = window.document().expect("should have a document on window");
@ -106,3 +130,18 @@ impl World {
div.set_inner_html(&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
}
}

View File

@ -87,13 +87,20 @@ const draw_tile = (ctx, row, col, tile_type) => {
if (tile_type == "floor") { if (tile_type == "floor") {
tile_x = 3; tile_x = 3;
tile_y = 2; tile_y = 2;
} else if (tile_type == "inner_wall") { } else if (tile_type == "wall") {
tile_x = 18;
tile_y = 0;
} else {
tile_x = 0; tile_x = 0;
tile_y = 3; 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( ctx.drawImage(
tiles_image, tiles_image,
tile_x * TILE_SIZE + 3, tile_x * TILE_SIZE + 3,
@ -111,8 +118,7 @@ const drawCells = () => {
const tilesPtr = world.tiles(); const tilesPtr = world.tiles();
const tiles = new Uint8Array(memory.buffer, tilesPtr, GRID_COLS * GRID_ROWS); const tiles = new Uint8Array(memory.buffer, tilesPtr, GRID_COLS * GRID_ROWS);
ctx.beginPath(); // tiles
for (let row = 0; row < GRID_ROWS; row++) { for (let row = 0; row < GRID_ROWS; row++) {
for (let col = 0; col < GRID_COLS; col++) { for (let col = 0; col < GRID_COLS; col++) {
const idx = getIndex(row, 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(); newRandomGen();