Better map selection. Prepare for prefabs

This commit is contained in:
klangner 2021-01-15 14:24:52 +01:00
parent 56417cf9b4
commit 1474324a1e
2 changed files with 67 additions and 64 deletions

View File

@ -28,14 +28,15 @@
<span class="navbar-toggler-icon"></span> <span class="navbar-toggler-icon"></span>
</button> </button>
<div class="collapse navbar-collapse" id="navbarSupportedContent"> <div class="mx-auto" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto"> <ul class="navbar-nav">
<span class="navbar-text mr-2">Generator</span>
<li class="nav-item dropdown"> <li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" <button class="btn btn-secondary dropdown-toggle mr-2" type="button" id="generatorDropdown"
aria-haspopup="true" aria-expanded="false"> data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Select generator Random
</a> </button>
<div class="dropdown-menu" aria-labelledby="navbarDropdown"> <div class="dropdown-menu" aria-labelledby="generatorDropdown">
<a class="dropdown-item" id="cellular-automata-option">Cellular Automata</a> <a class="dropdown-item" id="cellular-automata-option">Cellular Automata</a>
<a class="dropdown-item" id="simple-rooms-option">Simple Rooms</a> <a class="dropdown-item" id="simple-rooms-option">Simple Rooms</a>
<a class="dropdown-item" id="bsp-rooms-option">BSP Rooms</a> <a class="dropdown-item" id="bsp-rooms-option">BSP Rooms</a>
@ -43,10 +44,12 @@
<a class="dropdown-item" id="drunkard-option">Drunkard Walk</a> <a class="dropdown-item" id="drunkard-option">Drunkard Walk</a>
<a class="dropdown-item" id="maze-option">Maze</a> <a class="dropdown-item" id="maze-option">Maze</a>
<a class="dropdown-item" id="voronoi-option">Voronoi Hive</a> <a class="dropdown-item" id="voronoi-option">Voronoi Hive</a>
<a class="dropdown-item" id="random-option">Random Generator</a> <a class="dropdown-item" id="random-option">Random</a>
</div> </div>
</li> </li>
<input class="form-control mr-sm-2" id="seed" type="search" placeholder="Seed" aria-label="Seed"> <span class="navbar-text mr-2">Seed</span>
<input class="form-control mr-sm-2 w-25" id="seed" type="search" placeholder="random" aria-label="Seed">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit" id="refresh">Refresh</button>
</ul> </ul>
</div> </div>
<a class="github-button" href="https://github.com/klangner/mapgen.rs" data-icon="octicon-star" <a class="github-button" href="https://github.com/klangner/mapgen.rs" data-icon="octicon-star"

View File

@ -30,55 +30,53 @@ function get_seed() {
return Date.now(); return Date.now();
} }
function setGenerator(e) {
document.getElementById("generatorDropdown").innerHTML = e.target.innerText;
}
// Map generators // Map generators
function newCellularAutomata() { 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()); world = World.new_cellular_automata(GRID_COLS, GRID_ROWS, get_seed());
requestAnimationFrame(renderLoop); break;
}
function newSimpleRooms() { case "Simple Rooms":
var seed = Date.now();
world = World.new_simple_rooms(GRID_COLS, GRID_ROWS, get_seed()); world = World.new_simple_rooms(GRID_COLS, GRID_ROWS, get_seed());
requestAnimationFrame(renderLoop); break;
}
function newBspInterior() { case "BSP Rooms":
var seed = Date.now();
world = World.new_bsp_interior(GRID_COLS, GRID_ROWS, get_seed());
requestAnimationFrame(renderLoop);
}
function newDrunkard() {
var seed = Date.now();
world = World.new_drunkard(GRID_COLS, GRID_ROWS, get_seed());
requestAnimationFrame(renderLoop);
}
function newBspRooms() {
var seed = Date.now();
world = World.new_bsp_rooms(GRID_COLS, GRID_ROWS, get_seed()); world = World.new_bsp_rooms(GRID_COLS, GRID_ROWS, get_seed());
requestAnimationFrame(renderLoop); break;
}
function newMaze() { case "BSP Interior":
var seed = Date.now(); 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()); world = World.new_maze(GRID_COLS, GRID_ROWS, get_seed());
requestAnimationFrame(renderLoop); break;
}
function newVoronoi() { case "Voronoi Hive":
var seed = Date.now();
world = World.new_voronoi(GRID_COLS, GRID_ROWS, get_seed()); world = World.new_voronoi(GRID_COLS, GRID_ROWS, get_seed());
requestAnimationFrame(renderLoop); break;
}
function newRandomGen() { default:
var seed = Date.now();
world = World.new_random(GRID_COLS, GRID_ROWS, get_seed()); world = World.new_random(GRID_COLS, GRID_ROWS, get_seed());
}
requestAnimationFrame(renderLoop); requestAnimationFrame(renderLoop);
} }
const renderLoop = () => { // Main loop
function renderLoop() {
// universe.tick(); // universe.tick();
drawCells(); drawCells();
requestAnimationFrame(renderLoop); requestAnimationFrame(renderLoop);
@ -160,14 +158,16 @@ const drawCells = () => {
draw_tile(ctx, exit.row(), exit.col(), "exit"); draw_tile(ctx, exit.row(), exit.col(), "exit");
}; };
newRandomGen();
// Connect UI element // Connect UI element
document.getElementById('cellular-automata-option').addEventListener('click', newCellularAutomata); document.getElementById('cellular-automata-option').addEventListener('click', setGenerator);
document.getElementById('simple-rooms-option').addEventListener('click', newSimpleRooms); document.getElementById('simple-rooms-option').addEventListener('click', setGenerator);
document.getElementById('bsp-rooms-option').addEventListener('click', newBspRooms); document.getElementById('bsp-rooms-option').addEventListener('click', setGenerator);
document.getElementById('drunkard-option').addEventListener('click', newDrunkard); document.getElementById('drunkard-option').addEventListener('click', setGenerator);
document.getElementById('bsp-interior-option').addEventListener('click', newBspInterior); document.getElementById('bsp-interior-option').addEventListener('click', setGenerator);
document.getElementById('maze-option').addEventListener('click', newMaze); document.getElementById('maze-option').addEventListener('click', setGenerator);
document.getElementById('voronoi-option').addEventListener('click', newVoronoi); document.getElementById('voronoi-option').addEventListener('click', setGenerator);
document.getElementById('random-option').addEventListener('click', newRandomGen); document.getElementById('random-option').addEventListener('click', setGenerator);
document.getElementById('refresh').addEventListener('click', refreshMap);
refreshMap();