Play music-rival.mp3 during battle screen, resume map music on return

This commit is contained in:
justin committed 2026-05-20 23:48:11 -07:00
1 parent 05b125b6aa
commit 4f0ea6afe4
2 files changed
+25 -6

No files matched your search

+25 -6
View File
@@ -65,8 +65,9 @@ function clearSave() { try { localStorage.removeItem(SAVE_KEY); } catch(_){} }
/* ─── BACKGROUND MUSIC ───────────────────────────────────── */ /* ─── BACKGROUND MUSIC ───────────────────────────────────── */
const Music = (() => { const Music = (() => {
let audio = null; let audio = null;
let rival = null;
let muted = false; let muted = false;
let wantPlay = false; /* true if we tried to play but were blocked */ let wantPlay = false;
function getAudio() { function getAudio() {
if (!audio) { if (!audio) {
@@ -74,7 +75,6 @@ const Music = (() => {
audio.loop = true; audio.loop = true;
audio.volume = 0.5; audio.volume = 0.5;
audio.preload = 'auto'; audio.preload = 'auto';
/* If audio loads and we wanted to play, try again */
audio.addEventListener('canplaythrough', () => { audio.addEventListener('canplaythrough', () => {
if (wantPlay && !muted) audio.play().catch(() => {}); if (wantPlay && !muted) audio.play().catch(() => {});
}); });
@@ -82,6 +82,16 @@ const Music = (() => {
return audio; return audio;
} }
function getRival() {
if (!rival) {
rival = new Audio('./music-rival.mp3');
rival.loop = true;
rival.volume = 0.5;
rival.preload = 'auto';
}
return rival;
}
function updateBtn() { function updateBtn() {
const btn = document.getElementById('music-toggle'); const btn = document.getElementById('music-toggle');
if (!btn) return; if (!btn) return;
@@ -93,16 +103,25 @@ const Music = (() => {
play() { play() {
if (muted) return; if (muted) return;
wantPlay = true; wantPlay = true;
if (rival && !rival.paused) return;
const a = getAudio(); const a = getAudio();
if (a.readyState >= 3) { if (a.readyState >= 3) {
/* Enough data to play */
if (a.paused) a.play().catch((e) => { console.warn('Music play blocked:', e); }); if (a.paused) a.play().catch((e) => { console.warn('Music play blocked:', e); });
} }
/* else: canplaythrough listener will fire and retry */
}, },
pause() { pause() {
wantPlay = false; wantPlay = false;
if (audio && !audio.paused) audio.pause(); if (audio && !audio.paused) audio.pause();
if (rival && !rival.paused) rival.pause();
},
playBattle() {
if (muted) return;
if (audio && !audio.paused) audio.pause();
const r = getRival();
if (r.paused) { r.currentTime = 0; r.play().catch(() => {}); }
},
stopBattle() {
if (rival && !rival.paused) { rival.pause(); rival.currentTime = 0; }
}, },
toggle() { toggle() {
muted = !muted; muted = !muted;
@@ -110,7 +129,6 @@ const Music = (() => {
if (muted) Music.pause(); if (muted) Music.pause();
else Music.play(); else Music.play();
}, },
/* Call once on first user interaction to unblock autoplay */
unblock() { unblock() {
const a = getAudio(); const a = getAudio();
if (wantPlay && !muted && a.paused) { if (wantPlay && !muted && a.paused) {
@@ -1108,6 +1126,7 @@ class Game {
this._placeMapSprites(); this._placeMapSprites();
this._updateDirArrow(false); this._updateDirArrow(false);
Music.stopBattle();
Music.play(); Music.play();
saveGame(this.state); saveGame(this.state);
this.flashSaveDot(); this.flashSaveDot();
@@ -1146,7 +1165,7 @@ class Game {
this.show('battle'); this.show('battle');
this._showController(true); this._showController(true);
Music.pause(); Music.playBattle();
this.state.answering=false; this.state.answering=false;
this.state.cursor=0; this.state.cursor=0;
BIN
View File
Binary file not shown.