here_be_dragons/demo/www/index.js

173 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-09-14 09:46:48 +00:00
import {Cell, World} from "mapgen-demo";
import { memory } from "mapgen-demo/mapgen_demo_bg";
2020-10-19 19:28:01 +00:00
const CANVAS_SIZE = 750;
2020-10-18 18:44:08 +00:00
const GRID_COLS = 80;
const GRID_ROWS = 50;
2020-10-19 19:28:01 +00:00
const CELL_SIZE = CANVAS_SIZE/GRID_ROWS;
const TILE_SIZE = 39;
2020-09-14 09:46:48 +00:00
2020-10-19 19:28:01 +00:00
// Init canvas
2020-09-14 09:46:48 +00:00
const canvas = document.getElementById("mapgen-canvas");
2020-10-18 18:44:08 +00:00
canvas.height = CELL_SIZE * GRID_ROWS;
canvas.width = CELL_SIZE * GRID_COLS;
const ctx = canvas.getContext('2d');
2020-10-19 19:28:01 +00:00
// Info box
const infoDiv = document.getElementById('map-info');
// API to the WASM
let world = null;
2020-10-18 18:44:08 +00:00
2020-10-17 19:47:59 +00:00
// Load tiles bitmap
let tiles_image = new Image();
2020-10-17 20:30:28 +00:00
tiles_image.src = 'assets/tiles.png';
2020-09-14 09:46:48 +00:00
2020-10-18 18:44:08 +00:00
// Take provided seed or generate new one
2020-10-15 20:24:14 +00:00
function get_seed() {
var seed_text = document.getElementById("seed").value;
if( seed_text.length > 0) {
return Number(seed_text);
}
return Date.now();
}
2020-09-14 09:46:48 +00:00
function setGenerator(e) {
document.getElementById("generatorDropdown").innerHTML = e.target.innerText;
2020-09-15 06:45:40 +00:00
}
// Map generators
function refreshMap() {
var generator_name = document.getElementById("generatorDropdown").innerHTML;
switch(generator_name){
case "Cellular Automata":
world = World.new_cellular_automata(GRID_COLS, GRID_ROWS, get_seed());
break;
case "Simple Rooms":
world = World.new_simple_rooms(GRID_COLS, GRID_ROWS, get_seed());
break;
case "BSP Rooms":
world = World.new_bsp_rooms(GRID_COLS, GRID_ROWS, get_seed());
break;
case "BSP Interior":
world = World.new_bsp_interior(GRID_COLS, GRID_ROWS, get_seed());
break;
case "Drunkard Walk":
world = World.new_drunkard(GRID_COLS, GRID_ROWS, get_seed());
break;
case "Maze":
world = World.new_maze(GRID_COLS, GRID_ROWS, get_seed());
break;
case "Voronoi Hive":
world = World.new_voronoi(GRID_COLS, GRID_ROWS, get_seed());
break;
default:
world = World.new_random(GRID_COLS, GRID_ROWS, get_seed());
}
2020-11-19 20:00:36 +00:00
2020-09-14 18:29:36 +00:00
requestAnimationFrame(renderLoop);
2020-09-14 09:46:48 +00:00
}
// Main loop
function renderLoop() {
2020-09-14 09:46:48 +00:00
// universe.tick();
drawCells();
2020-10-17 19:47:59 +00:00
requestAnimationFrame(renderLoop);
2020-09-14 09:46:48 +00:00
};
const getIndex = (row, column) => {
2020-10-18 18:44:08 +00:00
return row * GRID_COLS + column;
2020-09-14 09:46:48 +00:00
};
2020-10-17 19:47:59 +00:00
const is_inner_wall = (tiles, col, row) => {
2020-10-18 18:44:08 +00:00
for (let c = Math.max(col - 1, 0); c < Math.min(col + 2, GRID_COLS); c++) {
for (let r = Math.max(row - 1, 0); r < Math.min(row + 2, GRID_ROWS); r++) {
2020-10-17 20:11:21 +00:00
if ((c != col || r != row) && tiles[getIndex(r, c)] == Cell.Floor) {
return false;
}
}
}
2020-10-17 19:47:59 +00:00
return true;
}
2020-10-18 11:07:36 +00:00
const draw_tile = (ctx, row, col, tile_type) => {
var tile_x = 0;
var tile_y = 0;
if (tile_type == "floor") {
tile_x = 3;
tile_y = 2;
} else if (tile_type == "wall") {
2020-10-18 11:07:36 +00:00
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;
2020-10-18 11:07:36 +00:00
}
2020-10-18 11:07:36 +00:00
ctx.drawImage(
tiles_image,
tile_x * TILE_SIZE + 3,
tile_y * TILE_SIZE + 3,
TILE_SIZE - 3,
TILE_SIZE - 3,
col * CELL_SIZE,
row * CELL_SIZE,
CELL_SIZE,
CELL_SIZE);
}
2020-09-14 09:46:48 +00:00
const drawCells = () => {
const tilesPtr = world.tiles();
2020-10-18 18:44:08 +00:00
const tiles = new Uint8Array(memory.buffer, tilesPtr, GRID_COLS * GRID_ROWS);
2020-09-14 09:46:48 +00:00
// tiles
2020-10-18 18:44:08 +00:00
for (let row = 0; row < GRID_ROWS; row++) {
for (let col = 0; col < GRID_COLS; col++) {
2020-09-14 09:46:48 +00:00
const idx = getIndex(row, col);
2020-10-17 19:47:59 +00:00
if (tiles[idx] == Cell.Floor) {
2020-10-18 11:07:36 +00:00
draw_tile(ctx, row, col, "floor");
2020-10-17 19:47:59 +00:00
} else if (is_inner_wall(tiles, col, row)){
2020-10-18 11:07:36 +00:00
draw_tile(ctx, row, col, "inner-wall");
2020-10-17 19:47:59 +00:00
} else {
2020-10-18 11:07:36 +00:00
draw_tile(ctx, row, col, "wall");
2020-10-17 19:47:59 +00:00
}
2020-09-14 09:46:48 +00:00
}
}
// 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");
2020-09-14 09:46:48 +00:00
};
// Connect UI element
document.getElementById('cellular-automata-option').addEventListener('click', setGenerator);
document.getElementById('simple-rooms-option').addEventListener('click', setGenerator);
document.getElementById('bsp-rooms-option').addEventListener('click', setGenerator);
document.getElementById('drunkard-option').addEventListener('click', setGenerator);
document.getElementById('bsp-interior-option').addEventListener('click', setGenerator);
document.getElementById('maze-option').addEventListener('click', setGenerator);
document.getElementById('voronoi-option').addEventListener('click', setGenerator);
document.getElementById('random-option').addEventListener('click', setGenerator);
document.getElementById('refresh').addEventListener('click', refreshMap);
refreshMap();