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

132 lines
3.7 KiB
C#
Raw Normal View History

2022-08-12 12:53:28 +10:00
using Content.Shared.Audio;
2023-05-29 10:44:11 +10:00
using Robust.Client.GameObjects;
2023-10-29 14:58:23 +11:00
using Robust.Shared.Audio;
using AudioComponent = Robust.Shared.Audio.Components.AudioComponent;
2022-08-12 12:53:28 +10:00
namespace Content.Client.Audio;
2023-05-29 10:44:11 +10:00
public sealed partial class ContentAudioSystem : SharedContentAudioSystem
2022-08-12 12:53:28 +10:00
{
2023-05-29 10:44:11 +10:00
// Need how much volume to change per tick and just remove it when it drops below "0"
2023-10-29 14:58:23 +11:00
private readonly Dictionary<EntityUid, float> _fadingOut = new();
2022-08-12 12:53:28 +10:00
2023-05-29 10:44:11 +10:00
// Need volume change per tick + target volume.
2023-10-29 14:58:23 +11:00
private readonly Dictionary<EntityUid, (float VolumeChange, float TargetVolume)> _fadingIn = new();
2023-05-29 10:44:11 +10:00
2023-10-29 14:58:23 +11:00
private readonly List<EntityUid> _fadeToRemove = new();
2023-05-29 10:44:11 +10:00
private const float MinVolume = -32f;
private const float DefaultDuration = 2f;
public override void Initialize()
{
base.Initialize();
UpdatesOutsidePrediction = true;
InitializeAmbientMusic();
}
public override void Shutdown()
{
base.Shutdown();
ShutdownAmbientMusic();
}
public override void Update(float frameTime)
{
base.Update(frameTime);
if (!_timing.IsFirstTimePredicted)
return;
UpdateAmbientMusic();
UpdateFades(frameTime);
}
#region Fades
2023-10-29 14:58:23 +11:00
public void FadeOut(EntityUid? stream, AudioComponent? component = null, float duration = DefaultDuration)
2023-05-29 10:44:11 +10:00
{
2023-10-29 14:58:23 +11:00
if (stream == null || duration <= 0f || !Resolve(stream.Value, ref component))
2023-05-29 10:44:11 +10:00
return;
// Just in case
// TODO: Maybe handle the removals by making it seamless?
2023-10-29 14:58:23 +11:00
_fadingIn.Remove(stream.Value);
var diff = component.Volume - MinVolume;
_fadingOut.Add(stream.Value, diff / duration);
2023-05-29 10:44:11 +10:00
}
2023-10-29 14:58:23 +11:00
public void FadeIn(EntityUid? stream, AudioComponent? component = null, float duration = DefaultDuration)
2023-05-29 10:44:11 +10:00
{
2023-10-29 14:58:23 +11:00
if (stream == null || duration <= 0f || !Resolve(stream.Value, ref component) || component.Volume < MinVolume)
2023-05-29 10:44:11 +10:00
return;
2023-10-29 14:58:23 +11:00
_fadingOut.Remove(stream.Value);
var curVolume = component.Volume;
2023-05-29 10:44:11 +10:00
var change = (curVolume - MinVolume) / duration;
2023-10-29 14:58:23 +11:00
_fadingIn.Add(stream.Value, (change, component.Volume));
component.Volume = MinVolume;
2023-05-29 10:44:11 +10:00
}
private void UpdateFades(float frameTime)
{
_fadeToRemove.Clear();
foreach (var (stream, change) in _fadingOut)
{
2023-10-29 14:58:23 +11:00
if (!TryComp(stream, out AudioComponent? component))
2023-05-29 10:44:11 +10:00
{
_fadeToRemove.Add(stream);
continue;
}
2023-10-29 14:58:23 +11:00
var volume = component.Volume - change * frameTime;
component.Volume = MathF.Max(MinVolume, volume);
2023-05-29 10:44:11 +10:00
2023-10-29 14:58:23 +11:00
if (component.Volume.Equals(MinVolume))
2023-05-29 10:44:11 +10:00
{
2023-10-29 14:58:23 +11:00
_audio.Stop(stream);
2023-05-29 10:44:11 +10:00
_fadeToRemove.Add(stream);
}
}
foreach (var stream in _fadeToRemove)
{
_fadingOut.Remove(stream);
}
_fadeToRemove.Clear();
foreach (var (stream, (change, target)) in _fadingIn)
{
// Cancelled elsewhere
2023-10-29 14:58:23 +11:00
if (!TryComp(stream, out AudioComponent? component))
2023-05-29 10:44:11 +10:00
{
_fadeToRemove.Add(stream);
continue;
}
2023-10-29 14:58:23 +11:00
var volume = component.Volume + change * frameTime;
component.Volume = MathF.Min(target, volume);
2023-05-29 10:44:11 +10:00
2023-10-29 14:58:23 +11:00
if (component.Volume.Equals(target))
2023-05-29 10:44:11 +10:00
{
_fadeToRemove.Add(stream);
}
}
foreach (var stream in _fadeToRemove)
{
_fadingIn.Remove(stream);
}
}
#endregion
2022-08-12 12:53:28 +10:00
}
2023-06-27 21:28:51 +10:00
/// <summary>
/// Raised whenever ambient music tries to play.
/// </summary>
[ByRefEvent]
public record struct PlayAmbientMusicEvent(bool Cancelled = false);