using ProjectM.Simulation; using Unity.Entities; using Unity.NetCode; using UnityEngine; namespace ProjectM.Client { /// /// Client-only AMBIENT audio bed + run cues. A managed presentation /// (, main thread, no Burst) that plays a low, seamless-looping /// procedural drone (asset-free, AudioClip.Create like CombatFeedbackSystem.MakeClip) plus /// launch-countdown beeps and the boss-arrival roar — replicated-state observations only. Lives only in the /// client world, so the server never creates audio and nothing here affects determinism. Volumes are /// deliberately conservative. (The cycle-phase stingers + Core alarm retired with the siege loop — LANTERN purge.) /// [WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)] [UpdateInGroup(typeof(PresentationSystemGroup))] public partial class AmbientAudioSystem : SystemBase { AudioSource _ambient; AudioClip _ambientClip; GameObject _root; AudioClip _stingBeep, _stingRoar; int _lastCountdownSec = -1; bool _bossRoared; // 07-16 underwater re-voice: the bed volume + groan cadence live on FeelConfig (live-tunable). AudioSource _groanSrc; AudioClip _groanClip; float _nextGroanIn = 12f; // first distant groan ~12s into a session protected override void OnCreate() { _ambientClip = MakeDrone(); _groanClip = MakeGroan(); _stingBeep = MakeSting(880f, 880f, 0.09f, 0.30f); // countdown tick _stingRoar = MakeSting(90f, 38f, 0.90f, 0.60f); // boss-arrival roar (low falling growl) } protected override void OnStartRunning() { if (_root != null) return; _root = new GameObject("~AmbientAudio"); _ambient = _root.AddComponent(); _ambient.clip = _ambientClip; _ambient.loop = true; _ambient.playOnAwake = false; _ambient.spatialBlend = 0f; // 2D bed _ambient.volume = FeelConfig.AmbienceVolume * GameVolume.Music; _ambient.Play(); _groanSrc = _root.AddComponent(); _groanSrc.playOnAwake = false; _groanSrc.spatialBlend = 0f; // distant, directionless _groanSrc.loop = false; } protected override void OnDestroy() { if (_root != null) Object.Destroy(_root); } protected override void OnUpdate() { if (_ambient == null) return; float dt = SystemAPI.Time.DeltaTime; _ambient.volume = Mathf.MoveTowards(_ambient.volume, FeelConfig.AmbienceVolume * GameVolume.Music, dt * 0.25f); // Distant groans (07-16 gap-list): a slow low sweep with a soft attack, jittered interval + pitch — // the murk answering back. Presentation-only wall-clock randomness is fine here. if (_groanSrc != null && FeelConfig.GroanIntervalSec > 0f) { _nextGroanIn -= dt; if (_nextGroanIn <= 0f) { _nextGroanIn = FeelConfig.GroanIntervalSec * (0.6f + Random.value * 0.8f); _groanSrc.pitch = 0.85f + Random.value * 0.3f; _groanSrc.PlayOneShot(_groanClip, FeelConfig.GroanVolume * GameVolume.Music); } } // Launch countdown beeps (3-2-1) + the boss-arrival roar — replicated-state observations only. if (SystemAPI.TryGetSingleton(out var runAudio)) { int sec = -1; if (runAudio.Lifecycle == RunLifecycle.Launching && runAudio.LaunchTick != 0 && SystemAPI.TryGetSingleton(out var antime) && antime.ServerTick.IsValid) { int tl = new NetworkTick(runAudio.LaunchTick).TicksSince(antime.ServerTick); if (tl > 0) sec = tl / 60 + 1; } if (sec > 0 && sec != _lastCountdownSec && sec <= 3) _ambient.PlayOneShot(_stingBeep, 0.5f * GameVolume.Sfx); _lastCountdownSec = sec; bool inBossRoom = runAudio.Lifecycle == RunLifecycle.InRoom && runAudio.CurrentRoomType == RoomTypeId.Boss; if (!inBossRoom) { _bossRoared = false; // re-arm for the next boss room } else if (!_bossRoared) { foreach (var _ in SystemAPI.Query>().WithAll()) { _ambient.PlayOneShot(_stingRoar, 0.9f * GameVolume.Sfx); _bossRoared = true; break; } } } } // ---- Procedural audio (asset-free; mirrors CombatFeedbackSystem.MakeClip) ---- // A low, seamless-looping pad: each partial completes an integer number of cycles over the buffer // (freq snapped to k/duration) so the loop point has no click. A slow tremolo adds motion. // A low, seamless-looping UNDERWATER pressure bed (07-16 re-voice): sub-heavy partials + a pair of // detuned subs whose beat completes exactly once per loop (Snap keeps every partial click-free at the // loop point) — reads as slow water-column pressure swell rather than a musical pad. static AudioClip MakeDrone() { const int rate = 44100; const float dur = 8f; int len = (int)(dur * rate); var clip = AudioClip.Create("underwater_bed", len, 1, rate, false); var data = new float[len]; float f0 = Snap(38f, dur); // deep sub float f0b = f0 + 1f / dur; // detuned sub: beats exactly once per loop (seamless) float f1 = Snap(55f, dur); float f2 = Snap(82.4f, dur); // faint upper body float swell = Snap(0.25f, dur); // very slow amplitude swell for (int i = 0; i < len; i++) { float t = i / (float)rate; float s = 0.46f * Mathf.Sin(2f * Mathf.PI * f0 * t) + 0.34f * Mathf.Sin(2f * Mathf.PI * f0b * t) + 0.22f * Mathf.Sin(2f * Mathf.PI * f1 * t) + 0.07f * Mathf.Sin(2f * Mathf.PI * f2 * t); float amp = 0.7f + 0.3f * Mathf.Sin(2f * Mathf.PI * swell * t); data[i] = s * amp * 0.5f; } clip.SetData(data, 0); return clip; } // A distant whale-adjacent groan: slow 58->34Hz sweep with a soft sin^2 attack/decay window and a // subtle wobble — never a jump-scare, just the deep being large somewhere off-screen. static AudioClip MakeGroan() { const int rate = 44100; const float dur = 3.4f; int len = (int)(dur * rate); var clip = AudioClip.Create("distant_groan", len, 1, rate, false); var data = new float[len]; float phase = 0f; for (int i = 0; i < len; i++) { float t01 = i / (float)len; float t = i / (float)rate; float freq = Mathf.Lerp(58f, 34f, t01) * (1f + 0.03f * Mathf.Sin(2f * Mathf.PI * 1.7f * t)); phase += 2f * Mathf.PI * freq / rate; float window = Mathf.Sin(Mathf.PI * t01); float env = window * window; // soft attack AND release data[i] = (0.8f * Mathf.Sin(phase) + 0.2f * Mathf.Sin(2f * phase)) * env * 0.55f; } clip.SetData(data, 0); return clip; } // freq snapped so freq*dur is an integer -> the waveform closes seamlessly at the loop point. static float Snap(float freq, float dur) { float cycles = Mathf.Max(1f, Mathf.Round(freq * dur)); return cycles / dur; } // Short one-shot tone sweeping f0->f1 with an exponential decay envelope. static AudioClip MakeSting(float f0, float f1, float dur, float vol) => FeedbackFx.MakeClip("sting", f0, f1, dur, vol, decay: 3.5f); } }