pages235/src/sounds/musicSystem.ts
Vitaly df442338f8
feat(sounds): Add sound variants and resource pack support! (#258)
feat: add in-game music support! Enable it with `options.enableMusic = true`
2025-01-29 04:54:51 +03:00

33 lines
734 B
TypeScript

import { loadOrPlaySound } from '../basicSounds'
import { options } from '../optionsStorage'
class MusicSystem {
private currentMusic: string | null = null
async playMusic (url: string, musicVolume = 1) {
if (!options.enableMusic || this.currentMusic) return
try {
const { onEnded } = await loadOrPlaySound(url, 0.5 * musicVolume, 5000) ?? {}
if (!onEnded) return
this.currentMusic = url
onEnded(() => {
this.currentMusic = null
})
} catch (err) {
console.warn('Failed to play music:', err)
this.currentMusic = null
}
}
stopMusic () {
if (this.currentMusic) {
this.currentMusic = null
}
}
}
export const musicSystem = new MusicSystem()