From 2f300a221b850ae66a785523436511c06b318a22 Mon Sep 17 00:00:00 2001 From: sanine Date: Sat, 6 Jul 2024 11:58:58 -0500 Subject: add level select screen --- level.js | 10 ++++++++++ levelSelect.js | 28 ++++++++++++++++++++++++++++ main.js | 6 ++++-- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 levelSelect.js diff --git a/level.js b/level.js index 17ccee7..27b7078 100644 --- a/level.js +++ b/level.js @@ -1,3 +1,4 @@ +import { setupLevelSelectUi } from './levelSelect.js'; import { render } from './render.js'; @@ -91,6 +92,15 @@ export function setupLevelUi(level, root, audio) { ui.canvas.height = 600; root.appendChild(ui.canvas); + ui.returnBtn = document.createElement('input'); + ui.returnBtn.type = 'button'; + ui.returnBtn.value = 'Return'; + ui.returnBtn.onclick = () => { + root.innerText = ''; + setupLevelSelectUi(root, audio); + } + root.appendChild(ui.returnBtn); + ui.ctx = ui.canvas.getContext('2d'); ui.ctx.translate(ui.canvas.width/2, ui.canvas.height/2) ui.ctx.scale(ui.canvas.width/20, -ui.canvas.height/20); diff --git a/levelSelect.js b/levelSelect.js new file mode 100644 index 0000000..6c0d995 --- /dev/null +++ b/levelSelect.js @@ -0,0 +1,28 @@ +import{ setupLevel, setupLevelUi } from './level.js'; + +const levels = { + 'Initial Jump': [ [], setupLevel([], [8, 0]) ], + 'Monopole Mining': [ ['Initial Jump'], setupLevel([[3, 3]], [8,8]) ], +}; + +export function setupLevelSelectUi(root, audio) { + const levelList = document.createElement('ol'); + const levelPicker = name => { + const [ dependencies, level ] = levels[name]; + const allDependenciesSatisfied = dependencies.map(x => levels[x][1].completed).reduce((acc, x) => acc && x, true); + if (allDependenciesSatisfied) { + const button = document.createElement('input'); + button.type = 'button'; + button.onclick = () => { + root.innerText = ''; + setupLevelUi(level, root, audio); + } + const li = document.createElement('li'); + li.appendChild(button); + levelList.appendChild(li); + } + } + + [...Object.keys(levels)].forEach(levelPicker); + root.appendChild(levelList); +} diff --git a/main.js b/main.js index 7ce6e6f..3ca2f27 100644 --- a/main.js +++ b/main.js @@ -1,3 +1,4 @@ +import { setupLevelSelectUi } from './levelSelect.js'; import { setupLevel, setupLevelUi } from './level.js'; window.onload = () => { @@ -34,8 +35,9 @@ window.onload = () => { ) musicList[0].element.play(); - const level = setupLevel([[2, 2], [3,3]], [7, 7]); - const ui = setupLevelUi(level, root, audio); + // const level = setupLevel([[2, 2], [3,3]], [7, 7]); + // const ui = setupLevelUi(level, root, audio); + setupLevelSelectUi(root, audio); }; root.appendChild(start); } -- cgit v1.2.1