using Cysharp.Threading.Tasks; using UnityEngine; namespace BrewMonster.Scripts { /// /// Lives on the persistent "Music" GameObject alongside AudioManager. /// Waits for the host player to be ready (mirroring TemporaryBackgroundMusic), /// then loads BGM + ambience clips via Addressables and plays them through AudioManager. /// public class WorldMusicController : MonoBehaviour { public static WorldMusicController Instance; [SerializeField] private WorldMusicDatabaseSO _worldMusicDB; void Awake() { if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(this); } } /// /// Called from LoginScreenUI when the player selects a character. /// Kicks off the async setup without blocking the caller. /// public void InitForWorld(int worldTag) { AudioManager.Instance.StopBGM(1f); SetupAudioSources(worldTag).Forget(); } private async UniTask SetupAudioSources(int worldTag) { var hostPlayer = CECGameRun.Instance?.GetHostPlayer(); while (hostPlayer == null) { await UniTask.DelayFrame(1); hostPlayer = CECGameRun.Instance?.GetHostPlayer(); } var entry = _worldMusicDB?.Lookup(worldTag); if (entry == null) { Debug.LogWarning($"[WorldMusicController] No music entry found for worldTag {worldTag}"); return; } var bgmClip = await AddressableManager.Instance.LoadAudioClipAsync(entry.bgmPath.ToLower()); var ambienceClip = await AddressableManager.Instance.LoadAudioClipAsync(entry.ambiencePath.ToLower()); AudioManager.Instance.PlayWorldMusic(bgmClip, ambienceClip, 2f); } } }