diff options
author | sanine <sanine.not@pm.me> | 2024-07-05 23:12:45 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2024-07-05 23:12:45 -0500 |
commit | 7e2cc59135c1bc0036c4a3a9c91defef274f005e (patch) | |
tree | 620f9e6f7217c5ac262377900874ae57aa1e7530 /level.js | |
parent | ad5e699c810e448dc24d06dec201b7867b9f5e06 (diff) |
add level completion
Diffstat (limited to 'level.js')
-rw-r--r-- | level.js | 37 |
1 files changed, 33 insertions, 4 deletions
@@ -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); +} |