Cleanup audio (#11238)
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
using Content.Client.GameTicking.Managers;
|
||||
using System.Threading;
|
||||
using Content.Client.Gameplay;
|
||||
using Content.Client.GameTicking.Managers;
|
||||
using Content.Client.Lobby;
|
||||
using Content.Shared.CCVar;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Client.ResourceManagement;
|
||||
using Robust.Client.State;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Configuration;
|
||||
@@ -13,24 +15,22 @@ using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.ResourceManagement;
|
||||
using Timer = Robust.Shared.Timing.Timer;
|
||||
|
||||
namespace Content.Client.Audio
|
||||
{
|
||||
namespace Content.Client.Audio;
|
||||
|
||||
[UsedImplicitly]
|
||||
public sealed class BackgroundAudioSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||
[Dependency] private readonly IBaseClient _client = default!;
|
||||
[Dependency] private readonly IConfigurationManager _configManager = default!;
|
||||
[Dependency] private readonly IGameTiming _timing = default!;
|
||||
[Dependency] private readonly ClientGameTicker _gameTicker = default!;
|
||||
[Dependency] private readonly IPlayerManager _playMan = default!;
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
||||
[Dependency] private readonly IStateManager _stateManager = default!;
|
||||
[Dependency] private readonly ClientGameTicker _gameTicker = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||
[Dependency] private readonly IGameTiming _timing = default!;
|
||||
|
||||
private readonly AudioParams _ambientParams = new(-10f, 1, "Master", 0, 0, 0, true, 0f);
|
||||
private readonly AudioParams _lobbyParams = new(-5f, 1, "Master", 0, 0, 0, true, 0f);
|
||||
@@ -60,7 +60,7 @@ 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
|
||||
// TODO: 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)
|
||||
@@ -79,7 +79,6 @@ namespace Content.Client.Audio
|
||||
|
||||
_stateManager.OnStateChanged += StateManagerOnStateChanged;
|
||||
|
||||
_client.PlayerJoinedServer += OnJoin;
|
||||
_client.PlayerLeaveServer += OnLeave;
|
||||
|
||||
_gameTicker.LobbyStatusUpdated += LobbySongReceived;
|
||||
@@ -109,7 +108,6 @@ namespace Content.Client.Audio
|
||||
|
||||
_stateManager.OnStateChanged -= StateManagerOnStateChanged;
|
||||
|
||||
_client.PlayerJoinedServer -= OnJoin;
|
||||
_client.PlayerLeaveServer -= OnLeave;
|
||||
|
||||
_gameTicker.LobbyStatusUpdated -= LobbySongReceived;
|
||||
@@ -121,21 +119,16 @@ namespace Content.Client.Audio
|
||||
private void CheckAmbience(TransformComponent xform)
|
||||
{
|
||||
if (xform.GridUid != null)
|
||||
{
|
||||
if (_currentCollection == _stationAmbience)
|
||||
return;
|
||||
ChangeAmbience(_stationAmbience);
|
||||
}
|
||||
else
|
||||
{
|
||||
ChangeAmbience(_spaceAmbience);
|
||||
}
|
||||
}
|
||||
|
||||
private void EntParentChanged(ref EntParentChangedMessage message)
|
||||
{
|
||||
if(_playMan.LocalPlayer is null || _playMan.LocalPlayer.ControlledEntity != message.Entity ||
|
||||
!_timing.IsFirstTimePredicted)
|
||||
if (_playMan.LocalPlayer is null
|
||||
|| _playMan.LocalPlayer.ControlledEntity != message.Entity
|
||||
|| !_timing.IsFirstTimePredicted)
|
||||
return;
|
||||
|
||||
// Check if we traversed to grid.
|
||||
@@ -146,9 +139,10 @@ namespace Content.Client.Audio
|
||||
{
|
||||
if (_currentCollection == newAmbience)
|
||||
return;
|
||||
|
||||
_timerCancelTokenSource.Cancel();
|
||||
_currentCollection = newAmbience;
|
||||
_timerCancelTokenSource = new();
|
||||
_timerCancelTokenSource = new CancellationTokenSource();
|
||||
Timer.Spawn(1500, () =>
|
||||
{
|
||||
// If we traverse a few times then don't interrupt an existing song.
|
||||
@@ -161,32 +155,20 @@ namespace Content.Client.Audio
|
||||
|
||||
private void StateManagerOnStateChanged(StateChangedEventArgs args)
|
||||
{
|
||||
EndAmbience();
|
||||
|
||||
if (args.NewState is LobbyState)
|
||||
{
|
||||
StartLobbyMusic();
|
||||
return;
|
||||
}
|
||||
else if (args.NewState is GameplayState)
|
||||
{
|
||||
StartAmbience();
|
||||
}
|
||||
|
||||
EndLobbyMusic();
|
||||
}
|
||||
|
||||
private void OnJoin(object? sender, PlayerEventArgs args)
|
||||
{
|
||||
if (_stateManager.CurrentState is LobbyState)
|
||||
switch (args.NewState)
|
||||
{
|
||||
case LobbyState:
|
||||
EndAmbience();
|
||||
StartLobbyMusic();
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
case GameplayState:
|
||||
EndLobbyMusic();
|
||||
StartAmbience();
|
||||
break;
|
||||
default:
|
||||
EndAmbience();
|
||||
EndLobbyMusic();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -288,6 +270,7 @@ namespace Content.Client.Audio
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_stateManager.CurrentState is LobbyState)
|
||||
{
|
||||
StartLobbyMusic();
|
||||
@@ -320,4 +303,3 @@ namespace Content.Client.Audio
|
||||
_lobbyStream = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user