Add API to change the sound of AmbientSoundComponent (#18115)
This commit is contained in:
@@ -50,7 +50,7 @@ public sealed class AmbientSoundSystem : SharedAmbientSoundSystem
|
||||
/// </summary>
|
||||
private int MaxSingleSound => (int) (_maxAmbientCount / (16.0f / 6.0f));
|
||||
|
||||
private readonly Dictionary<AmbientSoundComponent, (IPlayingAudioStream? Stream, string Sound)> _playingSounds = new();
|
||||
private readonly Dictionary<AmbientSoundComponent, (IPlayingAudioStream? Stream, SoundSpecifier Sound, string Path)> _playingSounds = new();
|
||||
private readonly Dictionary<string, int> _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)
|
||||
|
||||
@@ -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!;
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user