From 56b7b175b02f4efa13ae7e55904ccac3712ed61d Mon Sep 17 00:00:00 2001
From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Date: Mon, 22 Aug 2022 05:05:43 +1000
Subject: [PATCH] Fix ambient audio nags (#10698)
---
Content.Client/Audio/BackgroundAudioSystem.cs | 61 ++++++++++++++++---
1 file changed, 52 insertions(+), 9 deletions(-)
diff --git a/Content.Client/Audio/BackgroundAudioSystem.cs b/Content.Client/Audio/BackgroundAudioSystem.cs
index f4cbc5310b..3897e8e5d5 100644
--- a/Content.Client/Audio/BackgroundAudioSystem.cs
+++ b/Content.Client/Audio/BackgroundAudioSystem.cs
@@ -14,6 +14,8 @@ using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Timing;
using System.Threading;
+using Robust.Client.GameObjects;
+using Robust.Client.ResourceManagement;
using Timer = Robust.Shared.Timing.Timer;
namespace Content.Client.Audio
@@ -44,7 +46,7 @@ namespace Content.Client.Audio
///
/// What the ambience has been set to.
///
- private SoundCollectionPrototype _currentCollection = default!;
+ private SoundCollectionPrototype? _currentCollection;
private CancellationTokenSource _timerCancelTokenSource = new();
private SoundCollectionPrototype _spaceAmbience = default!;
@@ -58,12 +60,22 @@ namespace Content.Client.Audio
_spaceAmbience = _prototypeManager.Index("SpaceAmbienceBase");
_currentCollection = _stationAmbience;
+ // TOOD: Ideally audio loading streamed better / we have more robust audio but this is quite annoying
+ var cache = IoCManager.Resolve();
+
+ foreach (var audio in _spaceAmbience.PickFiles)
+ {
+ cache.GetResource(audio.ToString());
+ }
+
_configManager.OnValueChanged(CCVars.AmbienceVolume, AmbienceCVarChanged);
_configManager.OnValueChanged(CCVars.LobbyMusicEnabled, LobbyMusicCVarChanged);
_configManager.OnValueChanged(CCVars.StationAmbienceEnabled, StationAmbienceCVarChanged);
_configManager.OnValueChanged(CCVars.SpaceAmbienceEnabled, SpaceAmbienceCVarChanged);
+ SubscribeLocalEvent(OnPlayerAttached);
SubscribeLocalEvent(EntParentChanged);
+ SubscribeLocalEvent(OnPlayerDetached);
_stateManager.OnStateChanged += StateManagerOnStateChanged;
@@ -73,10 +85,28 @@ namespace Content.Client.Audio
_gameTicker.LobbyStatusUpdated += LobbySongReceived;
}
+ private void OnPlayerAttached(PlayerAttachedEvent ev)
+ {
+ if (!TryComp(ev.Entity, out var xform))
+ return;
+
+ CheckAmbience(xform);
+ }
+
+ private void OnPlayerDetached(PlayerDetachedEvent ev)
+ {
+ EndAmbience();
+ }
+
public override void Shutdown()
{
base.Shutdown();
+ _configManager.UnsubValueChanged(CCVars.AmbienceVolume, AmbienceCVarChanged);
+ _configManager.UnsubValueChanged(CCVars.LobbyMusicEnabled, LobbyMusicCVarChanged);
+ _configManager.UnsubValueChanged(CCVars.StationAmbienceEnabled, StationAmbienceCVarChanged);
+ _configManager.UnsubValueChanged(CCVars.SpaceAmbienceEnabled, SpaceAmbienceCVarChanged);
+
_stateManager.OnStateChanged -= StateManagerOnStateChanged;
_client.PlayerJoinedServer -= OnJoin;
@@ -88,15 +118,12 @@ namespace Content.Client.Audio
EndLobbyMusic();
}
- private void EntParentChanged(ref EntParentChangedMessage message)
+ private void CheckAmbience(TransformComponent xform)
{
- if(_playMan.LocalPlayer is null || _playMan.LocalPlayer.ControlledEntity != message.Entity ||
- !_timing.IsFirstTimePredicted) return;
-
- // Check if we traversed to grid.
- if (message.Transform.GridUid != null)
+ if (xform.GridUid != null)
{
- if (_currentCollection == _stationAmbience) return;
+ if (_currentCollection == _stationAmbience)
+ return;
ChangeAmbience(_stationAmbience);
}
else
@@ -105,6 +132,15 @@ namespace Content.Client.Audio
}
}
+ private void EntParentChanged(ref EntParentChangedMessage message)
+ {
+ if(_playMan.LocalPlayer is null || _playMan.LocalPlayer.ControlledEntity != message.Entity ||
+ !_timing.IsFirstTimePredicted) return;
+
+ // Check if we traversed to grid.
+ CheckAmbience(message.Transform);
+ }
+
private void ChangeAmbience(SoundCollectionPrototype newAmbience)
{
if (_currentCollection == newAmbience) return;
@@ -172,7 +208,8 @@ namespace Content.Client.Audio
private void StartAmbience()
{
EndAmbience();
- if (!CanPlayCollection(_currentCollection)) return;
+ if (_currentCollection == null || !CanPlayCollection(_currentCollection))
+ return;
_playingCollection = _currentCollection;
var file = _robustRandom.Pick(_currentCollection.PickFiles).ToString();
_ambientStream = SoundSystem.Play(file, Filter.Local(), _ambientParams.WithVolume(_ambientParams.Volume + _configManager.GetCVar(CCVars.AmbienceVolume)));
@@ -197,6 +234,9 @@ namespace Content.Client.Audio
private void StationAmbienceCVarChanged(bool enabled)
{
+ if (_currentCollection == null)
+ return;
+
if (enabled && _stateManager.CurrentState is GameScreen && _currentCollection.ID == _stationAmbience.ID)
{
StartAmbience();
@@ -209,6 +249,9 @@ namespace Content.Client.Audio
private void SpaceAmbienceCVarChanged(bool enabled)
{
+ if (_currentCollection == null)
+ return;
+
if (enabled && _stateManager.CurrentState is GameScreen && _currentCollection.ID == _spaceAmbience.ID)
{
StartAmbience();