Files
OldThink/Content.Client/Audio/BackgroundAudioSystem.cs

159 lines
4.3 KiB
C#
Raw Normal View History

2022-11-03 16:01:53 -07:00
using Content.Client.GameTicking.Managers;
2021-06-09 22:19:39 +02:00
using Content.Client.Lobby;
using Content.Shared.CCVar;
using Content.Shared.GameTicking;
using JetBrains.Annotations;
2021-06-09 22:19:39 +02:00
using Robust.Client;
using Robust.Client.State;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Configuration;
2021-06-09 22:19:39 +02:00
using Robust.Shared.Player;
2022-11-03 16:01:53 -07:00
namespace Content.Client.Audio;
[UsedImplicitly]
public sealed class BackgroundAudioSystem : EntitySystem
{
2023-12-11 21:26:55 +11:00
/*
* TODO: Nuke this system and merge into contentaudiosystem
*/
2022-11-03 16:01:53 -07:00
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly IBaseClient _client = default!;
[Dependency] private readonly IConfigurationManager _configManager = default!;
[Dependency] private readonly ClientGameTicker _gameTicker = default!;
[Dependency] private readonly IStateManager _stateManager = default!;
2022-11-04 00:15:37 +00:00
2022-11-03 16:01:53 -07:00
private readonly AudioParams _lobbyParams = new(-5f, 1, "Master", 0, 0, 0, true, 0f);
public EntityUid? LobbyMusicStream;
public EntityUid? LobbyRoundRestartAudioStream;
2022-11-03 16:01:53 -07:00
public override void Initialize()
{
2022-11-03 16:01:53 -07:00
base.Initialize();
2022-11-03 16:01:53 -07:00
_configManager.OnValueChanged(CCVars.LobbyMusicEnabled, LobbyMusicCVarChanged);
2022-11-04 00:15:37 +00:00
_configManager.OnValueChanged(CCVars.LobbyMusicVolume, LobbyMusicVolumeCVarChanged);
2022-11-03 16:01:53 -07:00
_stateManager.OnStateChanged += StateManagerOnStateChanged;
2022-11-03 16:01:53 -07:00
_client.PlayerLeaveServer += OnLeave;
_gameTicker.LobbySongUpdated += LobbySongUpdated;
SubscribeNetworkEvent<RoundRestartCleanupEvent>(PlayRestartSound);
2022-11-03 16:01:53 -07:00
}
2022-08-22 05:05:43 +10:00
2022-11-03 16:01:53 -07:00
public override void Shutdown()
{
base.Shutdown();
2022-08-22 05:05:43 +10:00
2022-11-03 16:01:53 -07:00
_configManager.UnsubValueChanged(CCVars.LobbyMusicEnabled, LobbyMusicCVarChanged);
2022-11-04 00:15:37 +00:00
_configManager.UnsubValueChanged(CCVars.LobbyMusicVolume, LobbyMusicVolumeCVarChanged);
2022-11-03 16:01:53 -07:00
_stateManager.OnStateChanged -= StateManagerOnStateChanged;
2022-11-03 16:01:53 -07:00
_client.PlayerLeaveServer -= OnLeave;
_gameTicker.LobbySongUpdated -= LobbySongUpdated;
2022-11-03 16:01:53 -07:00
EndLobbyMusic();
}
2022-11-03 16:01:53 -07:00
private void StateManagerOnStateChanged(StateChangedEventArgs args)
{
switch (args.NewState)
{
2022-11-03 16:01:53 -07:00
case LobbyState:
StartLobbyMusic();
2022-11-03 16:01:53 -07:00
break;
default:
EndLobbyMusic();
break;
}
2022-11-03 16:01:53 -07:00
}
private void OnLeave(object? sender, PlayerEventArgs args)
{
EndLobbyMusic();
}
2022-11-04 00:15:37 +00:00
private void LobbyMusicVolumeCVarChanged(float volume)
{
if (_stateManager.CurrentState is LobbyState)
{
RestartLobbyMusic();
}
}
2022-11-03 16:01:53 -07:00
private void LobbyMusicCVarChanged(bool musicEnabled)
{
if (!musicEnabled)
{
2022-11-03 16:01:53 -07:00
EndLobbyMusic();
}
2022-11-03 16:01:53 -07:00
else if (_stateManager.CurrentState is LobbyState)
{
2022-11-03 16:01:53 -07:00
StartLobbyMusic();
}
2022-11-03 16:01:53 -07:00
else
{
2022-11-03 16:01:53 -07:00
EndLobbyMusic();
}
2022-11-03 16:01:53 -07:00
}
private void LobbySongUpdated()
2022-11-03 16:01:53 -07:00
{
RestartLobbyMusic();
2022-11-03 16:01:53 -07:00
}
2022-05-13 10:13:07 +10:00
2022-11-03 16:01:53 -07:00
public void RestartLobbyMusic()
{
EndLobbyMusic();
StartLobbyMusic();
}
2022-10-04 14:24:19 +11:00
2022-11-03 16:01:53 -07:00
public void StartLobbyMusic()
{
if (LobbyMusicStream != null || !_configManager.GetCVar(CCVars.LobbyMusicEnabled))
2022-11-03 16:01:53 -07:00
return;
2022-11-03 16:01:53 -07:00
var file = _gameTicker.LobbySong;
if (file == null) // We have not received the lobby song yet.
{
2022-11-03 16:01:53 -07:00
return;
}
2022-11-03 16:01:53 -07:00
LobbyMusicStream = _audio.PlayGlobal(
file,
Filter.Local(),
false,
_lobbyParams.WithVolume(_lobbyParams.Volume + SharedAudioSystem.GainToVolume(_configManager.GetCVar(CCVars.LobbyMusicVolume))))?.Entity;
2022-11-03 16:01:53 -07:00
}
private void EndLobbyMusic()
{
LobbyMusicStream = _audio.Stop(LobbyMusicStream);
}
private void PlayRestartSound(RoundRestartCleanupEvent ev)
{
if (!_configManager.GetCVar(CCVars.LobbyMusicEnabled))
return;
var file = _gameTicker.RestartSound;
if (string.IsNullOrEmpty(file))
{
return;
}
LobbyRoundRestartAudioStream = _audio.PlayGlobal(
file,
Filter.Local(),
false,
_lobbyParams.WithVolume(_lobbyParams.Volume + SharedAudioSystem.GainToVolume(_configManager.GetCVar(CCVars.LobbyMusicVolume)))
)?.Entity;
}
2022-11-04 00:15:37 +00:00
}