@@ -1,23 +1,22 @@
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Shared.Sound.Components
|
||||
namespace Content.Shared.Sound.Components;
|
||||
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on ActivateInWorld
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class EmitSoundOnActivateComponent : BaseEmitSoundComponent
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on ActivateInWorld
|
||||
/// Whether or not to mark an interaction as handled after playing the sound. Useful if this component is
|
||||
/// used to play sound for some other component with activation functionality.
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class EmitSoundOnActivateComponent : BaseEmitSoundComponent
|
||||
{
|
||||
/// <summary>
|
||||
/// Whether or not to mark an interaction as handled after playing the sound. Useful if this component is
|
||||
/// used to play sound for some other component with activation functionality.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If false, you should be confident that the interaction will also be handled by some other system, as
|
||||
/// otherwise this might enable sound spamming, as use-delays are only initiated if the interaction was
|
||||
/// handled.
|
||||
/// </remarks>
|
||||
[DataField("handle")]
|
||||
public bool Handle = true;
|
||||
}
|
||||
/// <remarks>
|
||||
/// If false, you should be confident that the interaction will also be handled by some other system, as
|
||||
/// otherwise this might enable sound spamming, as use-delays are only initiated if the interaction was
|
||||
/// handled.
|
||||
/// </remarks>
|
||||
[DataField("handle")]
|
||||
public bool Handle = true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
|
||||
|
||||
namespace Content.Shared.Sound.Components;
|
||||
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class EmitSoundOnCollideComponent : BaseEmitSoundComponent
|
||||
{
|
||||
public static readonly TimeSpan CollideCooldown = TimeSpan.FromSeconds(0.2);
|
||||
|
||||
/// <summary>
|
||||
/// Minimum velocity required for the sound to play.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("minVelocity")]
|
||||
public float MinimumVelocity = 0.25f;
|
||||
|
||||
/// <summary>
|
||||
/// To avoid sound spam add a cooldown to it.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("nextSound", customTypeSerializer:typeof(TimeOffsetSerializer))]
|
||||
public TimeSpan NextSound;
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Shared.Sound.Components
|
||||
namespace Content.Shared.Sound.Components;
|
||||
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on entity drop
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class EmitSoundOnDropComponent : BaseEmitSoundComponent
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on entity drop
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class EmitSoundOnDropComponent : BaseEmitSoundComponent
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Shared.Sound.Components
|
||||
namespace Content.Shared.Sound.Components;
|
||||
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on LandEvent
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class EmitSoundOnLandComponent : BaseEmitSoundComponent
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on LandEvent
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class EmitSoundOnLandComponent : BaseEmitSoundComponent
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Shared.Sound.Components
|
||||
namespace Content.Shared.Sound.Components;
|
||||
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on entity pickup
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class EmitSoundOnPickupComponent : BaseEmitSoundComponent
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on entity pickup
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class EmitSoundOnPickupComponent : BaseEmitSoundComponent
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Shared.Sound.Components
|
||||
namespace Content.Shared.Sound.Components;
|
||||
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on ThrowEvent
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class EmitSoundOnThrowComponent : BaseEmitSoundComponent
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on ThrowEvent
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class EmitSoundOnThrowComponent : BaseEmitSoundComponent
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,22 @@
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Shared.Sound.Components
|
||||
namespace Content.Shared.Sound.Components;
|
||||
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on UseInHand
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class EmitSoundOnUseComponent : BaseEmitSoundComponent
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on UseInHand
|
||||
/// Whether or not to mark an interaction as handled after playing the sound. Useful if this component is
|
||||
/// used to play sound for some other component with on-use functionality
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class EmitSoundOnUseComponent : BaseEmitSoundComponent
|
||||
{
|
||||
/// <summary>
|
||||
/// Whether or not to mark an interaction as handled after playing the sound. Useful if this component is
|
||||
/// used to play sound for some other component with on-use functionality
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If false, you should be confident that the interaction will also be handled by some other system, as
|
||||
/// otherwise this might enable sound spamming, as use-delays are only initiated if the interaction was
|
||||
/// handled.
|
||||
/// </remarks>
|
||||
[DataField("handle")]
|
||||
public bool Handle = true;
|
||||
}
|
||||
/// <remarks>
|
||||
/// If false, you should be confident that the interaction will also be handled by some other system, as
|
||||
/// otherwise this might enable sound spamming, as use-delays are only initiated if the interaction was
|
||||
/// handled.
|
||||
/// </remarks>
|
||||
[DataField("handle")]
|
||||
public bool Handle = true;
|
||||
}
|
||||
|
||||
@@ -8,104 +8,122 @@ using Content.Shared.Throwing;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.Physics.Components;
|
||||
using Robust.Shared.Physics.Events;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Shared.Sound
|
||||
namespace Content.Shared.Sound;
|
||||
|
||||
/// <summary>
|
||||
/// Will play a sound on various events if the affected entity has a component derived from BaseEmitSoundComponent
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public abstract class SharedEmitSoundSystem : EntitySystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Will play a sound on various events if the affected entity has a component derived from BaseEmitSoundComponent
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public abstract class SharedEmitSoundSystem : EntitySystem
|
||||
[Dependency] private readonly IGameTiming _timing = default!;
|
||||
[Dependency] private readonly INetManager _netMan = default!;
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly ITileDefinitionManager _tileDefMan = default!;
|
||||
[Dependency] protected readonly IRobustRandom Random = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
|
||||
[Dependency] protected readonly SharedPopupSystem Popup = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
[Dependency] private readonly INetManager _netMan = default!;
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly ITileDefinitionManager _tileDefMan = default!;
|
||||
[Dependency] protected readonly IRobustRandom Random = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
|
||||
[Dependency] protected readonly SharedPopupSystem Popup = default!;
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<EmitSoundOnSpawnComponent, ComponentInit>(OnEmitSpawnOnInit);
|
||||
SubscribeLocalEvent<EmitSoundOnLandComponent, LandEvent>(OnEmitSoundOnLand);
|
||||
SubscribeLocalEvent<EmitSoundOnUseComponent, UseInHandEvent>(OnEmitSoundOnUseInHand);
|
||||
SubscribeLocalEvent<EmitSoundOnThrowComponent, ThrownEvent>(OnEmitSoundOnThrown);
|
||||
SubscribeLocalEvent<EmitSoundOnActivateComponent, ActivateInWorldEvent>(OnEmitSoundOnActivateInWorld);
|
||||
SubscribeLocalEvent<EmitSoundOnPickupComponent, GotEquippedHandEvent>(OnEmitSoundOnPickup);
|
||||
SubscribeLocalEvent<EmitSoundOnDropComponent, DroppedEvent>(OnEmitSoundOnDrop);
|
||||
SubscribeLocalEvent<EmitSoundOnCollideComponent, StartCollideEvent>(OnEmitSoundOnCollide);
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
private void OnEmitSpawnOnInit(EntityUid uid, EmitSoundOnSpawnComponent component, ComponentInit args)
|
||||
{
|
||||
TryEmitSound(uid, component, predict: false);
|
||||
}
|
||||
|
||||
private void OnEmitSoundOnLand(EntityUid uid, BaseEmitSoundComponent component, ref LandEvent args)
|
||||
{
|
||||
if (!TryComp<TransformComponent>(uid, out var xform) ||
|
||||
!_mapManager.TryGetGrid(xform.GridUid, out var grid))
|
||||
return;
|
||||
|
||||
var tile = grid.GetTileRef(xform.Coordinates);
|
||||
|
||||
// Handle maps being grids (we'll still emit the sound).
|
||||
if (xform.GridUid != xform.MapUid && tile.IsSpace(_tileDefMan))
|
||||
return;
|
||||
|
||||
// hand throwing not predicted sadly
|
||||
TryEmitSound(uid, component, args.User, false);
|
||||
}
|
||||
|
||||
private void OnEmitSoundOnUseInHand(EntityUid uid, EmitSoundOnUseComponent component, UseInHandEvent args)
|
||||
{
|
||||
// Intentionally not checking whether the interaction has already been handled.
|
||||
TryEmitSound(uid, component, args.User);
|
||||
|
||||
if (component.Handle)
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnEmitSoundOnThrown(EntityUid uid, BaseEmitSoundComponent component, ThrownEvent args)
|
||||
{
|
||||
TryEmitSound(uid, component, args.User, false);
|
||||
}
|
||||
|
||||
private void OnEmitSoundOnActivateInWorld(EntityUid uid, EmitSoundOnActivateComponent component, ActivateInWorldEvent args)
|
||||
{
|
||||
// Intentionally not checking whether the interaction has already been handled.
|
||||
TryEmitSound(uid, component, args.User);
|
||||
|
||||
if (component.Handle)
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnEmitSoundOnPickup(EntityUid uid, EmitSoundOnPickupComponent component, GotEquippedHandEvent args)
|
||||
{
|
||||
TryEmitSound(uid, component, args.User);
|
||||
}
|
||||
|
||||
private void OnEmitSoundOnDrop(EntityUid uid, EmitSoundOnDropComponent component, DroppedEvent args)
|
||||
{
|
||||
TryEmitSound(uid, component, args.User);
|
||||
}
|
||||
|
||||
protected void TryEmitSound(EntityUid uid, BaseEmitSoundComponent component, EntityUid? user=null, bool predict=true)
|
||||
{
|
||||
if (component.Sound == null)
|
||||
return;
|
||||
|
||||
if (predict)
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<EmitSoundOnSpawnComponent, ComponentInit>(HandleEmitSpawnOnInit);
|
||||
SubscribeLocalEvent<EmitSoundOnLandComponent, LandEvent>(OnEmitSoundOnLand);
|
||||
SubscribeLocalEvent<EmitSoundOnUseComponent, UseInHandEvent>(HandleEmitSoundOnUseInHand);
|
||||
SubscribeLocalEvent<EmitSoundOnThrowComponent, ThrownEvent>(HandleEmitSoundOnThrown);
|
||||
SubscribeLocalEvent<EmitSoundOnActivateComponent, ActivateInWorldEvent>(HandleEmitSoundOnActivateInWorld);
|
||||
SubscribeLocalEvent<EmitSoundOnPickupComponent, GotEquippedHandEvent>(HandleEmitSoundOnPickup);
|
||||
SubscribeLocalEvent<EmitSoundOnDropComponent, DroppedEvent>(HandleEmitSoundOnDrop);
|
||||
_audioSystem.PlayPredicted(component.Sound, uid, user);
|
||||
}
|
||||
|
||||
private void HandleEmitSpawnOnInit(EntityUid uid, EmitSoundOnSpawnComponent component, ComponentInit args)
|
||||
else if (_netMan.IsServer)
|
||||
{
|
||||
TryEmitSound(component, predict: false);
|
||||
}
|
||||
|
||||
private void OnEmitSoundOnLand(EntityUid uid, BaseEmitSoundComponent component, ref LandEvent args)
|
||||
{
|
||||
if (!TryComp<TransformComponent>(uid, out var xform) ||
|
||||
!_mapManager.TryGetGrid(xform.GridUid, out var grid))
|
||||
return;
|
||||
|
||||
var tile = grid.GetTileRef(xform.Coordinates);
|
||||
|
||||
// Handle maps being grids (we'll still emit the sound).
|
||||
if (xform.GridUid != xform.MapUid && tile.IsSpace(_tileDefMan))
|
||||
return;
|
||||
|
||||
// hand throwing not predicted sadly
|
||||
TryEmitSound(component, args.User, false);
|
||||
}
|
||||
|
||||
private void HandleEmitSoundOnUseInHand(EntityUid eUI, EmitSoundOnUseComponent component, UseInHandEvent args)
|
||||
{
|
||||
// Intentionally not checking whether the interaction has already been handled.
|
||||
TryEmitSound(component, args.User);
|
||||
|
||||
if (component.Handle)
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void HandleEmitSoundOnThrown(EntityUid eUI, BaseEmitSoundComponent component, ThrownEvent args)
|
||||
{
|
||||
TryEmitSound(component, args.User, false);
|
||||
}
|
||||
|
||||
private void HandleEmitSoundOnActivateInWorld(EntityUid eUI, EmitSoundOnActivateComponent component, ActivateInWorldEvent args)
|
||||
{
|
||||
// Intentionally not checking whether the interaction has already been handled.
|
||||
TryEmitSound(component, args.User);
|
||||
|
||||
if (component.Handle)
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void HandleEmitSoundOnPickup(EntityUid uid, EmitSoundOnPickupComponent component, GotEquippedHandEvent args)
|
||||
{
|
||||
TryEmitSound(component, args.User);
|
||||
}
|
||||
|
||||
private void HandleEmitSoundOnDrop(EntityUid uid, EmitSoundOnDropComponent component, DroppedEvent args)
|
||||
{
|
||||
TryEmitSound(component, args.User);
|
||||
}
|
||||
|
||||
protected void TryEmitSound(BaseEmitSoundComponent component, EntityUid? user=null, bool predict=true)
|
||||
{
|
||||
if (component.Sound == null)
|
||||
return;
|
||||
|
||||
if (predict)
|
||||
{
|
||||
_audioSystem.PlayPredicted(component.Sound, component.Owner, user);
|
||||
}
|
||||
else if (_netMan.IsServer)
|
||||
{
|
||||
// don't predict sounds that client couldn't have played already
|
||||
_audioSystem.PlayPvs(component.Sound, component.Owner);
|
||||
}
|
||||
// don't predict sounds that client couldn't have played already
|
||||
_audioSystem.PlayPvs(component.Sound, uid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEmitSoundOnCollide(EntityUid uid, EmitSoundOnCollideComponent component, ref StartCollideEvent args)
|
||||
{
|
||||
if (!args.OurFixture.Hard ||
|
||||
!args.OtherFixture.Hard ||
|
||||
!TryComp<PhysicsComponent>(uid, out var physics) ||
|
||||
physics.LinearVelocity.Length < component.MinimumVelocity ||
|
||||
_timing.CurTime < component.NextSound)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
component.NextSound = _timing.CurTime + EmitSoundOnCollideComponent.CollideCooldown;
|
||||
TryEmitSound(uid, component, predict: false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user