wobblyhead hg > minor_monsters
comparison game/js/audio.js @ 8:2c4fed8baed9
art update, persistent sound setting, tower selling as double action
| author | ark |
|---|---|
| date | 2026-10-19 13:37 +0200 |
| parents | 910b26feca76 |
| children |
comparison
equal
deleted
inserted
replaced
| 7:4fd2da999ba4 | 8:2c4fed8baed9 |
|---|---|
| 1 // sound: recorded sounds from data/sounds.js, placeholder beeps for events | 1 // sound: recorded sounds from data/sounds.js, placeholder beeps for events |
| 2 // without a recording | 2 // without a recording. the player's sound on or off choice is stored with |
| 3 // MM.storage; the config's enabled setting applies until the first choice. | |
| 3 | 4 |
| 4 window.MM = window.MM || {}; | 5 window.MM = window.MM || {}; |
| 5 | 6 |
| 6 MM.audio = (function () { | 7 MM.audio = (function () { |
| 7 "use strict"; | 8 "use strict"; |
| 10 var ctx = null, master = null; | 11 var ctx = null, master = null; |
| 11 var buffers = {}; // event -> decoded recordings | 12 var buffers = {}; // event -> decoded recordings |
| 12 var last = {}; // event -> audio clock time of its last play | 13 var last = {}; // event -> audio clock time of its last play |
| 13 var ends = {}; // event -> audio clock time when its sounds have ended | 14 var ends = {}; // event -> audio clock time when its sounds have ended |
| 14 var started = false; | 15 var started = false; |
| 16 var on = MM.storage.get("sound", cfg.enabled); | |
| 15 | 17 |
| 16 // create the audio context and decode the recordings. the browser keeps | 18 // create the audio context and decode the recordings. the browser keeps |
| 17 // the context suspended until unlock() runs in a user interaction. | 19 // the context suspended until unlock() runs in a user interaction. |
| 18 function init() { | 20 function init() { |
| 19 var AC = window.AudioContext || window.webkitAudioContext; | 21 var AC = window.AudioContext || window.webkitAudioContext; |
| 20 if (!AC || !cfg.enabled) return; | 22 if (!AC) return; |
| 21 ctx = new AC(); | 23 ctx = new AC(); |
| 22 master = ctx.createGain(); | 24 master = ctx.createGain(); |
| 23 master.gain.value = cfg.volume; | 25 master.gain.value = on ? cfg.volume : 0; |
| 24 master.connect(ctx.destination); | 26 master.connect(ctx.destination); |
| 25 var sounds = MM.sounds || {}; | 27 var sounds = MM.sounds || {}; |
| 26 Object.keys(sounds).forEach(function (name) { | 28 Object.keys(sounds).forEach(function (name) { |
| 27 buffers[name] = []; | 29 buffers[name] = []; |
| 28 sounds[name].forEach(function (b64) { decode(name, b64); }); | 30 sounds[name].forEach(function (b64) { decode(name, b64); }); |
| 78 // play a random recording of the event, or its beep if it has none. | 80 // play a random recording of the event, or its beep if it has none. |
| 79 // after lists events whose sounds still playing are let finish first. | 81 // after lists events whose sounds still playing are let finish first. |
| 80 // variants are picked with Math.random, so sound does not change the | 82 // variants are picked with Math.random, so sound does not change the |
| 81 // game's seeded random numbers. | 83 // game's seeded random numbers. |
| 82 function play(name, after) { | 84 function play(name, after) { |
| 83 if (!ctx || ctx.state !== "running") return; | 85 if (!on || !ctx || ctx.state !== "running") return; |
| 84 var ev = cfg.event[name]; | 86 var ev = cfg.event[name]; |
| 85 if (!ev) return; | 87 if (!ev) return; |
| 86 var now = ctx.currentTime; | 88 var now = ctx.currentTime; |
| 87 if (ev.min_interval && now - last[name] < ev.min_interval) return; | 89 if (ev.min_interval && now - last[name] < ev.min_interval) return; |
| 88 last[name] = now; | 90 last[name] = now; |
| 106 return; | 108 return; |
| 107 } | 109 } |
| 108 ends[name] = Math.max(ends[name] || 0, when + length); | 110 ends[name] = Math.max(ends[name] || 0, when + length); |
| 109 } | 111 } |
| 110 | 112 |
| 111 return { init: init, start: start, unlock: unlock, play: play }; | 113 function enabled() { |
| 114 return on; | |
| 115 } | |
| 116 | |
| 117 // switch sound on or off and store the choice. sounds already playing | |
| 118 // are muted at once. | |
| 119 function toggle() { | |
| 120 on = !on; | |
| 121 if (master) master.gain.value = on ? cfg.volume : 0; | |
| 122 MM.storage.set("sound", on); | |
| 123 } | |
| 124 | |
| 125 return { init: init, start: start, unlock: unlock, play: play, enabled: enabled, toggle: toggle }; | |
| 112 })(); | 126 })(); |
