最前面;遊戲用 SND.play(name)、SND.muted。 手機任何手勢解鎖、靜音鈕與記憶(localStorage)、背景暫停;遊戲完全沒聲音時補點擊/得分/失敗/結算四種保守音效。 */ const SND = (() => { const KEY = 'snd-muted:' + location.pathname.replace(/[^\w-]+/g, '_'), AC = window.AudioContext || window.webkitAudioContext, ctxs = []; let muted = false, own = null, ownMaster = null, gameSound = false; try { muted = localStorage.getItem(KEY) === '1'; } catch (e) {} if (AC) { const Wrapped = function (...a) { const c = new AC(...a), g = c.createGain(); g.gain.value = muted ? 0 : 1; g.connect(c.destination); try { Object.defineProperty(c, 'destination', { value: g }); } catch (e) {} ctxs.push({ c, g }); gameSound = true; return c; }; Wrapped.prototype = AC.prototype; window.AudioContext = Wrapped; window.webkitAudioContext = Wrapped; } const media = () => document.querySelectorAll('audio,video'); const applyMedia = () => media().forEach(m => { m.muted = muted; }); const MP = window.HTMLMediaElement && HTMLMediaElement.prototype, origPlay = MP && MP.play; if (origPlay) MP.play = function () { gameSound = true; this.muted = muted; return origPlay.apply(this, arguments); }; function ensureOwn() { if (own || !AC) return; try { own = new AC(); ownMaster = own.createGain(); ownMaster.gain.value = muted ? 0 : .5; ownMaster.connect(own.destination); } catch (e) { own = null; } } function unlock() { ctxs.forEach(({ c }) => { if (c.state === 'suspended') c.resume().catch(() => {}); }); if (own && own.state === 'suspended') own.resume().catch(() => {}); } function tone(f, d, type, v, at, f2) { if (!own) return; const t = own.currentTime + (at || 0), o = own.createOscillator(), g = own.createGain(); o.type = type || 'sine'; o.frequency.setValueAtTime(f, t); if (f2) o.frequency.exponentialRampToValueAtTime(f2, t + d); g.gain.setValueAtTime(v || .08, t); g.gain.exponentialRampToValueAtTime(.0001, t + d); o.connect(g); g.connect(ownMaster); o.start(t); o.stop(t + d + .02); } const E = { click: () => tone(660, .05, 'triangle', .04), ok: () => { tone(784, .09, 'triangle', .07); tone(1175, .14, 'triangle', .07, .07); }, bad: () => tone(220, .22, 'sawtooth', .05, 0, 110), end: () => { tone(523, .14, 'triangle', .07); tone(659, .14, 'triangle', .07, .14); tone(784, .3, 'triangle', .07, .28); }, }; let last = 0; function play(n) { if (muted || gameSound) return; const now = Date.now(); if (now - last < 60) return; last = now; ensureOwn(); unlock(); if (own && E[n]) E[n](); } function setMuted(v) { muted = v; try { localStorage.setItem(KEY, v ? '1' : '0'); } catch (e) {} ctxs.forEach(({ g }) => { g.gain.value = v ? 0 : 1; }); if (ownMaster) ownMaster.gain.value = v ? 0 : .5; applyMedia(); } const gesture = e => { unlock(); const t = e.target && e.target.closest && e.target.closest('button,[role=button],a,summary,label,.btn,.card'); if (e.type === 'pointerdown' && t && !t.disabled) play('click'); }; document.addEventListener('pointerdown', gesture, { passive: true, capture: true }); document.addEventListener('keydown', unlock, { passive: true, capture: true }); document.addEventListener('visibilitychange', () => { const all = ctxs.map(x => x.c).concat(own ? [own] : []); if (document.hidden) all.forEach(c => { if (c.state === 'running') c.suspend().catch(() => {}); }); else all.forEach(c => { if (c.state === 'suspended') c.resume().catch(() => {}); }); }); const GOOD = /(^|\s)(ok|right|good|correct|win)(\s|$)/, BAD = /(^|\s)(bad|wrong|no|err|lose)(\s|$)/, END = /(^|\s)(gameover|game-over|result|results)(\s|$)/; new MutationObserver(ms => { for (const m of ms) { const o = m.oldValue || '', n = m.target.className; if (typeof n !== 'string') continue; if (END.test(n) && !END.test(o)) play('end'); else if (GOOD.test(n) && !GOOD.test(o)) play('ok'); else if (BAD.test(n) && !BAD.test(o)) play('bad'); } }) .observe(document.documentElement, { subtree: true, attributes: true, attributeFilter: ['class'], attributeOldValue: true }); if (document.body) applyMedia(); else document.addEventListener('DOMContentLoaded', applyMedia); return { play, unlock, setMuted, get muted() { return muted; } }; })();