Ambient music fixes (#17685)
This commit is contained in:
@@ -13,304 +13,302 @@ using Robust.Shared.Utility;
|
||||
using System.Linq;
|
||||
using Robust.Client.GameObjects;
|
||||
|
||||
namespace Content.Client.Audio
|
||||
namespace Content.Client.Audio;
|
||||
//TODO: This is using a incomplete version of the whole "only play nearest sounds" algo, that breaks down a bit should the ambient sound cap get hit.
|
||||
//TODO: This'll be fixed when GetEntitiesInRange produces consistent outputs.
|
||||
|
||||
/// <summary>
|
||||
/// Samples nearby <see cref="AmbientSoundComponent"/> and plays audio.
|
||||
/// </summary>
|
||||
public sealed class AmbientSoundSystem : SharedAmbientSoundSystem
|
||||
{
|
||||
//TODO: This is using a incomplete version of the whole "only play nearest sounds" algo, that breaks down a bit should the ambient sound cap get hit.
|
||||
//TODO: This'll be fixed when GetEntitiesInRange produces consistent outputs.
|
||||
[Dependency] private readonly AmbientSoundTreeSystem _treeSys = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
protected override void QueueUpdate(EntityUid uid, AmbientSoundComponent ambience)
|
||||
=> _treeSys.QueueTreeUpdate(uid, ambience);
|
||||
|
||||
private AmbientSoundOverlay? _overlay;
|
||||
private int _maxAmbientCount;
|
||||
private bool _overlayEnabled;
|
||||
private float _maxAmbientRange;
|
||||
private float _cooldown;
|
||||
private TimeSpan _targetTime = TimeSpan.Zero;
|
||||
private float _ambienceVolume = 0.0f;
|
||||
|
||||
private static AudioParams _params = AudioParams.Default.WithVariation(0.01f).WithLoop(true).WithAttenuation(Attenuation.LinearDistance);
|
||||
|
||||
/// <summary>
|
||||
/// Samples nearby <see cref="AmbientSoundComponent"/> and plays audio.
|
||||
/// How many times we can be playing 1 particular sound at once.
|
||||
/// </summary>
|
||||
public sealed class AmbientSoundSystem : SharedAmbientSoundSystem
|
||||
private int MaxSingleSound => (int) (_maxAmbientCount / (16.0f / 6.0f));
|
||||
|
||||
private readonly Dictionary<AmbientSoundComponent, (IPlayingAudioStream? Stream, string Sound)> _playingSounds = new();
|
||||
private readonly Dictionary<string, int> _playingCount = new();
|
||||
|
||||
public bool OverlayEnabled
|
||||
{
|
||||
[Dependency] private readonly AmbientSoundTreeSystem _treeSys = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
protected override void QueueUpdate(EntityUid uid, AmbientSoundComponent ambience)
|
||||
=> _treeSys.QueueTreeUpdate(uid, ambience);
|
||||
|
||||
private AmbientSoundOverlay? _overlay;
|
||||
private int _maxAmbientCount;
|
||||
private bool _overlayEnabled;
|
||||
private float _maxAmbientRange;
|
||||
private float _cooldown;
|
||||
private TimeSpan _targetTime = TimeSpan.Zero;
|
||||
private float _ambienceVolume = 0.0f;
|
||||
|
||||
private static AudioParams _params = AudioParams.Default.WithVariation(0.01f).WithLoop(true).WithAttenuation(Attenuation.LinearDistance);
|
||||
|
||||
/// <summary>
|
||||
/// How many times we can be playing 1 particular sound at once.
|
||||
/// </summary>
|
||||
private int MaxSingleSound => (int) (_maxAmbientCount / (16.0f / 6.0f));
|
||||
|
||||
private readonly Dictionary<AmbientSoundComponent, (IPlayingAudioStream? Stream, string Sound)> _playingSounds = new();
|
||||
private readonly Dictionary<string, int> _playingCount = new();
|
||||
|
||||
public bool OverlayEnabled
|
||||
get => _overlayEnabled;
|
||||
set
|
||||
{
|
||||
get => _overlayEnabled;
|
||||
set
|
||||
{
|
||||
if (_overlayEnabled == value) return;
|
||||
_overlayEnabled = value;
|
||||
var overlayManager = IoCManager.Resolve<IOverlayManager>();
|
||||
if (_overlayEnabled == value) return;
|
||||
_overlayEnabled = value;
|
||||
var overlayManager = IoCManager.Resolve<IOverlayManager>();
|
||||
|
||||
if (_overlayEnabled)
|
||||
{
|
||||
_overlay = new AmbientSoundOverlay(EntityManager, this, EntityManager.System<EntityLookupSystem>());
|
||||
overlayManager.AddOverlay(_overlay);
|
||||
}
|
||||
else
|
||||
{
|
||||
overlayManager.RemoveOverlay(_overlay!);
|
||||
_overlay = null;
|
||||
}
|
||||
if (_overlayEnabled)
|
||||
{
|
||||
_overlay = new AmbientSoundOverlay(EntityManager, this, EntityManager.System<EntityLookupSystem>());
|
||||
overlayManager.AddOverlay(_overlay);
|
||||
}
|
||||
else
|
||||
{
|
||||
overlayManager.RemoveOverlay(_overlay!);
|
||||
_overlay = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Is this AmbientSound actively playing right now?
|
||||
/// </summary>
|
||||
/// <param name="component"></param>
|
||||
/// <returns></returns>
|
||||
public bool IsActive(AmbientSoundComponent component)
|
||||
/// <summary>
|
||||
/// Is this AmbientSound actively playing right now?
|
||||
/// </summary>
|
||||
/// <param name="component"></param>
|
||||
/// <returns></returns>
|
||||
public bool IsActive(AmbientSoundComponent component)
|
||||
{
|
||||
return _playingSounds.ContainsKey(component);
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
UpdatesOutsidePrediction = true;
|
||||
UpdatesAfter.Add(typeof(AmbientSoundTreeSystem));
|
||||
|
||||
_cfg.OnValueChanged(CCVars.AmbientCooldown, SetCooldown, true);
|
||||
_cfg.OnValueChanged(CCVars.MaxAmbientSources, SetAmbientCount, true);
|
||||
_cfg.OnValueChanged(CCVars.AmbientRange, SetAmbientRange, true);
|
||||
_cfg.OnValueChanged(CCVars.AmbienceVolume, SetAmbienceVolume, true);
|
||||
SubscribeLocalEvent<AmbientSoundComponent, ComponentShutdown>(OnShutdown);
|
||||
}
|
||||
|
||||
private void OnShutdown(EntityUid uid, AmbientSoundComponent component, ComponentShutdown args)
|
||||
{
|
||||
if (!_playingSounds.Remove(component, out var sound))
|
||||
return;
|
||||
|
||||
sound.Stream?.Stop();
|
||||
_playingCount[sound.Sound] -= 1;
|
||||
if (_playingCount[sound.Sound] == 0)
|
||||
_playingCount.Remove(sound.Sound);
|
||||
}
|
||||
|
||||
private void SetAmbienceVolume(float value)
|
||||
{
|
||||
_ambienceVolume = value;
|
||||
|
||||
foreach (var (comp, values) in _playingSounds)
|
||||
{
|
||||
return _playingSounds.ContainsKey(component);
|
||||
if (values.Stream == null)
|
||||
continue;
|
||||
|
||||
var stream = (AudioSystem.PlayingStream) values.Stream;
|
||||
stream.Volume = _params.Volume + comp.Volume + _ambienceVolume;
|
||||
}
|
||||
}
|
||||
private void SetCooldown(float value) => _cooldown = value;
|
||||
private void SetAmbientCount(int value) => _maxAmbientCount = value;
|
||||
private void SetAmbientRange(float value) => _maxAmbientRange = value;
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
ClearSounds();
|
||||
|
||||
_cfg.UnsubValueChanged(CCVars.AmbientCooldown, SetCooldown);
|
||||
_cfg.UnsubValueChanged(CCVars.MaxAmbientSources, SetAmbientCount);
|
||||
_cfg.UnsubValueChanged(CCVars.AmbientRange, SetAmbientRange);
|
||||
_cfg.UnsubValueChanged(CCVars.AmbienceVolume, SetAmbienceVolume);
|
||||
}
|
||||
|
||||
private int PlayingCount(string countSound)
|
||||
{
|
||||
var count = 0;
|
||||
|
||||
foreach (var (_, (_, sound)) in _playingSounds)
|
||||
{
|
||||
if (sound.Equals(countSound))
|
||||
count++;
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
UpdatesOutsidePrediction = true;
|
||||
UpdatesAfter.Add(typeof(AmbientSoundTreeSystem));
|
||||
return count;
|
||||
}
|
||||
|
||||
_cfg.OnValueChanged(CCVars.AmbientCooldown, SetCooldown, true);
|
||||
_cfg.OnValueChanged(CCVars.MaxAmbientSources, SetAmbientCount, true);
|
||||
_cfg.OnValueChanged(CCVars.AmbientRange, SetAmbientRange, true);
|
||||
_cfg.OnValueChanged(CCVars.AmbienceVolume, SetAmbienceVolume, true);
|
||||
SubscribeLocalEvent<AmbientSoundComponent, ComponentShutdown>(OnShutdown);
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
if (!_gameTiming.IsFirstTimePredicted)
|
||||
return;
|
||||
|
||||
if (_cooldown <= 0f)
|
||||
return;
|
||||
|
||||
if (_gameTiming.CurTime < _targetTime)
|
||||
return;
|
||||
|
||||
_targetTime = _gameTiming.CurTime+TimeSpan.FromSeconds(_cooldown);
|
||||
|
||||
var player = _playerManager.LocalPlayer?.ControlledEntity;
|
||||
if (!EntityManager.TryGetComponent(player, out TransformComponent? xform))
|
||||
{
|
||||
ClearSounds();
|
||||
return;
|
||||
}
|
||||
|
||||
private void OnShutdown(EntityUid uid, AmbientSoundComponent component, ComponentShutdown args)
|
||||
ProcessNearbyAmbience(xform);
|
||||
}
|
||||
|
||||
private void ClearSounds()
|
||||
{
|
||||
foreach (var (stream, _) in _playingSounds.Values)
|
||||
{
|
||||
if (!_playingSounds.Remove(component, out var sound))
|
||||
return;
|
||||
stream?.Stop();
|
||||
}
|
||||
|
||||
_playingSounds.Clear();
|
||||
_playingCount.Clear();
|
||||
}
|
||||
|
||||
private readonly struct QueryState
|
||||
{
|
||||
public readonly Dictionary<string, List<(float Importance, AmbientSoundComponent)>> SourceDict = new();
|
||||
public readonly Vector2 MapPos;
|
||||
public readonly TransformComponent Player;
|
||||
public readonly EntityQuery<TransformComponent> Query;
|
||||
|
||||
public QueryState(Vector2 mapPos, TransformComponent player, EntityQuery<TransformComponent> query)
|
||||
{
|
||||
MapPos = mapPos;
|
||||
Player = player;
|
||||
Query = query;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool Callback(
|
||||
ref QueryState state,
|
||||
in ComponentTreeEntry<AmbientSoundComponent> value)
|
||||
{
|
||||
var (ambientComp, xform) = value;
|
||||
|
||||
DebugTools.Assert(ambientComp.Enabled);
|
||||
|
||||
var delta = xform.ParentUid == state.Player.ParentUid
|
||||
? xform.LocalPosition - state.Player.LocalPosition
|
||||
: xform.WorldPosition - state.MapPos;
|
||||
|
||||
var range = delta.Length;
|
||||
if (range >= ambientComp.Range)
|
||||
return true;
|
||||
|
||||
string key;
|
||||
|
||||
if (ambientComp.Sound is SoundPathSpecifier path)
|
||||
key = path.Path.ToString();
|
||||
else
|
||||
key = ((SoundCollectionSpecifier) ambientComp.Sound).Collection ?? string.Empty;
|
||||
|
||||
// Prioritize far away & loud sounds.
|
||||
var importance = range * (ambientComp.Volume + 32);
|
||||
state.SourceDict.GetOrNew(key).Add((importance, ambientComp));
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a list of ambient components in range and determine which ones to start playing.
|
||||
/// </summary>
|
||||
private void ProcessNearbyAmbience(TransformComponent playerXform)
|
||||
{
|
||||
var query = GetEntityQuery<TransformComponent>();
|
||||
var metaQuery = GetEntityQuery<MetaDataComponent>();
|
||||
var mapPos = playerXform.MapPosition;
|
||||
|
||||
// Remove out-of-range ambiences
|
||||
foreach (var (comp, sound) in _playingSounds)
|
||||
{
|
||||
var entity = comp.Owner;
|
||||
|
||||
if (comp.Enabled &&
|
||||
query.TryGetComponent(entity, out var xform) &&
|
||||
xform.MapID == playerXform.MapID &&
|
||||
!metaQuery.GetComponent(entity).EntityPaused)
|
||||
{
|
||||
var distance = (xform.ParentUid == playerXform.ParentUid)
|
||||
? xform.LocalPosition - playerXform.LocalPosition
|
||||
: xform.WorldPosition - mapPos.Position;
|
||||
|
||||
if (distance.LengthSquared < comp.Range * comp.Range)
|
||||
continue;
|
||||
}
|
||||
|
||||
sound.Stream?.Stop();
|
||||
_playingSounds.Remove(comp);
|
||||
_playingCount[sound.Sound] -= 1;
|
||||
if (_playingCount[sound.Sound] == 0)
|
||||
_playingCount.Remove(sound.Sound);
|
||||
}
|
||||
|
||||
private void SetAmbienceVolume(float value)
|
||||
{
|
||||
_ambienceVolume = value;
|
||||
if (_playingSounds.Count >= _maxAmbientCount)
|
||||
return;
|
||||
|
||||
foreach (var (comp, values) in _playingSounds)
|
||||
var pos = mapPos.Position;
|
||||
var state = new QueryState(pos, playerXform, query);
|
||||
var worldAabb = new Box2(pos - _maxAmbientRange, pos + _maxAmbientRange);
|
||||
_treeSys.QueryAabb(ref state, Callback, mapPos.MapId, worldAabb);
|
||||
|
||||
// Add in range ambiences
|
||||
foreach (var (key, sources) in state.SourceDict)
|
||||
{
|
||||
if (_playingSounds.Count >= _maxAmbientCount)
|
||||
break;
|
||||
|
||||
if (_playingCount.TryGetValue(key, out var playingCount) && playingCount >= MaxSingleSound)
|
||||
continue;
|
||||
|
||||
sources.Sort(static (a, b) => b.Importance.CompareTo(a.Importance));
|
||||
|
||||
foreach (var (_, comp) in sources)
|
||||
{
|
||||
if (values.Stream == null)
|
||||
var uid = comp.Owner;
|
||||
|
||||
if (_playingSounds.ContainsKey(comp) ||
|
||||
metaQuery.GetComponent(uid).EntityPaused)
|
||||
continue;
|
||||
|
||||
var stream = (AudioSystem.PlayingStream) values.Stream;
|
||||
stream.Volume = _params.Volume + comp.Volume + _ambienceVolume;
|
||||
}
|
||||
}
|
||||
private void SetCooldown(float value) => _cooldown = value;
|
||||
private void SetAmbientCount(int value) => _maxAmbientCount = value;
|
||||
private void SetAmbientRange(float value) => _maxAmbientRange = value;
|
||||
var audioParams = _params
|
||||
.AddVolume(comp.Volume + _ambienceVolume)
|
||||
// Randomise start so 2 sources don't increase their volume.
|
||||
.WithPlayOffset(_random.NextFloat(0.0f, 100.0f))
|
||||
.WithMaxDistance(comp.Range);
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
ClearSounds();
|
||||
var stream = _audio.PlayPvs(comp.Sound, uid, audioParams);
|
||||
if (stream == null)
|
||||
continue;
|
||||
|
||||
_cfg.UnsubValueChanged(CCVars.AmbientCooldown, SetCooldown);
|
||||
_cfg.UnsubValueChanged(CCVars.MaxAmbientSources, SetAmbientCount);
|
||||
_cfg.UnsubValueChanged(CCVars.AmbientRange, SetAmbientRange);
|
||||
_cfg.UnsubValueChanged(CCVars.AmbienceVolume, SetAmbienceVolume);
|
||||
}
|
||||
_playingSounds[comp] = (stream, key);
|
||||
playingCount++;
|
||||
|
||||
private int PlayingCount(string countSound)
|
||||
{
|
||||
var count = 0;
|
||||
|
||||
foreach (var (_, (_, sound)) in _playingSounds)
|
||||
{
|
||||
if (sound.Equals(countSound))
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
if (!_gameTiming.IsFirstTimePredicted)
|
||||
return;
|
||||
|
||||
if (_cooldown <= 0f)
|
||||
return;
|
||||
|
||||
if (_gameTiming.CurTime < _targetTime)
|
||||
return;
|
||||
|
||||
_targetTime = _gameTiming.CurTime+TimeSpan.FromSeconds(_cooldown);
|
||||
|
||||
var player = _playerManager.LocalPlayer?.ControlledEntity;
|
||||
if (!EntityManager.TryGetComponent(player, out TransformComponent? xform))
|
||||
{
|
||||
ClearSounds();
|
||||
return;
|
||||
}
|
||||
|
||||
ProcessNearbyAmbience(xform);
|
||||
}
|
||||
|
||||
private void ClearSounds()
|
||||
{
|
||||
foreach (var (stream, _) in _playingSounds.Values)
|
||||
{
|
||||
stream?.Stop();
|
||||
}
|
||||
|
||||
_playingSounds.Clear();
|
||||
_playingCount.Clear();
|
||||
}
|
||||
|
||||
private readonly struct QueryState
|
||||
{
|
||||
public readonly Dictionary<string, List<(float Importance, AmbientSoundComponent)>> SourceDict = new();
|
||||
public readonly Vector2 MapPos;
|
||||
public readonly TransformComponent Player;
|
||||
public readonly EntityQuery<TransformComponent> Query;
|
||||
|
||||
public QueryState(Vector2 mapPos, TransformComponent player, EntityQuery<TransformComponent> query)
|
||||
{
|
||||
MapPos = mapPos;
|
||||
Player = player;
|
||||
Query = query;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool Callback(
|
||||
ref QueryState state,
|
||||
in ComponentTreeEntry<AmbientSoundComponent> value)
|
||||
{
|
||||
var (ambientComp, xform) = value;
|
||||
|
||||
DebugTools.Assert(ambientComp.Enabled);
|
||||
|
||||
var delta = xform.ParentUid == state.Player.ParentUid
|
||||
? xform.LocalPosition - state.Player.LocalPosition
|
||||
: xform.WorldPosition - state.MapPos;
|
||||
|
||||
var range = delta.Length;
|
||||
if (range >= ambientComp.Range)
|
||||
return true;
|
||||
|
||||
string key;
|
||||
|
||||
if (ambientComp.Sound is SoundPathSpecifier path)
|
||||
key = path.Path.ToString();
|
||||
else
|
||||
key = ((SoundCollectionSpecifier) ambientComp.Sound).Collection ?? string.Empty;
|
||||
|
||||
// Prioritize far away & loud sounds.
|
||||
var importance = range * (ambientComp.Volume + 32);
|
||||
state.SourceDict.GetOrNew(key).Add((importance, ambientComp));
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a list of ambient components in range and determine which ones to start playing.
|
||||
/// </summary>
|
||||
private void ProcessNearbyAmbience(TransformComponent playerXform)
|
||||
{
|
||||
var query = GetEntityQuery<TransformComponent>();
|
||||
var metaQuery = GetEntityQuery<MetaDataComponent>();
|
||||
var mapPos = playerXform.MapPosition;
|
||||
|
||||
// Remove out-of-range ambiences
|
||||
foreach (var (comp, sound) in _playingSounds)
|
||||
{
|
||||
var entity = comp.Owner;
|
||||
|
||||
if (comp.Enabled &&
|
||||
query.TryGetComponent(entity, out var xform) &&
|
||||
xform.MapID == playerXform.MapID &&
|
||||
!metaQuery.GetComponent(entity).EntityPaused)
|
||||
{
|
||||
var distance = (xform.ParentUid == playerXform.ParentUid)
|
||||
? xform.LocalPosition - playerXform.LocalPosition
|
||||
: xform.WorldPosition - mapPos.Position;
|
||||
|
||||
if (distance.LengthSquared < comp.Range * comp.Range)
|
||||
continue;
|
||||
}
|
||||
|
||||
sound.Stream?.Stop();
|
||||
_playingSounds.Remove(comp);
|
||||
_playingCount[sound.Sound] -= 1;
|
||||
if (_playingCount[sound.Sound] == 0)
|
||||
_playingCount.Remove(sound.Sound);
|
||||
}
|
||||
|
||||
if (_playingSounds.Count >= _maxAmbientCount)
|
||||
return;
|
||||
|
||||
var pos = mapPos.Position;
|
||||
var state = new QueryState(pos, playerXform, query);
|
||||
var worldAabb = new Box2(pos - _maxAmbientRange, pos + _maxAmbientRange);
|
||||
_treeSys.QueryAabb(ref state, Callback, mapPos.MapId, worldAabb);
|
||||
|
||||
// Add in range ambiences
|
||||
foreach (var (key, sources) in state.SourceDict)
|
||||
{
|
||||
if (_playingSounds.Count >= _maxAmbientCount)
|
||||
break;
|
||||
|
||||
if (_playingCount.TryGetValue(key, out var playingCount) && playingCount >= MaxSingleSound)
|
||||
continue;
|
||||
|
||||
sources.Sort(static (a, b) => b.Importance.CompareTo(a.Importance));
|
||||
|
||||
foreach (var (_, comp) in sources)
|
||||
{
|
||||
var uid = comp.Owner;
|
||||
|
||||
if (_playingSounds.ContainsKey(comp) ||
|
||||
metaQuery.GetComponent(uid).EntityPaused)
|
||||
continue;
|
||||
|
||||
var audioParams = _params
|
||||
.AddVolume(comp.Volume + _ambienceVolume)
|
||||
// Randomise start so 2 sources don't increase their volume.
|
||||
.WithPlayOffset(_random.NextFloat(0.0f, 100.0f))
|
||||
.WithMaxDistance(comp.Range);
|
||||
|
||||
var stream = _audio.PlayPvs(comp.Sound, uid, audioParams);
|
||||
if (stream == null)
|
||||
continue;
|
||||
|
||||
_playingSounds[comp] = (stream, key);
|
||||
playingCount++;
|
||||
|
||||
if (_playingSounds.Count >= _maxAmbientCount)
|
||||
break;
|
||||
}
|
||||
|
||||
if (playingCount != 0)
|
||||
_playingCount[key] = playingCount;
|
||||
}
|
||||
|
||||
DebugTools.Assert(_playingCount.All(x => x.Value == PlayingCount(x.Key)));
|
||||
if (playingCount != 0)
|
||||
_playingCount[key] = playingCount;
|
||||
}
|
||||
|
||||
DebugTools.Assert(_playingCount.All(x => x.Value == PlayingCount(x.Key)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,6 +245,12 @@ public sealed partial class ContentAudioSystem
|
||||
if (player == null)
|
||||
return null;
|
||||
|
||||
var ev = new PlayAmbientMusicEvent();
|
||||
RaiseLocalEvent(ref ev);
|
||||
|
||||
if (ev.Cancelled)
|
||||
return null;
|
||||
|
||||
var ambiences = _proto.EnumeratePrototypes<AmbientMusicPrototype>().ToList();
|
||||
ambiences.Sort((x, y) => y.Priority.CompareTo(x.Priority));
|
||||
|
||||
@@ -259,4 +265,13 @@ public sealed partial class ContentAudioSystem
|
||||
_sawmill.Warning($"Unable to find fallback ambience track");
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fades out the current ambient music temporarily.
|
||||
/// </summary>
|
||||
public void DisableAmbientMusic()
|
||||
{
|
||||
FadeOut(_ambientMusicStream);
|
||||
_ambientMusicStream = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,3 +122,9 @@ public sealed partial class ContentAudioSystem : SharedContentAudioSystem
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised whenever ambient music tries to play.
|
||||
/// </summary>
|
||||
[ByRefEvent]
|
||||
public record struct PlayAmbientMusicEvent(bool Cancelled = false);
|
||||
|
||||
9
Content.Client/Salvage/SalvageExpeditionComponent.cs
Normal file
9
Content.Client/Salvage/SalvageExpeditionComponent.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Content.Shared.Salvage.Expeditions;
|
||||
|
||||
namespace Content.Client.Salvage;
|
||||
|
||||
[RegisterComponent]
|
||||
public sealed class SalvageExpeditionComponent : SharedSalvageExpeditionComponent
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,8 +1,50 @@
|
||||
using Content.Client.Audio;
|
||||
using Content.Shared.Salvage;
|
||||
using Content.Shared.Salvage.Expeditions;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Client.Salvage;
|
||||
|
||||
public sealed class SalvageSystem : SharedSalvageSystem
|
||||
{
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly ContentAudioSystem _audio = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<PlayAmbientMusicEvent>(OnPlayAmbientMusic);
|
||||
SubscribeLocalEvent<SalvageExpeditionComponent, ComponentHandleState>(OnExpeditionHandleState);
|
||||
}
|
||||
|
||||
private void OnExpeditionHandleState(EntityUid uid, SalvageExpeditionComponent component, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not SalvageExpeditionComponentState state)
|
||||
return;
|
||||
|
||||
component.Stage = state.Stage;
|
||||
|
||||
if (component.Stage >= ExpeditionStage.MusicCountdown)
|
||||
{
|
||||
_audio.DisableAmbientMusic();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPlayAmbientMusic(ref PlayAmbientMusicEvent ev)
|
||||
{
|
||||
if (ev.Cancelled)
|
||||
return;
|
||||
|
||||
var player = _playerManager.LocalPlayer?.ControlledEntity;
|
||||
|
||||
if (!TryComp<TransformComponent>(player, out var xform) ||
|
||||
!TryComp<SalvageExpeditionComponent>(xform.MapUid, out var expedition) ||
|
||||
expedition.Stage < ExpeditionStage.MusicCountdown)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ev.Cancelled = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Content.Shared.Salvage;
|
||||
using Content.Shared.Salvage.Expeditions;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client.GameObjects;
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ using Content.Client.UserInterface.Controls;
|
||||
using Content.Shared.CCVar;
|
||||
using Content.Shared.Parallax.Biomes;
|
||||
using Content.Shared.Salvage;
|
||||
using Content.Shared.Salvage.Expeditions;
|
||||
using Content.Shared.Salvage.Expeditions.Modifiers;
|
||||
using Content.Shared.Shuttles.BUIStates;
|
||||
using Robust.Client.AutoGenerated;
|
||||
|
||||
@@ -2,25 +2,24 @@ using Content.Server.Power.Components;
|
||||
using Content.Server.Power.EntitySystems;
|
||||
using Content.Shared.Audio;
|
||||
|
||||
namespace Content.Server.Audio
|
||||
namespace Content.Server.Audio;
|
||||
|
||||
public sealed class AmbientSoundSystem : SharedAmbientSoundSystem
|
||||
{
|
||||
public sealed class AmbientSoundSystem : SharedAmbientSoundSystem
|
||||
public override void Initialize()
|
||||
{
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<AmbientOnPoweredComponent, PowerChangedEvent>(HandlePowerChange);
|
||||
SubscribeLocalEvent<AmbientOnPoweredComponent, PowerNetBatterySupplyEvent>(HandlePowerSupply);
|
||||
}
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<AmbientOnPoweredComponent, PowerChangedEvent>(HandlePowerChange);
|
||||
SubscribeLocalEvent<AmbientOnPoweredComponent, PowerNetBatterySupplyEvent>(HandlePowerSupply);
|
||||
}
|
||||
|
||||
private void HandlePowerSupply(EntityUid uid, AmbientOnPoweredComponent component, ref PowerNetBatterySupplyEvent args)
|
||||
{
|
||||
SetAmbience(uid, args.Supply);
|
||||
}
|
||||
private void HandlePowerSupply(EntityUid uid, AmbientOnPoweredComponent component, ref PowerNetBatterySupplyEvent args)
|
||||
{
|
||||
SetAmbience(uid, args.Supply);
|
||||
}
|
||||
|
||||
private void HandlePowerChange(EntityUid uid, AmbientOnPoweredComponent component, ref PowerChangedEvent args)
|
||||
{
|
||||
SetAmbience(uid, args.Powered);
|
||||
}
|
||||
private void HandlePowerChange(EntityUid uid, AmbientOnPoweredComponent component, ref PowerChangedEvent args)
|
||||
{
|
||||
SetAmbience(uid, args.Powered);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
using Content.Shared.Random;
|
||||
using Content.Shared.Salvage;
|
||||
using Content.Shared.Salvage.Expeditions;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
|
||||
|
||||
namespace Content.Server.Salvage.Expeditions;
|
||||
@@ -12,7 +11,7 @@ namespace Content.Server.Salvage.Expeditions;
|
||||
/// Designates this entity as holding a salvage expedition.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class SalvageExpeditionComponent : Component
|
||||
public sealed class SalvageExpeditionComponent : SharedSalvageExpeditionComponent
|
||||
{
|
||||
public SalvageMissionParams MissionParams = default!;
|
||||
|
||||
@@ -36,9 +35,6 @@ public sealed class SalvageExpeditionComponent : Component
|
||||
|
||||
[ViewVariables] public bool Completed = false;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("stage")]
|
||||
public ExpeditionStage Stage = ExpeditionStage.Added;
|
||||
|
||||
/// <summary>
|
||||
/// Countdown audio stream.
|
||||
/// </summary>
|
||||
@@ -50,7 +46,7 @@ public sealed class SalvageExpeditionComponent : Component
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("sound")]
|
||||
public SoundSpecifier Sound = new SoundPathSpecifier("/Audio/Misc/tension_session.ogg")
|
||||
{
|
||||
Params = AudioParams.Default.WithVolume(-15),
|
||||
Params = AudioParams.Default.WithVolume(-5),
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
@@ -65,12 +61,3 @@ public sealed class SalvageExpeditionComponent : Component
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("rewards", customTypeSerializer: typeof(PrototypeIdListSerializer<EntityPrototype>))]
|
||||
public List<string> Rewards = default!;
|
||||
}
|
||||
|
||||
public enum ExpeditionStage : byte
|
||||
{
|
||||
Added,
|
||||
Running,
|
||||
Countdown,
|
||||
MusicCountdown,
|
||||
FinalCountdown,
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Content.Shared.Salvage;
|
||||
using Content.Shared.Salvage.Expeditions;
|
||||
using Robust.Server.GameObjects;
|
||||
|
||||
namespace Content.Server.Salvage;
|
||||
|
||||
@@ -2,16 +2,15 @@ using Content.Server.Cargo.Components;
|
||||
using Content.Server.Cargo.Systems;
|
||||
using Content.Server.Salvage.Expeditions;
|
||||
using Content.Server.Salvage.Expeditions.Structure;
|
||||
using Content.Server.Station.Systems;
|
||||
using Content.Shared.CCVar;
|
||||
using Content.Shared.Random;
|
||||
using Content.Shared.Random.Helpers;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Salvage;
|
||||
using Robust.Shared.CPUJob.JobQueues;
|
||||
using Robust.Shared.CPUJob.JobQueues.Queues;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Content.Shared.Salvage.Expeditions;
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Server.Salvage;
|
||||
|
||||
@@ -42,6 +41,7 @@ public sealed partial class SalvageSystem
|
||||
|
||||
SubscribeLocalEvent<SalvageExpeditionComponent, ComponentShutdown>(OnExpeditionShutdown);
|
||||
SubscribeLocalEvent<SalvageExpeditionComponent, EntityUnpausedEvent>(OnExpeditionUnpaused);
|
||||
SubscribeLocalEvent<SalvageExpeditionComponent, ComponentGetState>(OnExpeditionGetState);
|
||||
|
||||
SubscribeLocalEvent<SalvageStructureComponent, ExaminedEvent>(OnStructureExamine);
|
||||
|
||||
@@ -51,6 +51,14 @@ public sealed partial class SalvageSystem
|
||||
_configurationManager.OnValueChanged(CCVars.SalvageExpeditionFailedCooldown, SetFailedCooldownChange);
|
||||
}
|
||||
|
||||
private void OnExpeditionGetState(EntityUid uid, SalvageExpeditionComponent component, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new SalvageExpeditionComponentState()
|
||||
{
|
||||
Stage = component.Stage
|
||||
};
|
||||
}
|
||||
|
||||
private void ShutdownExpeditions()
|
||||
{
|
||||
_configurationManager.UnsubValueChanged(CCVars.SalvageExpeditionCooldown, SetCooldownChange);
|
||||
|
||||
@@ -8,9 +8,7 @@ using Content.Shared.Chat;
|
||||
using Content.Shared.Humanoid;
|
||||
using Content.Shared.Mobs.Components;
|
||||
using Content.Shared.Mobs.Systems;
|
||||
using Content.Shared.Salvage;
|
||||
using Content.Shared.Shuttles.Components;
|
||||
using Robust.Shared.Audio;
|
||||
using Content.Shared.Salvage.Expeditions;
|
||||
using Robust.Shared.Map.Components;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Utility;
|
||||
@@ -110,6 +108,7 @@ public sealed partial class SalvageSystem
|
||||
Announce(args.MapUid, Loc.GetString("salvage-expedition-announcement-dungeon", ("direction", component.DungeonLocation.GetDir())));
|
||||
|
||||
component.Stage = ExpeditionStage.Running;
|
||||
Dirty(component);
|
||||
}
|
||||
|
||||
private void OnFTLStarted(ref FTLStartedEvent ev)
|
||||
@@ -158,6 +157,7 @@ public sealed partial class SalvageSystem
|
||||
if (comp.Stage < ExpeditionStage.FinalCountdown && remaining < TimeSpan.FromSeconds(30))
|
||||
{
|
||||
comp.Stage = ExpeditionStage.FinalCountdown;
|
||||
Dirty(comp);
|
||||
Announce(uid, Loc.GetString("salvage-expedition-announcement-countdown-seconds", ("duration", TimeSpan.FromSeconds(30).Seconds)));
|
||||
}
|
||||
else if (comp.Stage < ExpeditionStage.MusicCountdown && remaining < TimeSpan.FromMinutes(2))
|
||||
@@ -165,11 +165,13 @@ public sealed partial class SalvageSystem
|
||||
// TODO: Some way to play audio attached to a map for players.
|
||||
comp.Stream = _audio.PlayGlobal(comp.Sound, Filter.BroadcastMap(Comp<MapComponent>(uid).MapId), true);
|
||||
comp.Stage = ExpeditionStage.MusicCountdown;
|
||||
Dirty(comp);
|
||||
Announce(uid, Loc.GetString("salvage-expedition-announcement-countdown-minutes", ("duration", TimeSpan.FromMinutes(2).Minutes)));
|
||||
}
|
||||
else if (comp.Stage < ExpeditionStage.Countdown && remaining < TimeSpan.FromMinutes(5))
|
||||
{
|
||||
comp.Stage = ExpeditionStage.Countdown;
|
||||
Dirty(comp);
|
||||
Announce(uid, Loc.GetString("salvage-expedition-announcement-countdown-minutes", ("duration", TimeSpan.FromMinutes(5).Minutes)));
|
||||
}
|
||||
// Auto-FTL out any shuttles
|
||||
|
||||
@@ -1,66 +1,65 @@
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Shared.Audio
|
||||
namespace Content.Shared.Audio;
|
||||
|
||||
public abstract class SharedAmbientSoundSystem : EntitySystem
|
||||
{
|
||||
public abstract class SharedAmbientSoundSystem : EntitySystem
|
||||
public override void Initialize()
|
||||
{
|
||||
public override void Initialize()
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<AmbientSoundComponent, ComponentGetState>(GetCompState);
|
||||
SubscribeLocalEvent<AmbientSoundComponent, ComponentHandleState>(HandleCompState);
|
||||
}
|
||||
|
||||
public virtual void SetAmbience(EntityUid uid, bool value, AmbientSoundComponent? ambience = null)
|
||||
{
|
||||
if (!Resolve(uid, ref ambience, false) || ambience.Enabled == value)
|
||||
return;
|
||||
|
||||
ambience.Enabled = value;
|
||||
QueueUpdate(uid, ambience);
|
||||
Dirty(ambience);
|
||||
}
|
||||
|
||||
public virtual void SetRange(EntityUid uid, float value, AmbientSoundComponent? ambience = null)
|
||||
{
|
||||
if (!Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Range, value))
|
||||
return;
|
||||
|
||||
ambience.Range = value;
|
||||
QueueUpdate(uid, ambience);
|
||||
Dirty(ambience);
|
||||
}
|
||||
|
||||
protected virtual void QueueUpdate(EntityUid uid, AmbientSoundComponent ambience)
|
||||
{
|
||||
// client side tree
|
||||
}
|
||||
|
||||
public virtual void SetVolume(EntityUid uid, float value, AmbientSoundComponent? ambience = null)
|
||||
{
|
||||
if (!Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Volume, value))
|
||||
return;
|
||||
|
||||
ambience.Volume = value;
|
||||
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);
|
||||
}
|
||||
|
||||
private void GetCompState(EntityUid uid, AmbientSoundComponent component, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new AmbientSoundComponentState
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<AmbientSoundComponent, ComponentGetState>(GetCompState);
|
||||
SubscribeLocalEvent<AmbientSoundComponent, ComponentHandleState>(HandleCompState);
|
||||
}
|
||||
|
||||
public virtual void SetAmbience(EntityUid uid, bool value, AmbientSoundComponent? ambience = null)
|
||||
{
|
||||
if (!Resolve(uid, ref ambience, false) || ambience.Enabled == value)
|
||||
return;
|
||||
|
||||
ambience.Enabled = value;
|
||||
QueueUpdate(uid, ambience);
|
||||
Dirty(ambience);
|
||||
}
|
||||
|
||||
public virtual void SetRange(EntityUid uid, float value, AmbientSoundComponent? ambience = null)
|
||||
{
|
||||
if (!Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Range, value))
|
||||
return;
|
||||
|
||||
ambience.Range = value;
|
||||
QueueUpdate(uid, ambience);
|
||||
Dirty(ambience);
|
||||
}
|
||||
|
||||
protected virtual void QueueUpdate(EntityUid uid, AmbientSoundComponent ambience)
|
||||
{
|
||||
// client side tree
|
||||
}
|
||||
|
||||
public virtual void SetVolume(EntityUid uid, float value, AmbientSoundComponent? ambience = null)
|
||||
{
|
||||
if (!Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Volume, value))
|
||||
return;
|
||||
|
||||
ambience.Volume = value;
|
||||
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);
|
||||
}
|
||||
|
||||
private void GetCompState(EntityUid uid, AmbientSoundComponent component, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new AmbientSoundComponentState
|
||||
{
|
||||
Enabled = component.Enabled,
|
||||
Range = component.Range,
|
||||
Volume = component.Volume,
|
||||
};
|
||||
}
|
||||
Enabled = component.Enabled,
|
||||
Range = component.Range,
|
||||
Volume = component.Volume,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
10
Content.Shared/Salvage/Expeditions/ExpeditionStage.cs
Normal file
10
Content.Shared/Salvage/Expeditions/ExpeditionStage.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace Content.Shared.Salvage.Expeditions;
|
||||
|
||||
public enum ExpeditionStage : byte
|
||||
{
|
||||
Added,
|
||||
Running,
|
||||
Countdown,
|
||||
MusicCountdown,
|
||||
FinalCountdown,
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
using Content.Shared.Salvage.Expeditions;
|
||||
using Content.Shared.Salvage.Expeditions.Modifiers;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
|
||||
|
||||
namespace Content.Shared.Salvage;
|
||||
namespace Content.Shared.Salvage.Expeditions;
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class SalvageExpeditionConsoleState : BoundUserInterfaceState
|
||||
@@ -0,0 +1,17 @@
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Salvage.Expeditions;
|
||||
|
||||
[NetworkedComponent]
|
||||
public abstract class SharedSalvageExpeditionComponent : Component
|
||||
{
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("stage")]
|
||||
public ExpeditionStage Stage = ExpeditionStage.Added;
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class SalvageExpeditionComponentState : ComponentState
|
||||
{
|
||||
public ExpeditionStage Stage;
|
||||
}
|
||||
Reference in New Issue
Block a user