using System.Collections.Generic;
using UnityEngine;
namespace GrassCutter
{
/// Pooled 2D/3D sound effects, music crossfading.
public class Sfx : MonoBehaviour
{
static Sfx _i;
public static Sfx I
{
get
{
if (_i == null)
{
var go = new GameObject("[Sfx]");
DontDestroyOnLoad(go);
_i = go.AddComponent();
}
return _i;
}
}
const int Voices = 40;
readonly List _pool = new List();
int _next;
AudioSource _musicA, _musicB;
bool _musicOnA = true;
string _currentMusic;
readonly Dictionary _lastPlayed = new Dictionary();
public static float MasterVolume = 1f, MusicVolume = 0.6f, SfxVolume = 1f;
/// The people on the calls: the Director and his assistant. Their own slider, apart from the effects.
public static float VoiceVolume = 1f;
/// Music sinks under a voice: 1 is full, 0 silent.
static float _duck = 1f;
void Awake()
{
for (int i = 0; i < Voices; i++)
{
var go = new GameObject("voice" + i);
go.transform.SetParent(transform);
var s = go.AddComponent();
s.playOnAwake = false;
s.rolloffMode = AudioRolloffMode.Linear;
s.minDistance = 2f;
s.maxDistance = 28f;
s.dopplerLevel = 0f;
_pool.Add(s);
}
_musicA = MakeMusic("musicA");
_musicB = MakeMusic("musicB");
ApplyVolumes();
}
AudioSource MakeMusic(string n)
{
var go = new GameObject(n);
go.transform.SetParent(transform);
var s = go.AddComponent();
s.loop = true;
s.spatialBlend = 0f;
s.playOnAwake = false;
s.volume = 0f;
return s;
}
public static void ApplyVolumes()
{
AudioListener.volume = MasterVolume;
}
AudioSource NextVoice()
{
// prefer a free voice, otherwise steal round-robin
for (int k = 0; k < _pool.Count; k++)
{
var s = _pool[(_next + k) % _pool.Count];
if (!s.isPlaying) { _next = (_next + k + 1) % _pool.Count; return s; }
}
var v = _pool[_next];
_next = (_next + 1) % _pool.Count;
return v;
}
/// Every sound asked for: name, volume, pitch, and where in the world (null: in the ears). The trailer logs them.
public static System.Action Heard;
public static AudioSource Play(string clip, float volume = 1f, float pitch = 1f, float pitchJitter = 0.05f, float minInterval = 0f)
{
Heard?.Invoke(clip, volume, pitch, null);
return I.PlayAt(AssetBank.I.Clip(clip), null, volume, pitch, pitchJitter, minInterval, clip);
}
public static AudioSource PlayAt(string clip, Vector3 pos, float volume = 1f, float pitch = 1f, float pitchJitter = 0.06f, float minInterval = 0f)
{
Heard?.Invoke(clip, volume, pitch, pos);
return I.PlayAt(AssetBank.I.Clip(clip), pos, volume, pitch, pitchJitter, minInterval, clip);
}
AudioSource PlayAt(AudioClip c, Vector3? pos, float volume, float pitch, float jitter, float minInterval, string key)
{
if (!c) return null;
if (minInterval > 0f)
{
if (_lastPlayed.TryGetValue(key, out float t) && Time.unscaledTime - t < minInterval) return null;
_lastPlayed[key] = Time.unscaledTime;
}
var s = NextVoice();
s.clip = c;
s.volume = volume * SfxVolume;
s.pitch = pitch * (1f + Random.Range(-jitter, jitter));
s.loop = false;
if (pos.HasValue) { s.spatialBlend = 0.85f; s.transform.position = pos.Value; }
else s.spatialBlend = 0f;
s.Play();
return s;
}
public static void Music(string name, float fade = 1.5f)
{
var i = I;
if (i._currentMusic == name) return;
i._currentMusic = name;
var clip = AssetBank.I.Clip(name);
var from = i._musicOnA ? i._musicA : i._musicB;
var to = i._musicOnA ? i._musicB : i._musicA;
i._musicOnA = !i._musicOnA;
Tween.Kill(i._musicA); Tween.Kill(i._musicB);
float fromStart = from.volume;
Tween.Run(from, fade, k => from.volume = Mathf.Lerp(fromStart, 0f, k), Ease.Linear, 0f, () => from.Stop());
if (!clip) return;
to.clip = clip;
to.volume = 0f;
to.Play();
Tween.Run(to, fade, k => to.volume = k * MusicVolume * 0.55f * _duck, Ease.Linear);
}
/// Lowers (or restores) the music under the director's voice.
public static void Duck(float k)
{
k = Mathf.Clamp01(k);
if (Mathf.Abs(k - _duck) < 0.001f) return;
_duck = k;
var i = I;
if (!i) return;
var active = i._musicOnA ? i._musicA : i._musicB;
if (active.isPlaying && !Tween.Running(active)) active.volume = MusicVolume * 0.55f * _duck;
}
public static void RefreshMusicVolume()
{
var i = I;
// Music() flips _musicOnA after picking the target, so the active source is the one it now names.
var active = i._musicOnA ? i._musicA : i._musicB;
if (active.isPlaying) active.volume = MusicVolume * 0.55f * _duck;
}
/// Starts a looping 3D sound and returns its source (caller stops it).
public static AudioSource Loop(string clip, Transform follow, float volume)
{
var c = AssetBank.I.Clip(clip);
if (!c || !follow) return null;
var go = new GameObject("loop_" + clip);
go.transform.SetParent(follow, false);
var s = go.AddComponent();
s.clip = c;
s.loop = true;
s.spatialBlend = 0.9f;
s.minDistance = 2f;
s.maxDistance = 25f;
s.rolloffMode = AudioRolloffMode.Linear;
s.volume = volume * SfxVolume;
s.dopplerLevel = 0f;
s.Play();
return s;
}
}
}