const REGISTRY_KEY = 'bgMusic';

export function playMusic(scene, key) {
    const prev = scene.game.registry.get(REGISTRY_KEY);
    if (prev) {
        prev.stop();
        prev.destroy();
    }
    const music = scene.sound.add(key, { loop: true, volume: 0.7 });
    music.play();
    scene.game.registry.set(REGISTRY_KEY, music);
    return music;
}

export function stopMusic(scene) {
    const music = scene.game.registry.get(REGISTRY_KEY);
    if (music) {
        music.stop();
        music.destroy();
        scene.game.registry.set(REGISTRY_KEY, null);
    }
}
