EmitSound prediction (#13282)
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
using Robust.Shared.Audio;
|
||||
|
||||
namespace Content.Server.Sound.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Base sound emitter which defines most of the data fields.
|
||||
/// Accepts both single sounds and sound collections.
|
||||
/// </summary>
|
||||
public abstract class BaseEmitSoundComponent : Component
|
||||
{
|
||||
public static readonly AudioParams DefaultParams = AudioParams.Default.WithVolume(-2f);
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("sound", required: true)]
|
||||
public SoundSpecifier? Sound;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
namespace Content.Server.Sound.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on ActivateInWorld
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace Content.Server.Sound.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on entity drop
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class EmitSoundOnDropComponent : BaseEmitSoundComponent
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace Content.Server.Sound.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on LandEvent
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class EmitSoundOnLandComponent : BaseEmitSoundComponent
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace Content.Server.Sound.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on entity pickup
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class EmitSoundOnPickupComponent : BaseEmitSoundComponent
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace Content.Server.Sound.Components;
|
||||
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on entity spawn.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class EmitSoundOnSpawnComponent : BaseEmitSoundComponent
|
||||
{
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Content.Server.Explosion.EntitySystems;
|
||||
using Content.Shared.Sound.Components;
|
||||
|
||||
namespace Content.Server.Sound.Components
|
||||
{
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using Content.Shared.Sound.Components;
|
||||
|
||||
namespace Content.Server.Sound.Components
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using Content.Shared.Sound.Components;
|
||||
|
||||
namespace Content.Server.Sound.Components
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -1,142 +1,53 @@
|
||||
using Content.Server.Explosion.EntitySystems;
|
||||
using Content.Server.Interaction.Components;
|
||||
using Content.Server.Explosion.EntitySystems;
|
||||
using Content.Server.Sound.Components;
|
||||
using Content.Server.Throwing;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Server.Popups;
|
||||
using Content.Shared.Hands;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Interaction.Events;
|
||||
using Content.Shared.Item;
|
||||
using Content.Shared.Maps;
|
||||
using Content.Shared.Throwing;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Player;
|
||||
using Content.Shared.Sound;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Sound
|
||||
namespace Content.Server.Sound;
|
||||
|
||||
public sealed class EmitSoundSystem : SharedEmitSoundSystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Will play a sound on various events if the affected entity has a component derived from BaseEmitSoundComponent
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public sealed class EmitSoundSystem : EntitySystem
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly ITileDefinitionManager _tileDefMan = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
|
||||
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
public override void Update(float frameTime)
|
||||
base.Update(frameTime);
|
||||
foreach (var soundSpammer in EntityQuery<SpamEmitSoundComponent>())
|
||||
{
|
||||
base.Update(frameTime);
|
||||
foreach (var soundSpammer in EntityQuery<SpamEmitSoundComponent>())
|
||||
if (!soundSpammer.Enabled)
|
||||
continue;
|
||||
|
||||
soundSpammer.Accumulator += frameTime;
|
||||
if (soundSpammer.Accumulator < soundSpammer.RollInterval)
|
||||
{
|
||||
if (!soundSpammer.Enabled)
|
||||
continue;
|
||||
continue;
|
||||
}
|
||||
soundSpammer.Accumulator -= soundSpammer.RollInterval;
|
||||
|
||||
soundSpammer.Accumulator += frameTime;
|
||||
if (soundSpammer.Accumulator < soundSpammer.RollInterval)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
soundSpammer.Accumulator -= soundSpammer.RollInterval;
|
||||
|
||||
if (_random.Prob(soundSpammer.PlayChance))
|
||||
{
|
||||
if (soundSpammer.PopUp != null)
|
||||
_popupSystem.PopupEntity(Loc.GetString(soundSpammer.PopUp), soundSpammer.Owner);
|
||||
TryEmitSound(soundSpammer);
|
||||
}
|
||||
if (Random.Prob(soundSpammer.PlayChance))
|
||||
{
|
||||
if (soundSpammer.PopUp != null)
|
||||
Popup.PopupEntity(Loc.GetString(soundSpammer.PopUp), soundSpammer.Owner);
|
||||
TryEmitSound(soundSpammer);
|
||||
}
|
||||
}
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<EmitSoundOnSpawnComponent, ComponentInit>(HandleEmitSpawnOnInit);
|
||||
SubscribeLocalEvent<EmitSoundOnLandComponent, LandEvent>(HandleEmitSoundOnLand);
|
||||
SubscribeLocalEvent<EmitSoundOnUseComponent, UseInHandEvent>(HandleEmitSoundOnUseInHand);
|
||||
SubscribeLocalEvent<EmitSoundOnThrowComponent, ThrownEvent>(HandleEmitSoundOnThrown);
|
||||
SubscribeLocalEvent<EmitSoundOnActivateComponent, ActivateInWorldEvent>(HandleEmitSoundOnActivateInWorld);
|
||||
SubscribeLocalEvent<EmitSoundOnTriggerComponent, TriggerEvent>(HandleEmitSoundOnTrigger);
|
||||
SubscribeLocalEvent<EmitSoundOnUIOpenComponent, AfterActivatableUIOpenEvent>(HandleEmitSoundOnUIOpen);
|
||||
SubscribeLocalEvent<EmitSoundOnPickupComponent, GotEquippedHandEvent>(HandleEmitSoundOnPickup);
|
||||
SubscribeLocalEvent<EmitSoundOnDropComponent, DroppedEvent>(HandleEmitSoundOnDrop);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleEmitSpawnOnInit(EntityUid uid, EmitSoundOnSpawnComponent component, ComponentInit args)
|
||||
{
|
||||
TryEmitSound(component);
|
||||
}
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
private void HandleEmitSoundOnTrigger(EntityUid uid, EmitSoundOnTriggerComponent component, TriggerEvent args)
|
||||
{
|
||||
TryEmitSound(component);
|
||||
args.Handled = true;
|
||||
}
|
||||
SubscribeLocalEvent<EmitSoundOnTriggerComponent, TriggerEvent>(HandleEmitSoundOnTrigger);
|
||||
SubscribeLocalEvent<EmitSoundOnUIOpenComponent, AfterActivatableUIOpenEvent>(HandleEmitSoundOnUIOpen);
|
||||
}
|
||||
|
||||
private void HandleEmitSoundOnLand(EntityUid uid, BaseEmitSoundComponent component, LandEvent arg)
|
||||
{
|
||||
if (!TryComp<TransformComponent>(uid, out var xform) ||
|
||||
!_mapManager.TryGetGrid(xform.GridUid, out var grid)) return;
|
||||
private void HandleEmitSoundOnUIOpen(EntityUid eUI, EmitSoundOnUIOpenComponent component, AfterActivatableUIOpenEvent args)
|
||||
{
|
||||
TryEmitSound(component, args.User);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
TryEmitSound(component);
|
||||
}
|
||||
|
||||
private void HandleEmitSoundOnUseInHand(EntityUid eUI, EmitSoundOnUseComponent component, UseInHandEvent arg)
|
||||
{
|
||||
// Intentionally not checking whether the interaction has already been handled.
|
||||
TryEmitSound(component);
|
||||
|
||||
if (component.Handle)
|
||||
arg.Handled = true;
|
||||
}
|
||||
|
||||
private void HandleEmitSoundOnThrown(EntityUid eUI, BaseEmitSoundComponent component, ThrownEvent arg)
|
||||
{
|
||||
TryEmitSound(component);
|
||||
}
|
||||
|
||||
private void HandleEmitSoundOnActivateInWorld(EntityUid eUI, EmitSoundOnActivateComponent component, ActivateInWorldEvent arg)
|
||||
{
|
||||
// Intentionally not checking whether the interaction has already been handled.
|
||||
TryEmitSound(component);
|
||||
|
||||
if (component.Handle)
|
||||
arg.Handled = true;
|
||||
}
|
||||
|
||||
private void HandleEmitSoundOnUIOpen(EntityUid eUI, BaseEmitSoundComponent component, AfterActivatableUIOpenEvent arg)
|
||||
{
|
||||
TryEmitSound(component);
|
||||
}
|
||||
|
||||
private void HandleEmitSoundOnPickup(EntityUid uid, EmitSoundOnPickupComponent component, GotEquippedHandEvent args)
|
||||
{
|
||||
TryEmitSound(component);
|
||||
}
|
||||
|
||||
private void HandleEmitSoundOnDrop(EntityUid uid, EmitSoundOnDropComponent component, DroppedEvent args)
|
||||
{
|
||||
TryEmitSound(component);
|
||||
}
|
||||
|
||||
private void TryEmitSound(BaseEmitSoundComponent component)
|
||||
{
|
||||
if (component.Sound == null)
|
||||
return;
|
||||
_audioSystem.PlayPvs(component.Sound, component.Owner, component.Sound.Params.AddVolume(-2f));
|
||||
}
|
||||
private void HandleEmitSoundOnTrigger(EntityUid uid, EmitSoundOnTriggerComponent component, TriggerEvent args)
|
||||
{
|
||||
TryEmitSound(component, args.User);
|
||||
args.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user