Files
tbd-station-14/Content.Client/GameObjects/EntitySystems/BackgroundAudioSystem.cs
ike709 31f1cf9936 Adds lobby music (#3620)
* Adds lobby music

* Add UI toggle for lobby music

* Pick the song serverside so everyone gets the same song

* Add more songs

* Fixes

* Catch-all end lobby music

* Rename it

* Catchall ambience cvar change

* Wait until we receive the lobby song to start playing one

* Fix toggling ready status resetting the song

* Comment

* Expend the last of my sanity fixing latejoin lobby music

* Update Content.Client/GameObjects/EntitySystems/BackgroundAudioSystem.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Update Content.Client/GameObjects/EntitySystems/BackgroundAudioSystem.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Update Content.Client/GameObjects/EntitySystems/BackgroundAudioSystem.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Move the var

Co-authored-by: ike709 <sparebytes@protonmail.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2021-03-12 23:40:28 +01:00

187 lines
5.6 KiB
C#

#nullable enable
using Content.Client.Interfaces;
using Content.Shared.Audio;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Content.Shared;
using Robust.Shared.Audio;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Client;
using Robust.Client.State;
using Content.Client.State;
namespace Content.Client.GameObjects.EntitySystems
{
[UsedImplicitly]
public class BackgroundAudioSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly IConfigurationManager _configManager = default!;
[Dependency] private readonly IStateManager _stateManager = default!;
[Dependency] private readonly IBaseClient _client = default!;
[Dependency] private readonly IClientGameTicker _clientGameTicker = default!;
private AudioSystem _audioSystem = default!;
private SoundCollectionPrototype _ambientCollection = default!;
private AudioParams _ambientParams = new(-10f, 1, "Master", 0, 0, AudioMixTarget.Stereo, true, 0f);
private AudioParams _lobbyParams = new(5f, 1, "Master", 0, 0, AudioMixTarget.Stereo, true, 0f);
private IPlayingAudioStream? _ambientStream;
private IPlayingAudioStream? _lobbyStream;
public override void Initialize()
{
base.Initialize();
_audioSystem = Get<AudioSystem>();
_ambientCollection = _prototypeManager.Index<SoundCollectionPrototype>("AmbienceBase");
_configManager.OnValueChanged(CCVars.AmbienceBasicEnabled, AmbienceCVarChanged);
_configManager.OnValueChanged(CCVars.LobbyMusicEnabled, LobbyMusicCVarChanged);
_stateManager.OnStateChanged += StateManagerOnStateChanged;
_client.PlayerJoinedServer += OnJoin;
_client.PlayerLeaveServer += OnLeave;
_clientGameTicker.LobbyStatusUpdated += LobbySongReceived;
}
public override void Shutdown()
{
base.Shutdown();
_stateManager.OnStateChanged -= StateManagerOnStateChanged;
_client.PlayerJoinedServer -= OnJoin;
_client.PlayerLeaveServer -= OnLeave;
_clientGameTicker.LobbyStatusUpdated -= LobbySongReceived;
EndAmbience();
EndLobbyMusic();
}
private void StateManagerOnStateChanged(StateChangedEventArgs args)
{
EndAmbience();
EndLobbyMusic();
if (args.NewState is LobbyState && _configManager.GetCVar(CCVars.LobbyMusicEnabled))
{
StartLobbyMusic();
}
else if (args.NewState is GameScreen && _configManager.GetCVar(CCVars.AmbienceBasicEnabled))
{
StartAmbience();
}
}
private void OnJoin(object? sender, PlayerEventArgs args)
{
if (_stateManager.CurrentState is LobbyState)
{
EndAmbience();
if (_configManager.GetCVar(CCVars.LobbyMusicEnabled))
{
StartLobbyMusic();
}
}
else
{
EndLobbyMusic();
if (_configManager.GetCVar(CCVars.AmbienceBasicEnabled))
{
StartAmbience();
}
}
}
private void OnLeave(object? sender, PlayerEventArgs args)
{
EndAmbience();
EndLobbyMusic();
}
private void AmbienceCVarChanged(bool ambienceEnabled)
{
if (!ambienceEnabled)
{
EndAmbience();
}
else if (_stateManager.CurrentState is GameScreen)
{
StartAmbience();
}
else
{
EndAmbience();
}
}
private void StartAmbience()
{
EndAmbience();
var file = _robustRandom.Pick(_ambientCollection.PickFiles);
_ambientStream = _audioSystem.Play(file, _ambientParams);
}
private void EndAmbience()
{
_ambientStream?.Stop();
_ambientStream = null;
}
private void LobbyMusicCVarChanged(bool musicEnabled)
{
if (!musicEnabled)
{
EndLobbyMusic();
}
else if (_stateManager.CurrentState is LobbyState)
{
StartLobbyMusic();
}
else
{
EndLobbyMusic();
}
}
private void LobbySongReceived()
{
if (_lobbyStream != null) //Toggling Ready status fires this method. This check ensures we only start the lobby music if it's not playing.
{
return;
}
if (_stateManager.CurrentState is LobbyState && _configManager.GetCVar(CCVars.LobbyMusicEnabled))
{
StartLobbyMusic();
}
}
private void StartLobbyMusic()
{
EndLobbyMusic();
var file = _clientGameTicker.LobbySong;
if (file == null) // We have not received the lobby song yet.
{
return;
}
_lobbyStream = _audioSystem.Play(file, _lobbyParams);
}
private void EndLobbyMusic()
{
_lobbyStream?.Stop();
_lobbyStream = null;
}
}
}