using System; using UnityEngine; namespace DiamondArrow { public enum SoundCue { Tap, Slide, Blocked, Hammer, Hint, Heart, Reward, Clear, Fail, Countdown, ChestOpen } // One persistent music source and a bounded pool of 2D voices across all game pages. [DisallowMultipleComponent] public sealed class GameAudio : MonoBehaviour { public AudioSource MusicSource { get; private set; } public SoundCue LastCue { get; private set; } public int PlayedCount { get; private set; } public const int VoiceCount = 8; readonly AudioSource[] voices = new AudioSource[VoiceCount]; readonly float[] lastPlayed = new float[Enum.GetValues(typeof(SoundCue)).Length]; NeonTheme theme; bool musicEnabled, effectsEnabled, suspended, gameplay; float duckUntil; int nextVoice; public void Initialize(NeonTheme assets, bool musicOn, bool effectsOn) { if (MusicSource) return; theme = assets; MusicSource = Source("Music"); MusicSource.loop = true; MusicSource.priority = 32; MusicSource.volume = 0; MusicSource.clip = theme.backgroundMusic; for (int i = 0; i < voices.Length; i++) voices[i] = Source("Effect " + i); for (int i = 0; i < lastPlayed.Length; i++) lastPlayed[i] = float.NegativeInfinity; if (!MusicSource.clip) Debug.LogError("Diamond Arrow: background music is unassigned. Run Diamond Arrow/Refresh audio assets.", this); ApplyPreferences(musicOn, effectsOn); } AudioSource Source(string label) { var child = new GameObject("DiamondArrow " + label); child.transform.SetParent(transform, false); var source = child.AddComponent(); source.playOnAwake = false; source.spatialBlend = 0; source.dopplerLevel = 0; source.reverbZoneMix = 0; return source; } public void ApplyPreferences(bool musicOn, bool effectsOn) { musicEnabled = musicOn; effectsEnabled = effectsOn; if (!effectsOn) foreach (var voice in voices) if (voice) voice.Stop(); EnsureMusic(); } public void SetGameplay(bool value) { gameplay = value; } // Stop immediately when another platform opens. An explicit preference change can resume playback. public void StopMusic() { musicEnabled = false; if (MusicSource) MusicSource.Stop(); } public void SetSuspended(bool value) { suspended = value; if (value) { if (MusicSource) MusicSource.Pause(); foreach (var voice in voices) if (voice) voice.Stop(); } else EnsureMusic(); } void EnsureMusic() { if (!MusicSource || !MusicSource.clip || !musicEnabled || suspended || !isActiveAndEnabled) return; if (!MusicSource.isPlaying) { MusicSource.UnPause(); if (!MusicSource.isPlaying) MusicSource.Play(); } } void Update() { if (!MusicSource) return; EnsureMusic(); float target = musicEnabled && !suspended ? theme.musicVolume * (gameplay ? .8f : 1f) : 0; if (Time.unscaledTime < duckUntil) target *= .45f; MusicSource.volume = Mathf.MoveTowards(MusicSource.volume, target, Time.unscaledDeltaTime * 1.2f); if (!musicEnabled && MusicSource.volume == 0 && MusicSource.isPlaying) MusicSource.Pause(); } public bool Play(SoundCue cue) { int index = (int)cue; if (!effectsEnabled || suspended || !isActiveAndEnabled || !theme || theme.soundEffects == null || index < 0 || index >= theme.soundEffects.Length) return false; var clip = theme.soundEffects[index]; if (!clip) return false; float cooldown = cue == SoundCue.Slide ? .055f : cue == SoundCue.Blocked ? .12f : .045f; if (Time.unscaledTime - lastPlayed[index] < cooldown) return false; lastPlayed[index] = Time.unscaledTime; // Reserve one voice for outcome/reward tails so rapid taps cannot cut off the fanfare. bool accent = cue == SoundCue.Clear || cue == SoundCue.Reward || cue == SoundCue.Fail; int slot = accent ? VoiceCount - 1 : nextVoice++ % (VoiceCount - 1); var source = voices[slot]; source.Stop(); source.clip = clip; source.volume = theme.effectsVolume; source.pitch = cue == SoundCue.Slide || cue == SoundCue.Tap ? UnityEngine.Random.Range(.97f, 1.035f) : 1; source.Play(); if (accent) duckUntil = Time.unscaledTime + clip.length; LastCue = cue; PlayedCount++; return true; } void OnDisable() { if (MusicSource) MusicSource.Pause(); foreach (var voice in voices) if (voice) voice.Stop(); } } }