GameAudio.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using UnityEngine;
  3. namespace DiamondArrow
  4. {
  5. public enum SoundCue { Tap, Slide, Blocked, Hammer, Hint, Heart, Reward, Clear, Fail, Countdown, ChestOpen }
  6. // One persistent music source and a bounded pool of 2D voices across all game pages.
  7. [DisallowMultipleComponent]
  8. public sealed class GameAudio : MonoBehaviour
  9. {
  10. public AudioSource MusicSource { get; private set; }
  11. public SoundCue LastCue { get; private set; }
  12. public int PlayedCount { get; private set; }
  13. public const int VoiceCount = 8;
  14. readonly AudioSource[] voices = new AudioSource[VoiceCount];
  15. readonly float[] lastPlayed = new float[Enum.GetValues(typeof(SoundCue)).Length];
  16. NeonTheme theme;
  17. bool musicEnabled, effectsEnabled, suspended, gameplay;
  18. float duckUntil;
  19. int nextVoice;
  20. public void Initialize(NeonTheme assets, bool musicOn, bool effectsOn)
  21. {
  22. if (MusicSource) return;
  23. theme = assets;
  24. MusicSource = Source("Music");
  25. MusicSource.loop = true;
  26. MusicSource.priority = 32;
  27. MusicSource.volume = 0;
  28. MusicSource.clip = theme.backgroundMusic;
  29. for (int i = 0; i < voices.Length; i++) voices[i] = Source("Effect " + i);
  30. for (int i = 0; i < lastPlayed.Length; i++) lastPlayed[i] = float.NegativeInfinity;
  31. if (!MusicSource.clip) Debug.LogError("Diamond Arrow: background music is unassigned. Run Diamond Arrow/Refresh audio assets.", this);
  32. ApplyPreferences(musicOn, effectsOn);
  33. }
  34. AudioSource Source(string label)
  35. {
  36. var child = new GameObject("DiamondArrow " + label);
  37. child.transform.SetParent(transform, false);
  38. var source = child.AddComponent<AudioSource>();
  39. source.playOnAwake = false;
  40. source.spatialBlend = 0;
  41. source.dopplerLevel = 0;
  42. source.reverbZoneMix = 0;
  43. return source;
  44. }
  45. public void ApplyPreferences(bool musicOn, bool effectsOn)
  46. {
  47. musicEnabled = musicOn;
  48. effectsEnabled = effectsOn;
  49. if (!effectsOn) foreach (var voice in voices) if (voice) voice.Stop();
  50. EnsureMusic();
  51. }
  52. public void SetGameplay(bool value) { gameplay = value; }
  53. // Stop immediately when another platform opens. An explicit preference change can resume playback.
  54. public void StopMusic()
  55. {
  56. musicEnabled = false;
  57. if (MusicSource) MusicSource.Stop();
  58. }
  59. public void SetSuspended(bool value)
  60. {
  61. suspended = value;
  62. if (value)
  63. {
  64. if (MusicSource) MusicSource.Pause();
  65. foreach (var voice in voices) if (voice) voice.Stop();
  66. }
  67. else EnsureMusic();
  68. }
  69. void EnsureMusic()
  70. {
  71. if (!MusicSource || !MusicSource.clip || !musicEnabled || suspended || !isActiveAndEnabled) return;
  72. if (!MusicSource.isPlaying)
  73. {
  74. MusicSource.UnPause();
  75. if (!MusicSource.isPlaying) MusicSource.Play();
  76. }
  77. }
  78. void Update()
  79. {
  80. if (!MusicSource) return;
  81. EnsureMusic();
  82. float target = musicEnabled && !suspended ? theme.musicVolume * (gameplay ? .8f : 1f) : 0;
  83. if (Time.unscaledTime < duckUntil) target *= .45f;
  84. MusicSource.volume = Mathf.MoveTowards(MusicSource.volume, target, Time.unscaledDeltaTime * 1.2f);
  85. if (!musicEnabled && MusicSource.volume == 0 && MusicSource.isPlaying) MusicSource.Pause();
  86. }
  87. public bool Play(SoundCue cue)
  88. {
  89. int index = (int)cue;
  90. if (!effectsEnabled || suspended || !isActiveAndEnabled || !theme || theme.soundEffects == null || index < 0 || index >= theme.soundEffects.Length) return false;
  91. var clip = theme.soundEffects[index];
  92. if (!clip) return false;
  93. float cooldown = cue == SoundCue.Slide ? .055f : cue == SoundCue.Blocked ? .12f : .045f;
  94. if (Time.unscaledTime - lastPlayed[index] < cooldown) return false;
  95. lastPlayed[index] = Time.unscaledTime;
  96. // Reserve one voice for outcome/reward tails so rapid taps cannot cut off the fanfare.
  97. bool accent = cue == SoundCue.Clear || cue == SoundCue.Reward || cue == SoundCue.Fail;
  98. int slot = accent ? VoiceCount - 1 : nextVoice++ % (VoiceCount - 1);
  99. var source = voices[slot];
  100. source.Stop();
  101. source.clip = clip;
  102. source.volume = theme.effectsVolume;
  103. source.pitch = cue == SoundCue.Slide || cue == SoundCue.Tap ? UnityEngine.Random.Range(.97f, 1.035f) : 1;
  104. source.Play();
  105. if (accent) duckUntil = Time.unscaledTime + clip.length;
  106. LastCue = cue;
  107. PlayedCount++;
  108. return true;
  109. }
  110. void OnDisable()
  111. {
  112. if (MusicSource) MusicSource.Pause();
  113. foreach (var voice in voices) if (voice) voice.Stop();
  114. }
  115. }
  116. }