summaryrefslogtreecommitdiff
path: root/level.js
diff options
context:
space:
mode:
Diffstat (limited to 'level.js')
-rw-r--r--level.js37
1 files changed, 33 insertions, 4 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);
+}