# HG changeset patch # User ark # Date 1792514362 -7200 # Node ID 85d23328a215d79ad8c4f347d616e0fcb9729695 # Parent d1ae50439686d2bc1ed7bd5952348ff6ee19f0ae added intro music, logic for browser audio triggering on splash screen diff -r d1ae50439686 -r 85d23328a215 art/sound_sources/intro.m4a Binary file art/sound_sources/intro.m4a has changed diff -r d1ae50439686 -r 85d23328a215 art/sounds/intro.mp3 Binary file art/sounds/intro.mp3 has changed diff -r d1ae50439686 -r 85d23328a215 config/game.toml --- a/config/game.toml Tue Oct 20 17:58:10 2026 +0200 +++ b/config/game.toml Tue Oct 20 18:39:22 2026 +0200 @@ -190,6 +190,7 @@ enabled = true # until the player switches sound on or off in the game volume = 0.8 # base for all sounds beep_wave = "square" +music_fade = 0.4 # seconds the splash screen music takes to fade out. proto [sound.event] t0_attack = { volume = 0.6 } @@ -213,6 +214,7 @@ button_tap = { volume = 0.3, beep = [[1000, 0.02]] } map_won = { volume = 1.0 } map_lost = { volume = 1.0 } +intro = { volume = 1.0 } # splash screen music, looped. proto # sprite drawing direction (art.md): angle the drawing points to as drawn, diff -r d1ae50439686 -r 85d23328a215 doc/architecture.md --- a/doc/architecture.md Tue Oct 20 17:58:10 2026 +0200 +++ b/doc/architecture.md Tue Oct 20 18:39:22 2026 +0200 @@ -76,11 +76,14 @@ reachable from outside, so other screens are reached by tapping: -J runs javascript after the wait, and -S waits again before capturing. a pointer event sent to the canvas counts as a tap at that position in css pixels. -this taps the play button on the splash screen at 1024 x 768: +a fresh firefox profile has no stored settings, so the splash screen first +shows the sound button over the play button (sound.md). this taps it, and +the play button a second later, at 1024 x 768: - ff_screenshot -w 1024,768 -s 2 -S 3 -J 'document.getElementById("game") - .dispatchEvent(new PointerEvent("pointerdown", - {clientX: 497, clientY: 560, bubbles: true}))' \ + ff_screenshot -w 1024,768 -s 2 -S 3 -J 'function tap() { + document.getElementById("game").dispatchEvent(new PointerEvent( + "pointerdown", {clientX: 497, clientY: 560, bubbles: true})) } + tap(); setTimeout(tap, 1000)' \ file://$PWD/www/index.html temp/play.png `ff_screenshot -h` lists the other options. @@ -291,7 +294,8 @@ js/util.js vector maths, random numbers, easing js/storage.js settings kept in the browser js/assets.js image loading, sprite table - js/audio.js recorded sounds, placeholder beeps for the other events + js/audio.js recorded sounds, placeholder beeps for the other events, + splash screen music js/map.js placement lookup, snapping, route sampling js/monster.js movement along a route, hops, slow, hp js/tower.js targeting, attacks, upgrade, sell diff -r d1ae50439686 -r 85d23328a215 doc/art.md --- a/doc/art.md Tue Oct 20 17:58:10 2026 +0200 +++ b/doc/art.md Tue Oct 20 18:39:22 2026 +0200 @@ -78,6 +78,7 @@ boss_die: boss death map_won: level win map_lost: game lost + intro: splash screen music, starts with a burp that the script cuts off state of the files ------------------ diff -r d1ae50439686 -r 85d23328a215 doc/game_design.md --- a/doc/game_design.md Tue Oct 20 17:58:10 2026 +0200 +++ b/doc/game_design.md Tue Oct 20 18:39:22 2026 +0200 @@ -137,8 +137,10 @@ ------- - splash: title "minor monsters", byline "By M.A. and J.A. Juszczyk" and a - play button, drawn over the first map with the game paused. the play button - also starts the sound. left of the play button a logo opens a web page in a + play button, drawn over the first map with the game paused. music loops + while it shows. on the first splash screen a sound button covers the play + button, as browsers allow sound only after a tap; it starts the sound + (sound.md). left of the play button a logo opens a web page in a new tab, right of it a question mark opens the help popup - map won, on maps before the last: one button, next, which starts the next map diff -r d1ae50439686 -r 85d23328a215 doc/roadmap.md --- a/doc/roadmap.md Tue Oct 20 17:58:10 2026 +0200 +++ b/doc/roadmap.md Tue Oct 20 18:39:22 2026 +0200 @@ -157,7 +157,7 @@ - game flow: the first map is debug.start_map, and only the first map starts at debug.start_wave; the next maps start at wave 1. debug.start_coins applies to every map. the splash screen pauses the - first map, and only its play button responds. the next map starts with its break running, as + first map, and only its buttons respond. the next map starts with its break running, as does the first map after a lost map. the splash screen has a pale veil over the map, the other screens a dark one - screens: the scale is the same for all four drawings, set so the widest @@ -252,7 +252,8 @@ second half. they are dropped when the map ends - debug switches: all from architecture.md. start_coins -1 means the map's own value, seed 0 a new seed per run. the debug tower ranges are drawn under the towers -- sound: the play button on the splash screen starts the sound; taps +- sound: the sound button over the splash screen's play button starts the + sound, or the play button when sound is switched off (sound.md); taps before it leave sound off. later pointer and touch events resume audio if the browser has suspended it. switching sound off mutes the output at once and stops new sounds; the button tap is played after switching, so diff -r d1ae50439686 -r 85d23328a215 doc/sound.md --- a/doc/sound.md Tue Oct 20 17:58:10 2026 +0200 +++ b/doc/sound.md Tue Oct 20 18:39:22 2026 +0200 @@ -35,6 +35,7 @@ | button tap | button_tap | | | map won | map_won | recorded | | map lost | map_lost | recorded | +| splash screen music | intro | recorded, loops while the splash screen shows | several recordings per event are allowed; the game picks one at random. @@ -59,11 +60,22 @@ trims silence, filters low rumble, fades the ends and brings every sound to the same peak level. the output is mono mp3, which desktop browsers and tablets (including safari) play +- the script can drop the start of a recording up to a set time, and add a + pause of silence after a sound that loops. intro loses its first 3 s, + which hold a burp before the music, and gets 0.3 s of silence at the end - the build embeds art/sounds/ as base64 in www/data/sounds.js. see architecture.md for why -browsers only play sound after the first user interaction. the play button on -the splash screen starts the sound. +browsers only play sound after the first user interaction. on the first +splash screen after loading, a sound button covers the play button. it +pulses with a red glow, like an armed sell button. tapping it starts the +sound and the intro music, and uncovers the play button. when sound was +switched off in an earlier visit, there is no sound button: the play button +shows at once and starts the sound as before, and there is no music. + +the intro loops until the play button is tapped, then fades out over the +music_fade setting in [sound]. the splash screen that follows a won game +plays the intro at once, as sound is already running. the sound button in the top bar switches sound on and off. the choice is stored in the browser (architecture.md, persistence). until the player first diff -r d1ae50439686 -r 85d23328a215 game/js/audio.js --- a/game/js/audio.js Tue Oct 20 17:58:10 2026 +0200 +++ b/game/js/audio.js Tue Oct 20 18:39:22 2026 +0200 @@ -1,5 +1,5 @@ // sound: recorded sounds from data/sounds.js, placeholder beeps for events -// without a recording. the player's sound on or off choice is stored with +// without a recording, and looping music for the splash screen. the player's sound on or off choice is stored with // MM.storage; the config's enabled setting applies until the first choice. window.MM = window.MM || {}; @@ -13,6 +13,9 @@ var last = {}; // event -> audio clock time of its last play var ends = {}; // event -> audio clock time when its sounds have ended var started = false; + // the looping music: {name, src, gain}, src null while its recording is + // still being decoded + var music = null; var on = MM.storage.get("sound", cfg.enabled); // create the audio context and decode the recordings. the browser keeps @@ -36,7 +39,10 @@ for (var i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i); // the callback form, as older safari has no promise form ctx.decodeAudioData(bytes.buffer, - function (buf) { buffers[name].push(buf); }, + function (buf) { + buffers[name].push(buf); + if (music && music.name === name && !music.src) startMusic(); + }, function () { console.error("sound not decoded:", name); }); } @@ -110,6 +116,38 @@ ends[name] = Math.max(ends[name] || 0, when + length); } + // play the event's first recording in a loop until stopMusic(). does + // nothing while sound is off. on a context not yet unlocked, the music + // starts with the unlock; before its recording is decoded, once it is. + function playMusic(name) { + if (!on || !ctx || music || !cfg.event[name]) return; + music = { name: name, src: null, gain: null }; + if (buffers[name] && buffers[name].length) startMusic(); + } + + function startMusic() { + music.gain = ctx.createGain(); + music.gain.gain.value = cfg.event[music.name].volume; + music.gain.connect(master); + music.src = ctx.createBufferSource(); + music.src.buffer = buffers[music.name][0]; + music.src.loop = true; + music.src.connect(music.gain); + music.src.start(ctx.currentTime); + } + + // fade the music out over the music_fade setting and stop it + function stopMusic() { + if (!music) return; + if (music.src) { + var now = ctx.currentTime; + music.gain.gain.setValueAtTime(music.gain.gain.value, now); + music.gain.gain.linearRampToValueAtTime(0, now + cfg.music_fade); + music.src.stop(now + cfg.music_fade); + } + music = null; + } + function enabled() { return on; } @@ -122,5 +160,6 @@ MM.storage.set("sound", on); } - return { init: init, start: start, unlock: unlock, play: play, enabled: enabled, toggle: toggle }; + return { init: init, start: start, unlock: unlock, play: play, playMusic: playMusic, + stopMusic: stopMusic, enabled: enabled, toggle: toggle }; })(); diff -r d1ae50439686 -r 85d23328a215 game/js/game.js --- a/game/js/game.js Tue Oct 20 17:58:10 2026 +0200 +++ b/game/js/game.js Tue Oct 20 18:39:22 2026 +0200 @@ -30,6 +30,7 @@ this.speedIndex = 0; this.result = null; // "won" or "lost" this.splash = false; // splash screen shown over the paused map + this.askSound = false; // the splash screen's sound button covers play this.help = false; // help popup shown over everything this.helpResume = false; // the game ran when the help popup opened // player selection: shop tower type, its snapped preview, a built tower diff -r d1ae50439686 -r 85d23328a215 game/js/input.js --- a/game/js/input.js Tue Oct 20 17:58:10 2026 +0200 +++ b/game/js/input.js Tue Oct 20 18:39:22 2026 +0200 @@ -13,8 +13,8 @@ var DRAG_SLOP = 10; var REFUSED_TIME = 500; - // app: {canvas, view, game(), layout(), and the screen actions play(), - // next(), retry(), restart()} + // app: {canvas, view, game(), layout(), and the screen actions + // startSound(), play(), next(), retry(), restart()} function attach(app) { var c = app.canvas; // the press that moves the placement preview, followed until the @@ -107,7 +107,8 @@ if (b.id !== "sell") game.sellArmed = false; if (b.id !== "sell" && b.id !== "upgrade") game.selected = null; if (b.id === "logo" || b.id === "email") return { game: game, button: b.id }; - if (b.id === "play") app.play(); + if (b.id === "start_sound") app.startSound(); + else if (b.id === "play") app.play(); else if (b.id === "next") app.next(); else if (b.id === "retry") app.retry(); else if (b.id === "restart") app.restart(); diff -r d1ae50439686 -r 85d23328a215 game/js/main.js --- a/game/js/main.js Tue Oct 20 17:58:10 2026 +0200 +++ b/game/js/main.js Tue Oct 20 18:39:22 2026 +0200 @@ -28,14 +28,27 @@ resize(); } - // the splash screen over the paused first map - function splash() { + // the splash screen over the paused first map, with the intro music. + // browsers start sound only in a user interaction, so on the first + // splash screen, unless sound is switched off, a sound button covers + // the play button until it is tapped. sound is unlocked when the + // splash screen returns after a won game. + function splash(first) { startMap(FIRST); game.splash = true; game.paused = true; + if (first && MM.audio.enabled()) game.askSound = true; + else MM.audio.playMusic("intro"); + } + + function startSound() { + MM.audio.start(); + game.askSound = false; + MM.audio.playMusic("intro"); } function play() { + MM.audio.stopMusic(); game.splash = false; game.paused = false; } @@ -89,14 +102,15 @@ view: view, game: function () { return game; }, layout: function () { return L; }, + startSound: startSound, play: play, next: function () { startMap(game.map.index + 1); }, retry: function () { startMap(FIRST); }, - restart: splash + restart: function () { splash(false); } }); MM.assets.load(function (failed) { if (failed.length) console.error("images not loaded:", failed); - splash(); + splash(true); requestAnimationFrame(frame); }); })(); diff -r d1ae50439686 -r 85d23328a215 game/js/ui.js --- a/game/js/ui.js Tue Oct 20 17:58:10 2026 +0200 +++ b/game/js/ui.js Tue Oct 20 18:39:22 2026 +0200 @@ -169,7 +169,15 @@ function buttons(game, L) { var out = []; var name = screenName(game); - if (name) return screenRects(name, L).buttons; + if (name) { + out = screenRects(name, L).buttons; + // the sound button in place of the play button, until sound starts + if (name === "splash" && game.askSound) { + var p = out[0]; + out[0] = { id: "start_sound", x: p.x, y: p.y, w: p.w, h: p.h }; + } + return out; + } // top bar, right to left var y = L.buttonRow + (TOP - BUTTON) / 2, x = L.w - 8; ["help", "pause", "speed", "sound"].forEach(function (id) { @@ -272,6 +280,14 @@ ctx.restore(); } + // an armed button pulses fast with a red glow. sets the glow on ctx and + // returns the size factor of the pulse + function armed(ctx, view) { + ctx.shadowColor = "rgba(220, 30, 20, 0.9)"; + ctx.shadowBlur = 14; + return 1 + 0.15 * (0.5 - 0.5 * Math.cos(2 * Math.PI * view.now / 1000 / WARN_PULSE)); + } + function drawButton(ctx, game, b, view) { var cx = b.x + b.w / 2, cy = b.y + b.h / 2; if (b.screen) { @@ -283,6 +299,14 @@ fit(ctx, game.paused ? "play_button" : "pause_button", cx, cy, b.w, b.h); } else if (b.id === "speed") { fit(ctx, "speed_x" + game.speed(), cx, cy, b.w, b.h); + } else if (b.id === "start_sound") { + // covers the play button of the splash drawing with paper + ctx.fillStyle = "#fff"; + ctx.fillRect(b.x, b.y, b.w, b.h); + ctx.save(); + var a = armed(ctx, view); + fit(ctx, "sound_on", cx, cy, b.w * a, b.h * a); + ctx.restore(); } else if (b.id === "sound") { fit(ctx, MM.audio.enabled() ? "sound_on" : "sound_off", cx, cy, b.w, b.h); } else if (b.id === "early") { @@ -308,13 +332,8 @@ } else if (b.id === "sell") { // armed by a first tap: a fast pulse with a red glow until the // second tap sells - var k = 1; ctx.save(); - if (game.sellArmed) { - k = 1 + 0.15 * (0.5 - 0.5 * Math.cos(2 * Math.PI * view.now / 1000 / WARN_PULSE)); - ctx.shadowColor = "rgba(220, 30, 20, 0.9)"; - ctx.shadowBlur = 14; - } + var k = game.sellArmed ? armed(ctx, view) : 1; fit(ctx, "sell_button", b.cx, b.bubble.y + b.bubble.h * 0.4, 46 * k, 44 * k); ctx.restore(); price(ctx, MM.tower.refund(game.selected), b.cx, diff -r d1ae50439686 -r 85d23328a215 tools/build.py --- a/tools/build.py Tue Oct 20 17:58:10 2026 +0200 +++ b/tools/build.py Tue Oct 20 18:39:22 2026 +0200 @@ -346,6 +346,7 @@ need(snd, "enabled", bool, "sound") need(snd, "volume", float, "sound") need(snd, "beep_wave", str, "sound") + need(snd, "music_fade", float, "sound") for name, ev in need(snd, "event", dict, "sound").items(): where = f"sound.event.{name}" need(ev, "volume", float, where) diff -r d1ae50439686 -r 85d23328a215 tools/cut_sounds.py --- a/tools/cut_sounds.py Tue Oct 20 17:58:10 2026 +0200 +++ b/tools/cut_sounds.py Tue Oct 20 18:39:22 2026 +0200 @@ -13,6 +13,10 @@ mp3. a recording is split at its longest pauses: as many as it takes to give the listed number of variants. pauses inside one sound are left alone. +a recording listed in SKIP loses its start up to the given time before any of +this. a sound listed in LOOP_GAP is meant to play in a loop: that much silence +is added after its end, so the loop has a pause where it starts again. + requires ffmpeg with libmp3lame, which the build does not need: the build reads art/sounds/ only. run this script by hand after a recording changes. """ @@ -37,6 +41,16 @@ "monster_die": 4, } +# seconds cut off the start of a recording: intro has a burp before the music +SKIP = { + "intro": 3.0, +} + +# seconds of silence after a looping sound. to adjust by ear +LOOP_GAP = { + "intro": 0.3, +} + RATE = 44100 BITRATE = "64k" # remove rumble below this frequency, Hz @@ -148,7 +162,8 @@ def process(path): name = path.stem count = VARIANTS.get(name, 1) - samples = decode(path) + skip = int(SKIP.get(name, 0) * RATE) + samples = decode(path)[skip:] runs = voice(samples) if not runs: sys.exit(f"{name}: no voice found") @@ -157,9 +172,11 @@ old.unlink() for i, (start, end) in enumerate(sounds, start=1): dst = SOUNDS / (f"{name}_{i}.mp3" if count > 1 else f"{name}.mp3") - encode(shape(samples, start, end), dst) - print(f" {dst.name}: {start / RATE:.2f}-{end / RATE:.2f} s of the recording," - f" {(end - start) / RATE:.2f} s") + sound = shape(samples, start, end) + sound.extend([0] * int(LOOP_GAP.get(name, 0) * RATE)) + encode(sound, dst) + print(f" {dst.name}: {(skip + start) / RATE:.2f}-{(skip + end) / RATE:.2f} s of the" + f" recording, {(end - start) / RATE:.2f} s") def main(names):