Store ambient sound entities on a component tree. (#13110)

This commit is contained in:
Leon Friedrich
2022-12-30 17:10:14 +13:00
committed by GitHub
parent 860ff9ee38
commit ae58bb1f1b
6 changed files with 145 additions and 63 deletions

View File

@@ -1,33 +1,44 @@
using Robust.Shared.Audio;
using Robust.Shared.ComponentTrees;
using Robust.Shared.GameStates;
using Robust.Shared.Physics;
using Robust.Shared.Serialization;
namespace Content.Shared.Audio
{
[RegisterComponent]
[NetworkedComponent]
public sealed class AmbientSoundComponent : Component
[Access(typeof(SharedAmbientSoundSystem))]
public sealed class AmbientSoundComponent : Component, IComponentTreeEntry<AmbientSoundComponent>
{
[ViewVariables(VVAccess.ReadWrite)]
[DataField("enabled")]
[ViewVariables(VVAccess.ReadWrite)] // only for map editing
public bool Enabled { get; set; } = true;
[DataField("sound", required: true), ViewVariables(VVAccess.ReadWrite)]
[DataField("sound", required: true), ViewVariables(VVAccess.ReadWrite)] // only for map editing
public SoundSpecifier Sound = default!;
/// <summary>
/// How far away this ambient sound can potentially be heard.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[ViewVariables(VVAccess.ReadWrite)] // only for map editing
[DataField("range")]
public float Range = 2f;
/// <summary>
/// Applies this volume to the sound being played.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[ViewVariables(VVAccess.ReadWrite)] // only for map editing
[DataField("volume")]
public float Volume = -10f;
public EntityUid? TreeUid { get; set; }
public DynamicTree<ComponentTreeEntry<AmbientSoundComponent>>? Tree { get; set; }
public bool AddToTree => Enabled;
public bool TreeUpdateQueued { get; set; }
}
[Serializable, NetSerializable]

View File

@@ -11,24 +11,46 @@ namespace Content.Shared.Audio
SubscribeLocalEvent<AmbientSoundComponent, ComponentHandleState>(HandleCompState);
}
public void SetAmbience(EntityUid uid, bool value)
public virtual void SetAmbience(EntityUid uid, bool value, AmbientSoundComponent? ambience = null)
{
// Reason I didn't make this eventbus for the callers is because it seemed a bit silly
// trying to account for damageable + powered + toggle, plus we can't just check if it's powered.
// So we'll just call it directly for whatever.
if (!EntityManager.TryGetComponent<AmbientSoundComponent>(uid, out var ambience) ||
ambience.Enabled == value) return;
if (!Resolve(uid, ref ambience, false) || ambience.Enabled == value)
return;
ambience.Enabled = value;
QueueUpdate(uid, ambience);
Dirty(ambience);
}
public virtual void SetRange(EntityUid uid, float value, AmbientSoundComponent? ambience = null)
{
if (!Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Range, value))
return;
ambience.Range = value;
QueueUpdate(uid, ambience);
Dirty(ambience);
}
protected virtual void QueueUpdate(EntityUid uid, AmbientSoundComponent ambience)
{
// client side tree
}
public virtual void SetVolume(EntityUid uid, float value, AmbientSoundComponent? ambience = null)
{
if (!Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Volume, value))
return;
ambience.Volume = value;
Dirty(ambience);
}
private void HandleCompState(EntityUid uid, AmbientSoundComponent component, ref ComponentHandleState args)
{
if (args.Current is not AmbientSoundComponentState state) return;
component.Enabled = state.Enabled;
component.Range = state.Range;
component.Volume = state.Volume;
SetAmbience(uid, state.Enabled, component);
SetRange(uid, state.Range, component);
SetVolume(uid, state.Volume, component);
}
private void GetCompState(EntityUid uid, AmbientSoundComponent component, ref ComponentGetState args)