diff --git a/demo/src/lib.rs b/demo/src/lib.rs index 993c453..1746e6f 100644 --- a/demo/src/lib.rs +++ b/demo/src/lib.rs @@ -1,6 +1,5 @@ use wasm_bindgen::prelude::*; use rand::prelude::*; -use js_sys::Date; use mapgen::dungeon::{ MapBuilder, map::TileType, @@ -11,24 +10,33 @@ use mapgen::dungeon::{ }; +#[wasm_bindgen] +#[repr(u8)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Cell { + Floor = 0, + Wall = 1, +} + #[wasm_bindgen] pub struct World { width: u32, height: u32, - tiles: Vec, + tiles: Vec, } #[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); + pub fn new_cellular_automata(width: u32, height: u32, seed: u32) -> World { + let mut rng = StdRng::seed_from_u64(seed as u64); 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); - let tiles = (0..map.tiles.len()).map(|i| map.tiles[i] == TileType::Floor).collect(); + let tiles = (0..map.tiles.len()) + .map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall}) + .collect(); World { width, height, @@ -43,14 +51,7 @@ impl World { self.height } - pub fn tiles(&self) -> *const bool { + pub fn tiles(&self) -> *const Cell { self.tiles.as_ptr() } -} - - -// Called when the wasm module is instantiated -// #[wasm_bindgen(start)] -// pub fn main() -> Result<(), JsValue> { -// Ok(()) -// } \ No newline at end of file +} \ No newline at end of file diff --git a/demo/www b/demo/www deleted file mode 160000 index 9ac3dff..0000000 --- a/demo/www +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 9ac3dff9ebea4675e5c478bcdcbc0fd547d1529f diff --git a/demo/www2/.gitignore b/demo/www2/.gitignore new file mode 100644 index 0000000..4104484 --- /dev/null +++ b/demo/www2/.gitignore @@ -0,0 +1,4 @@ +node_modules +dist +.bin/ +package-lock.json \ No newline at end of file diff --git a/demo/www2/bootstrap.js b/demo/www2/bootstrap.js new file mode 100644 index 0000000..7934d62 --- /dev/null +++ b/demo/www2/bootstrap.js @@ -0,0 +1,5 @@ +// A dependency graph that contains any wasm must all be imported +// asynchronously. This `bootstrap.js` file does the single async import, so +// that no one else needs to worry about it again. +import("./index.js") + .catch(e => console.error("Error importing `index.js`:", e)); diff --git a/demo/www2/favicon.ico b/demo/www2/favicon.ico new file mode 100644 index 0000000..d9eaec7 Binary files /dev/null and b/demo/www2/favicon.ico differ diff --git a/demo/www2/index.html b/demo/www2/index.html new file mode 100644 index 0000000..16c15cf --- /dev/null +++ b/demo/www2/index.html @@ -0,0 +1,49 @@ + + + + + Mapgen demo + + + + + + + + +
+ + + + + + diff --git a/demo/www2/index.js b/demo/www2/index.js new file mode 100644 index 0000000..95e7d32 --- /dev/null +++ b/demo/www2/index.js @@ -0,0 +1,98 @@ +import {Cell, World} from "mapgen-demo"; +import { memory } from "mapgen-demo/mapgen_demo_bg"; + +const CELL_SIZE = 12; +const GRID_COLOR = "#CCCCCC"; +const DEAD_COLOR = "#FFFFFF"; +const ALIVE_COLOR = "#000000"; + +var world = null; +const width = 80; +const height = 50; + +const infoDiv = document.getElementById('map-info'); +// Give the canvas room for all of our cells and a 1px border +// around each of them. +const canvas = document.getElementById("mapgen-canvas"); +canvas.height = (CELL_SIZE + 1) * height + 1; +canvas.width = (CELL_SIZE + 1) * width + 1; + +const ctx = canvas.getContext('2d'); + +// Map generators +function newCellularAutomata() { + var seed = Date.now(); + world = World.new_cellular_automata(width, height, seed); + drawGrid(); + drawCells(); + requestAnimationFrame(renderLoop); + infoDiv.textContent = "Cellular automata with the seed: " + seed; +} + +function newSimpleRooms() { + console.log("Simple rooms") +} + +const renderLoop = () => { + // universe.tick(); + + drawGrid(); + drawCells(); + + requestAnimationFrame(renderLoop); +}; + +const drawGrid = () => { + ctx.beginPath(); + ctx.strokeStyle = GRID_COLOR; + + // Vertical lines. + for (let i = 0; i <= width; i++) { + ctx.moveTo(i * (CELL_SIZE + 1) + 1, 0); + ctx.lineTo(i * (CELL_SIZE + 1) + 1, (CELL_SIZE + 1) * height + 1); + } + + // Horizontal lines. + for (let j = 0; j <= height; j++) { + ctx.moveTo(0, j * (CELL_SIZE + 1) + 1); + ctx.lineTo((CELL_SIZE + 1) * width + 1, j * (CELL_SIZE + 1) + 1); + } + + ctx.stroke(); +}; + +const getIndex = (row, column) => { + return row * width + column; +}; + +const drawCells = () => { + const tilesPtr = world.tiles(); + const tiles = new Uint8Array(memory.buffer, tilesPtr, width * height); + + ctx.beginPath(); + + for (let row = 0; row < height; row++) { + for (let col = 0; col < width; col++) { + const idx = getIndex(row, col); + + ctx.fillStyle = tiles[idx] == Cell.Floor + ? DEAD_COLOR + : ALIVE_COLOR; + + ctx.fillRect( + col * (CELL_SIZE + 1) + 1, + row * (CELL_SIZE + 1) + 1, + CELL_SIZE, + CELL_SIZE + ); + } + } + + ctx.stroke(); +}; + +newCellularAutomata(); + +// Connect UI element +document.getElementById('cellular-automata-option').addEventListener('click', newCellularAutomata); +document.getElementById('simple-rooms-option').addEventListener('click', newSimpleRooms); diff --git a/demo/www2/package.json b/demo/www2/package.json new file mode 100644 index 0000000..85b7c26 --- /dev/null +++ b/demo/www2/package.json @@ -0,0 +1,40 @@ +{ + "name": "create-wasm-app", + "version": "0.1.0", + "description": "create an app to consume rust-generated wasm packages", + "main": "index.js", + "bin": { + "create-wasm-app": ".bin/create-wasm-app.js" + }, + "scripts": { + "build": "webpack --config webpack.config.js", + "start": "webpack-dev-server", + "deploy": "gh-pages -d dist" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/rustwasm/create-wasm-app.git" + }, + "keywords": [ + "webassembly", + "wasm", + "rust", + "webpack" + ], + "author": "Ashley Williams ", + "license": "(MIT OR Apache-2.0)", + "bugs": { + "url": "https://github.com/rustwasm/create-wasm-app/issues" + }, + "homepage": "https://github.com/rustwasm/create-wasm-app#readme", + "dependencies": { + "mapgen-demo": "file:../pkg" + }, + "devDependencies": { + "hello-wasm-pack": "^0.1.0", + "webpack": "^4.29.3", + "webpack-cli": "^3.1.0", + "webpack-dev-server": "^3.1.5", + "copy-webpack-plugin": "^5.0.0" + } +} diff --git a/demo/www2/webpack.config.js b/demo/www2/webpack.config.js new file mode 100644 index 0000000..3f7c2f3 --- /dev/null +++ b/demo/www2/webpack.config.js @@ -0,0 +1,14 @@ +const CopyWebpackPlugin = require("copy-webpack-plugin"); +const path = require('path'); + +module.exports = { + entry: "./bootstrap.js", + output: { + path: path.resolve(__dirname, "../../docs"), + filename: "bootstrap.js", + }, + mode: "development", + plugins: [ + new CopyWebpackPlugin(['index.html', 'favicon.ico']) + ], +};