here_be_dragons/demo/www/index.js

144 lines
3.8 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-17 19:47:59 +00:00
const CELL_SIZE = 15;
2020-09-14 09:46:48 +00:00
const GRID_COLOR = "#CCCCCC";
const DEAD_COLOR = "#FFFFFF";
const ALIVE_COLOR = "#000000";
2020-10-18 11:07:36 +00:00
const TILE_SIZE = 39;
2020-09-14 09:46:48 +00:00
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");
2020-10-17 19:47:59 +00:00
canvas.height = CELL_SIZE * height;
canvas.width = CELL_SIZE * width;
// 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
const ctx = canvas.getContext('2d');
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
// Map generators
function newCellularAutomata() {
2020-10-15 20:24:14 +00:00
world = World.new_cellular_automata(width, height, get_seed());
2020-09-14 09:46:48 +00:00
requestAnimationFrame(renderLoop);
}
function newSimpleRooms() {
2020-09-14 18:29:36 +00:00
var seed = Date.now();
2020-10-15 20:24:14 +00:00
world = World.new_simple_rooms(width, height, get_seed());
2020-09-15 06:45:40 +00:00
requestAnimationFrame(renderLoop);
}
2020-09-15 20:33:18 +00:00
function newBspInterior() {
2020-09-15 14:23:47 +00:00
var seed = Date.now();
2020-10-15 20:24:14 +00:00
world = World.new_bsp_interior(width, height, get_seed());
2020-09-15 14:23:47 +00:00
requestAnimationFrame(renderLoop);
}
2020-09-16 09:42:59 +00:00
function newDrunkard() {
var seed = Date.now();
2020-10-15 20:24:14 +00:00
world = World.new_drunkard(width, height, get_seed());
2020-09-16 09:42:59 +00:00
requestAnimationFrame(renderLoop);
}
2020-09-15 06:45:40 +00:00
function newRandomGen() {
var seed = Date.now();
2020-10-15 20:24:14 +00:00
world = World.new_random(width, height, get_seed());
2020-09-14 18:29:36 +00:00
requestAnimationFrame(renderLoop);
2020-09-14 09:46:48 +00:00
}
const renderLoop = () => {
// 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) => {
return row * width + column;
};
2020-10-17 19:47:59 +00:00
const is_inner_wall = (tiles, col, row) => {
2020-10-17 20:11:21 +00:00
for (let c = Math.max(col-1, 0); c < Math.min(col+2, width); c++) {
for (let r = Math.max(row-1, 0); r < Math.min(row+2, height); r++) {
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 == "inner_wall") {
tile_x = 18;
tile_y = 0;
} else {
tile_x = 0;
tile_y = 3;
}
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();
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);
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
}
}
ctx.stroke();
};
2020-09-15 06:45:40 +00:00
newRandomGen();
2020-09-14 09:46:48 +00:00
// Connect UI element
document.getElementById('cellular-automata-option').addEventListener('click', newCellularAutomata);
document.getElementById('simple-rooms-option').addEventListener('click', newSimpleRooms);
2020-09-15 20:33:18 +00:00
document.getElementById('bsp-interior-option').addEventListener('click', newBspInterior);
2020-09-16 09:42:59 +00:00
document.getElementById('drunkard-option').addEventListener('click', newDrunkard);
2020-09-15 06:45:40 +00:00
document.getElementById('random-option').addEventListener('click', newRandomGen);