Fix ambient audio nags (#10698)

This commit is contained in:
metalgearsloth
2022-08-22 05:05:43 +10:00
committed by GitHub
parent 92929b5d28
commit 56b7b175b0

View File

@@ -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
/// <summary>
/// What the ambience has been set to.
/// </summary>
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<SoundCollectionPrototype>("SpaceAmbienceBase");
_currentCollection = _stationAmbience;
// TOOD: Ideally audio loading streamed better / we have more robust audio but this is quite annoying
var cache = IoCManager.Resolve<IResourceCache>();
foreach (var audio in _spaceAmbience.PickFiles)
{
cache.GetResource<AudioResource>(audio.ToString());
}
_configManager.OnValueChanged(CCVars.AmbienceVolume, AmbienceCVarChanged);
_configManager.OnValueChanged(CCVars.LobbyMusicEnabled, LobbyMusicCVarChanged);
_configManager.OnValueChanged(CCVars.StationAmbienceEnabled, StationAmbienceCVarChanged);
_configManager.OnValueChanged(CCVars.SpaceAmbienceEnabled, SpaceAmbienceCVarChanged);
SubscribeLocalEvent<PlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<EntParentChangedMessage>(EntParentChanged);
SubscribeLocalEvent<PlayerDetachedEvent>(OnPlayerDetached);
_stateManager.OnStateChanged += StateManagerOnStateChanged;
@@ -73,10 +85,28 @@ namespace Content.Client.Audio
_gameTicker.LobbyStatusUpdated += LobbySongReceived;
}
private void OnPlayerAttached(PlayerAttachedEvent ev)
{
if (!TryComp<TransformComponent>(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();