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