From 7e2cc59135c1bc0036c4a3a9c91defef274f005e Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 5 Jul 2024 23:12:45 -0500 Subject: add level completion --- level.js | 37 +++++++++++++++++++++++++++++++++---- main.js | 2 +- render.js | 1 + 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/level.js b/level.js index 360b918..c10139c 100644 --- a/level.js +++ b/level.js @@ -41,6 +41,7 @@ export function setupLevelUi(level, root, audio) { ui.launchButton.type = 'button'; ui.launchButton.value = 'Launch'; ui.launchButton.onclick = () => { + ui.message.innerText = ''; level.running = false; ui.rocketThrustAudio.play(); ui.rocketGain.gain.setTargetAtTime(1, audio.currentTime, FAST_RAMP); @@ -54,7 +55,7 @@ export function setupLevelUi(level, root, audio) { setTimeout(() => ui.rocketThrustAudio.pause(), 50); level.equations.x = math.compile(e.target.value); level.running = false; - level.path = computePath(equations); + level.path = computePath(level.equations); render(ui.ctx, level, 0); }; ui.xeq.onkeydown = ui.xeq.onchange; @@ -67,15 +68,19 @@ export function setupLevelUi(level, root, audio) { setTimeout(() => ui.rocketThrustAudio.pause(), 50); level.equations.y = math.compile(e.target.value); level.running = false; - level.path = computePath(equations); + level.path = computePath(level.equations); render(ui.ctx, level, 0); }; ui.yeq.onkeydown = ui.yeq.onchange; ui.yeq.onkeyup = ui.yeq.onchange; + ui.message = document.createElement('h1'); + ui.id = "level-message"; + + root.appendChild(ui.launchButton); root.appendChild(ui.xeq); root.appendChild(ui.yeq); - root.appendChild(ui.launchButton); + root.appendChild(ui.message); ui.canvas = document.createElement('canvas'); ui.canvas.width = 600; @@ -93,7 +98,14 @@ export function setupLevelUi(level, root, audio) { function stepLevel(ui, level, index) { render(ui.ctx, level, index); - if (level.running && index < level.path.length-1) { + const pos = level.path[index]; + if (distance(pos, level.home) < 0.4) { + level.running = false; + ui.rocketGain.gain.setTargetAtTime(0, ui.audio.currentTime, FAST_RAMP); + setTimeout(() => ui.rocketThrustAudio.pause(), 50); + finishLevel(ui, level); + render(ui.ctx, level, 0); + } else if (level.running && index < level.path.length-1) { setTimeout(() => stepLevel(ui, level, index+1), 1); } else { level.running = false; @@ -110,3 +122,20 @@ function computePath(equations, start, end, step) { } +function distance(p1, p2) { + const [x1, y1] = p1; + const [x2, y2] = p2; + return Math.sqrt((x2-x1)**2 + (y2-y1)**2); +} + + +function finishLevel(ui, level) { + const uncollected = level.resources.reduce((acc, { collected }) => acc + (collected ? 0 : 1), 0); + if (uncollected > 0) { + ui.message.innerText = 'resources remaining!'; + } else { + ui.message.innerText = 'mission complete!'; + level.completed = true; + } + level.resources.forEach(x => x.collected = false); +} diff --git a/main.js b/main.js index 9a8e488..210822d 100644 --- a/main.js +++ b/main.js @@ -39,7 +39,7 @@ window.onload = () => { ) musicList[0].element.play(); - const level = setupLevel([], []); + const level = setupLevel([], [7, 7]); const ui = setupLevelUi(level, root, audio); }; root.appendChild(start); diff --git a/render.js b/render.js index 46cc37b..6adca2c 100644 --- a/render.js +++ b/render.js @@ -11,6 +11,7 @@ export function render(ctx, level, index) { level.resources .filter(({ collected }) => collected === false) .forEach(({ pos }) => drawMark(ctx, '+', pos[0], pos[1])); + drawMark(ctx, '※', level.home[0], level.home[1]); } -- cgit v1.2.1