summaryrefslogtreecommitdiff
path: root/level.js
diff options
context:
space:
mode:
Diffstat (limited to 'level.js')
-rw-r--r--level.js33
1 files changed, 17 insertions, 16 deletions
diff --git a/level.js b/level.js
index d511fa7..e2018e2 100644
--- a/level.js
+++ b/level.js
@@ -31,11 +31,17 @@ export function setupLevel(resources, home) {
export function setupLevelUi(level, root, audio) {
const ui = { audio };
- ui.rocketThrustAudio = document.getElementById('sfx-rocket-thrust');
- ui.rocketThrustSource = audio.createMediaElementSource(ui.rocketThrustAudio);
- ui.rocketGain = audio.createGain();
- ui.rocketGain.gain.value = 0;
- ui.rocketThrustSource.connect(ui.rocketGain).connect(audio.destination);
+ ui.resourceAudio = document.getElementById('sfx-resource');
+ ui.resourceSource = audio.createMediaElementSource(ui.resourceAudio);
+ ui.resourceSource.connect(audio.destination);
+
+ ui.doneAudio = document.getElementById('sfx-done');
+ ui.doneSource = audio.createMediaElementSource(ui.doneAudio);
+ ui.doneSource.connect(audio.destination);
+
+ ui.wrongAudio = document.getElementById('sfx-wrong');
+ ui.wrongSource = audio.createMediaElementSource(ui.wrongAudio);
+ ui.wrongSource.connect(audio.destination);
ui.launchButton = document.createElement('input');
ui.launchButton.type = 'button';
@@ -43,16 +49,12 @@ export function setupLevelUi(level, root, audio) {
ui.launchButton.onclick = () => {
ui.message.innerText = '';
level.running = false;
- ui.rocketThrustAudio.play();
- ui.rocketGain.gain.setTargetAtTime(1, audio.currentTime, FAST_RAMP);
setTimeout(() => { level.running=true; stepLevel(ui, level, 0); }, 20);
}
ui.xeq = document.createElement('input');
ui.xeq.value = X_START;
ui.xeq.onchange = (e) => {
- ui.rocketGain.gain.setTargetAtTime(0, audio.currentTime, FAST_RAMP);
- setTimeout(() => ui.rocketThrustAudio.pause(), 50);
level.equations.x = math.compile(e.target.value);
level.running = false;
level.path = computePath(level.equations);
@@ -64,8 +66,6 @@ export function setupLevelUi(level, root, audio) {
ui.yeq = document.createElement('input');
ui.yeq.value = Y_START;
ui.yeq.onchange = (e) => {
- ui.rocketGain.gain.setTargetAtTime(0, audio.currentTime, FAST_RAMP);
- setTimeout(() => ui.rocketThrustAudio.pause(), 50);
level.equations.y = math.compile(e.target.value);
level.running = false;
level.path = computePath(level.equations);
@@ -105,8 +105,9 @@ function stepLevel(ui, level, index) {
.filter(([x, _]) => x.collected === false)
.map(([x, i]) => [ distance(pos, x.position), i ])
.forEach(([d, i]) => {
- console.log(d);
if (d < 0.4) {
+ ui.resourceAudio.load();
+ ui.resourceAudio.play();
level.resources[i].collected = true;
}
});
@@ -115,16 +116,12 @@ function stepLevel(ui, level, 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;
- ui.rocketGain.gain.setTargetAtTime(0, ui.audio.currentTime, FAST_RAMP);
- setTimeout(() => ui.rocketThrustAudio.pause(), 50);
render(ui.ctx, level, 0);
}
}
@@ -146,8 +143,12 @@ function distance(p1, p2) {
function finishLevel(ui, level) {
const uncollected = level.resources.reduce((acc, { collected }) => acc + (collected ? 0 : 1), 0);
if (uncollected > 0) {
+ ui.wrongAudio.load();
+ ui.wrongAudio.play();
ui.message.innerText = 'resources remaining!';
} else {
+ ui.doneAudio.load();
+ ui.doneAudio.play();
ui.message.innerText = 'mission complete!';
level.completed = true;
}