Compare commits

..
10 Commits
+15 -14
View File
@@ -189,24 +189,27 @@ const SFX = (() => {
function getCtx() {
if (!ctx) ctx = new (window.AudioContext || window.webkitAudioContext)();
if (ctx.state === 'suspended') ctx.resume();
if (ctx.state === 'suspended') ctx.resume().catch(() => {});
return ctx;
}
function unlock() {
if (!ctx) ctx = new (window.AudioContext || window.webkitAudioContext)();
if (ctx.state === 'suspended') ctx.resume().then(() => {});
document.addEventListener('touchstart', () => {
if (!ctx) {
ctx = new (window.AudioContext || window.webkitAudioContext)();
} else if (ctx.state === 'suspended') {
ctx.resume().catch(() => {});
}
document.addEventListener('touchstart', unlock, { once: true, passive: true });
document.addEventListener('pointerdown', unlock, { once: true, passive: true });
}, { passive: true });
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible' && ctx && ctx.state === 'suspended') {
ctx.resume();
ctx.resume().catch(() => {});
}
});
function tone(freq, type, vol, attack, sustain, release, when) {
const c = getCtx();
if (c.state !== 'running') return;
const t = when ?? c.currentTime;
const osc = c.createOscillator();
const gain = c.createGain();
@@ -224,11 +227,9 @@ const SFX = (() => {
function seq(notes, type='square', vol=0.18) {
const c = getCtx();
if (c.state !== 'running') return;
let t = c.currentTime + 0.02;
notes.forEach(n => {
if (n.f) tone(n.f, type, vol, 0.01, n.d * 0.6, n.d * 0.4, t);
t += n.d;
});
notes.forEach(n => { if (n.f) tone(n.f, type, vol, 0.01, n.d * 0.6, n.d * 0.4, t); t += n.d; });
}
return {
@@ -237,7 +238,7 @@ const SFX = (() => {
step() { tone(180, 'square', 0.04, 0.005, 0.02, 0.03); },
encounter() { seq([{f:523,d:.1},{f:659,d:.18}], 'square', 0.22); },
type() { tone(880 + Math.random()*200, 'square', 0.03, 0.005, 0.01, 0.02); },
select() { const c = getCtx(); tone(440, 'square', 0.12, 0.005, 0.03, 0.04, c.currentTime + 0.05); },
select() { tone(440, 'square', 0.12, 0.005, 0.03, 0.04); },
backspace() { seq([{f:350,d:.05},{f:250,d:.08}], 'triangle', 0.12); },
correct() { seq([{f:523,d:.08},{f:659,d:.08},{f:784,d:.08},{f:1047,d:.2}], 'square', 0.18); },
streak() { seq([{f:523,d:.06},{f:659,d:.06},{f:784,d:.06},{f:1047,d:.06},{f:1319,d:.2}], 'square', 0.18); },
@@ -1150,11 +1151,11 @@ class Game {
showTitle() {
this.show('title');
SFX.boot();
this._showController(true);
const go = () => {
document.getElementById('screen-title').removeEventListener('click', go);
Music.unblock();
Music.play();
SFX.boot();
this.showContinueOrName();
};
document.getElementById('screen-title').addEventListener('click', go);