From f708a8dbf59c172d10d74515031e22b08b3cbf6a Mon Sep 17 00:00:00 2001 From: Abbey Armbruster Date: Thu, 3 Aug 2023 06:36:10 -0400 Subject: [PATCH] Add API to change the sound of AmbientSoundComponent (#18115) --- Content.Client/Audio/AmbientSoundSystem.cs | 24 ++++++++++--------- Content.Shared/Audio/AmbientSoundComponent.cs | 1 + .../Audio/SharedAmbientSoundSystem.cs | 13 ++++++++++ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/Content.Client/Audio/AmbientSoundSystem.cs b/Content.Client/Audio/AmbientSoundSystem.cs index 210b92d815..8fe331119a 100644 --- a/Content.Client/Audio/AmbientSoundSystem.cs +++ b/Content.Client/Audio/AmbientSoundSystem.cs @@ -50,7 +50,7 @@ public sealed class AmbientSoundSystem : SharedAmbientSoundSystem /// private int MaxSingleSound => (int) (_maxAmbientCount / (16.0f / 6.0f)); - private readonly Dictionary _playingSounds = new(); + private readonly Dictionary _playingSounds = new(); private readonly Dictionary _playingCount = new(); public bool OverlayEnabled @@ -104,9 +104,9 @@ public sealed class AmbientSoundSystem : SharedAmbientSoundSystem return; sound.Stream?.Stop(); - _playingCount[sound.Sound] -= 1; - if (_playingCount[sound.Sound] == 0) - _playingCount.Remove(sound.Sound); + _playingCount[sound.Path] -= 1; + if (_playingCount[sound.Path] == 0) + _playingCount.Remove(sound.Path); } private void SetAmbienceVolume(float value) @@ -141,9 +141,9 @@ public sealed class AmbientSoundSystem : SharedAmbientSoundSystem { var count = 0; - foreach (var (_, (_, sound)) in _playingSounds) + foreach (var (_, (_, sound, path)) in _playingSounds) { - if (sound.Equals(countSound)) + if (path.Equals(countSound)) count++; } @@ -177,7 +177,7 @@ public sealed class AmbientSoundSystem : SharedAmbientSoundSystem private void ClearSounds() { - foreach (var (stream, _) in _playingSounds.Values) + foreach (var (stream, _, _) in _playingSounds.Values) { stream?.Stop(); } @@ -245,6 +245,8 @@ public sealed class AmbientSoundSystem : SharedAmbientSoundSystem var entity = comp.Owner; if (comp.Enabled && + // Don't keep playing sounds that have changed since. + sound.Sound == comp.Sound && query.TryGetComponent(entity, out var xform) && xform.MapID == playerXform.MapID && !metaQuery.GetComponent(entity).EntityPaused) @@ -259,9 +261,9 @@ public sealed class AmbientSoundSystem : SharedAmbientSoundSystem sound.Stream?.Stop(); _playingSounds.Remove(comp); - _playingCount[sound.Sound] -= 1; - if (_playingCount[sound.Sound] == 0) - _playingCount.Remove(sound.Sound); + _playingCount[sound.Path] -= 1; + if (_playingCount[sound.Path] == 0) + _playingCount.Remove(sound.Path); } if (_playingSounds.Count >= _maxAmbientCount) @@ -301,7 +303,7 @@ public sealed class AmbientSoundSystem : SharedAmbientSoundSystem if (stream == null) continue; - _playingSounds[comp] = (stream, key); + _playingSounds[comp] = (stream, comp.Sound, key); playingCount++; if (_playingSounds.Count >= _maxAmbientCount) diff --git a/Content.Shared/Audio/AmbientSoundComponent.cs b/Content.Shared/Audio/AmbientSoundComponent.cs index b120510f4a..db13db2e6f 100644 --- a/Content.Shared/Audio/AmbientSoundComponent.cs +++ b/Content.Shared/Audio/AmbientSoundComponent.cs @@ -50,4 +50,5 @@ public sealed class AmbientSoundComponentState : ComponentState public bool Enabled { get; init; } public float Range { get; init; } public float Volume { get; init; } + public SoundSpecifier Sound { get; init; } = default!; } diff --git a/Content.Shared/Audio/SharedAmbientSoundSystem.cs b/Content.Shared/Audio/SharedAmbientSoundSystem.cs index 3bda37c634..30fdc946ed 100644 --- a/Content.Shared/Audio/SharedAmbientSoundSystem.cs +++ b/Content.Shared/Audio/SharedAmbientSoundSystem.cs @@ -1,3 +1,4 @@ +using Robust.Shared.Audio; using Robust.Shared.GameStates; namespace Content.Shared.Audio; @@ -45,12 +46,23 @@ public abstract class SharedAmbientSoundSystem : EntitySystem Dirty(ambience); } + public virtual void SetSound(EntityUid uid, SoundSpecifier sound, AmbientSoundComponent? ambience = null) + { + if (!Resolve(uid, ref ambience, false) || ambience.Sound == sound) + return; + + ambience.Sound = sound; + QueueUpdate(uid, ambience); + Dirty(ambience); + } + private void HandleCompState(EntityUid uid, AmbientSoundComponent component, ref ComponentHandleState args) { if (args.Current is not AmbientSoundComponentState state) return; SetAmbience(uid, state.Enabled, component); SetRange(uid, state.Range, component); SetVolume(uid, state.Volume, component); + SetSound(uid, state.Sound, component); } private void GetCompState(EntityUid uid, AmbientSoundComponent component, ref ComponentGetState args) @@ -60,6 +72,7 @@ public abstract class SharedAmbientSoundSystem : EntitySystem Enabled = component.Enabled, Range = component.Range, Volume = component.Volume, + Sound = component.Sound, }; } }