Grass Cutters Together: the project as of moon-sweepers f3666f0
A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date. Images, audio, models and binaries go through Git LFS (.gitattributes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
180
Assets/Scripts/Core/Sfx.cs
Normal file
180
Assets/Scripts/Core/Sfx.cs
Normal file
@ -0,0 +1,180 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace GrassCutter
|
||||
{
|
||||
/// <summary>Pooled 2D/3D sound effects, music crossfading.</summary>
|
||||
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<Sfx>();
|
||||
}
|
||||
return _i;
|
||||
}
|
||||
}
|
||||
|
||||
const int Voices = 40;
|
||||
readonly List<AudioSource> _pool = new List<AudioSource>();
|
||||
int _next;
|
||||
AudioSource _musicA, _musicB;
|
||||
bool _musicOnA = true;
|
||||
string _currentMusic;
|
||||
readonly Dictionary<string, float> _lastPlayed = new Dictionary<string, float>();
|
||||
|
||||
public static float MasterVolume = 1f, MusicVolume = 0.6f, SfxVolume = 1f;
|
||||
/// <summary>The people on the calls: the Director and his assistant. Their own slider, apart from the effects.</summary>
|
||||
public static float VoiceVolume = 1f;
|
||||
/// <summary>Music sinks under a voice: 1 is full, 0 silent.</summary>
|
||||
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<AudioSource>();
|
||||
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<AudioSource>();
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>Every sound asked for: name, volume, pitch, and where in the world (null: in the ears). The trailer logs them.</summary>
|
||||
public static System.Action<string, float, float, Vector3?> 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);
|
||||
}
|
||||
|
||||
/// <summary>Lowers (or restores) the music under the director's voice.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>Starts a looping 3D sound and returns its source (caller stops it).</summary>
|
||||
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<AudioSource>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user