Ambient sound system (#4552)
* Ambient sound system Client-side system that plays audio from nearby objects that are randomly sampled. * Decent * Tweaks * Tweaks * Comment this out for now * reduce VM sound * Fix rolloff * Fixes * Volume tweak
This commit is contained in:
46
Content.Shared/Audio/AmbientSoundComponent.cs
Normal file
46
Content.Shared/Audio/AmbientSoundComponent.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using Content.Shared.Sound;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Shared.Audio
|
||||
{
|
||||
[RegisterComponent]
|
||||
[NetworkedComponent]
|
||||
public sealed class AmbientSoundComponent : Component
|
||||
{
|
||||
public override string Name => "AmbientSound";
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("enabled")]
|
||||
public bool Enabled { get; set; } = true;
|
||||
|
||||
[DataField("sound")]
|
||||
public SoundSpecifier Sound = default!;
|
||||
|
||||
/// <summary>
|
||||
/// How far away this ambient sound can potentially be heard.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("range")]
|
||||
public float Range = 2f;
|
||||
|
||||
/// <summary>
|
||||
/// Applies this volume to the sound being played.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("volume")]
|
||||
public float Volume = -10f;
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class AmbientSoundComponentState : ComponentState
|
||||
{
|
||||
public bool Enabled { get; init; }
|
||||
public float Range { get; init; }
|
||||
public float Volume { get; init; }
|
||||
}
|
||||
}
|
||||
45
Content.Shared/Audio/SharedAmbientSoundSystem.cs
Normal file
45
Content.Shared/Audio/SharedAmbientSoundSystem.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Shared.Audio
|
||||
{
|
||||
public abstract class SharedAmbientSoundSystem : EntitySystem
|
||||
{
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<AmbientSoundComponent, ComponentGetState>(GetCompState);
|
||||
SubscribeLocalEvent<AmbientSoundComponent, ComponentHandleState>(HandleCompState);
|
||||
}
|
||||
|
||||
public void SetAmbience(EntityUid uid, bool value)
|
||||
{
|
||||
// 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 (!ComponentManager.TryGetComponent<AmbientSoundComponent>(uid, out var ambience) ||
|
||||
ambience.Enabled == value) return;
|
||||
|
||||
ambience.Enabled = value;
|
||||
ambience.Dirty();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private void GetCompState(EntityUid uid, AmbientSoundComponent component, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new AmbientSoundComponentState
|
||||
{
|
||||
Enabled = component.Enabled,
|
||||
Range = component.Range,
|
||||
Volume = component.Volume,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user