Event Music & Nuke Countdown Song (#8597)
Co-authored-by: ike709 <ike709@github.com> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
113
Content.Client/Audio/ClientGlobalSoundSystem.cs
Normal file
113
Content.Client/Audio/ClientGlobalSoundSystem.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.CCVar;
|
||||
using Content.Shared.GameTicking;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.Player;
|
||||
|
||||
namespace Content.Client.Audio;
|
||||
|
||||
public sealed class ClientGlobalSoundSystem : SharedGlobalSoundSystem
|
||||
{
|
||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||
|
||||
// Admin music
|
||||
private bool _adminAudioEnabled = true;
|
||||
private List<IPlayingAudioStream?> _adminAudio = new(1);
|
||||
|
||||
// Event sounds (e.g. nuke timer)
|
||||
private bool _eventAudioEnabled = true;
|
||||
private Dictionary<StationEventMusicType, IPlayingAudioStream?> _eventAudio = new(1);
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundRestart);
|
||||
SubscribeNetworkEvent<AdminSoundEvent>(PlayAdminSound);
|
||||
_cfg.OnValueChanged(CCVars.AdminSoundsEnabled, ToggleAdminSound, true);
|
||||
|
||||
SubscribeNetworkEvent<StationEventMusicEvent>(PlayStationEventMusic);
|
||||
SubscribeNetworkEvent<StopStationEventMusic>(StopStationEventMusic);
|
||||
_cfg.OnValueChanged(CCVars.EventMusicEnabled, ToggleStationEventMusic, true);
|
||||
|
||||
SubscribeNetworkEvent<GameGlobalSoundEvent>(PlayGameSound);
|
||||
}
|
||||
|
||||
private void OnRoundRestart(RoundRestartCleanupEvent ev)
|
||||
{
|
||||
ClearAudio();
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
ClearAudio();
|
||||
}
|
||||
|
||||
private void ClearAudio()
|
||||
{
|
||||
foreach (var stream in _adminAudio)
|
||||
{
|
||||
stream?.Stop();
|
||||
}
|
||||
_adminAudio.Clear();
|
||||
|
||||
foreach (var (_, stream) in _eventAudio)
|
||||
{
|
||||
stream?.Stop();
|
||||
}
|
||||
|
||||
_eventAudio.Clear();
|
||||
}
|
||||
|
||||
private void PlayAdminSound(AdminSoundEvent soundEvent)
|
||||
{
|
||||
if(!_adminAudioEnabled) return;
|
||||
|
||||
var stream = SoundSystem.Play(soundEvent.Filename, Filter.Local(), soundEvent.AudioParams);
|
||||
_adminAudio.Add(stream);
|
||||
}
|
||||
|
||||
private void PlayStationEventMusic(StationEventMusicEvent soundEvent)
|
||||
{
|
||||
// Either the cvar is disabled or it's already playing
|
||||
if(!_eventAudioEnabled || _eventAudio.ContainsKey(soundEvent.Type)) return;
|
||||
|
||||
var stream = SoundSystem.Play(soundEvent.Filename, Filter.Local(), soundEvent.AudioParams);
|
||||
_eventAudio.Add(soundEvent.Type, stream);
|
||||
}
|
||||
|
||||
private void PlayGameSound(GameGlobalSoundEvent soundEvent)
|
||||
{
|
||||
SoundSystem.Play(soundEvent.Filename, Filter.Local(), soundEvent.AudioParams);
|
||||
}
|
||||
|
||||
private void StopStationEventMusic(StopStationEventMusic soundEvent)
|
||||
{
|
||||
if (!_eventAudio.TryGetValue(soundEvent.Type, out var stream)) return;
|
||||
stream?.Stop();
|
||||
_eventAudio.Remove(soundEvent.Type);
|
||||
}
|
||||
|
||||
private void ToggleAdminSound(bool enabled)
|
||||
{
|
||||
_adminAudioEnabled = enabled;
|
||||
if (_adminAudioEnabled) return;
|
||||
foreach (var stream in _adminAudio)
|
||||
{
|
||||
stream?.Stop();
|
||||
}
|
||||
_adminAudio.Clear();
|
||||
}
|
||||
|
||||
private void ToggleStationEventMusic(bool enabled)
|
||||
{
|
||||
_eventAudioEnabled = enabled;
|
||||
if (_eventAudioEnabled) return;
|
||||
foreach (var stream in _eventAudio)
|
||||
{
|
||||
stream.Value?.Stop();
|
||||
}
|
||||
_eventAudio.Clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user