2022-03-25 17:17:29 +13:00
|
|
|
using Content.Server.Explosion.EntitySystems;
|
2021-06-19 11:35:56 +02:00
|
|
|
using Content.Server.Interaction.Components;
|
2021-06-27 21:55:18 +02:00
|
|
|
using Content.Server.Sound.Components;
|
2021-06-19 11:35:56 +02:00
|
|
|
using Content.Server.Throwing;
|
2022-04-08 17:17:05 -04:00
|
|
|
using Content.Server.UserInterface;
|
2021-07-10 11:20:23 +02:00
|
|
|
using Content.Shared.Interaction;
|
2022-03-13 01:33:23 +13:00
|
|
|
using Content.Shared.Interaction.Events;
|
2021-07-10 11:20:23 +02:00
|
|
|
using Content.Shared.Throwing;
|
2021-06-19 11:35:56 +02:00
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using Robust.Shared.Audio;
|
2021-07-10 11:20:23 +02:00
|
|
|
using Robust.Shared.Player;
|
2022-03-25 17:17:29 +13:00
|
|
|
using Robust.Shared.Random;
|
2021-06-19 11:35:56 +02:00
|
|
|
|
2021-06-27 21:48:57 +02:00
|
|
|
namespace Content.Server.Sound
|
2021-06-19 11:35:56 +02:00
|
|
|
{
|
2021-07-10 11:20:23 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Will play a sound on various events if the affected entity has a component derived from BaseEmitSoundComponent
|
|
|
|
|
/// </summary>
|
2021-06-19 11:35:56 +02:00
|
|
|
[UsedImplicitly]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class EmitSoundSystem : EntitySystem
|
2021-06-19 11:35:56 +02:00
|
|
|
{
|
2022-03-25 17:17:29 +13:00
|
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
|
|
|
|
|
2021-06-19 11:35:56 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2021-07-12 13:38:05 +02:00
|
|
|
SubscribeLocalEvent<EmitSoundOnLandComponent, LandEvent>(HandleEmitSoundOnLand);
|
|
|
|
|
SubscribeLocalEvent<EmitSoundOnUseComponent, UseInHandEvent>(HandleEmitSoundOnUseInHand);
|
|
|
|
|
SubscribeLocalEvent<EmitSoundOnThrowComponent, ThrownEvent>(HandleEmitSoundOnThrown);
|
|
|
|
|
SubscribeLocalEvent<EmitSoundOnActivateComponent, ActivateInWorldEvent>(HandleEmitSoundOnActivateInWorld);
|
2022-03-25 17:17:29 +13:00
|
|
|
SubscribeLocalEvent<EmitSoundOnTriggerComponent, TriggerEvent>(HandleEmitSoundOnTrigger);
|
2022-04-08 17:17:05 -04:00
|
|
|
SubscribeLocalEvent<EmitSoundOnUIOpenComponent, AfterActivatableUIOpenEvent>(HandleEmitSoundOnUIOpen);
|
2022-03-25 17:17:29 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleEmitSoundOnTrigger(EntityUid uid, EmitSoundOnTriggerComponent component, TriggerEvent args)
|
|
|
|
|
{
|
|
|
|
|
TryEmitSound(component);
|
2021-06-19 11:35:56 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-12 13:38:05 +02:00
|
|
|
private void HandleEmitSoundOnLand(EntityUid eUI, BaseEmitSoundComponent component, LandEvent arg)
|
|
|
|
|
{
|
|
|
|
|
TryEmitSound(component);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-30 22:01:32 +13:00
|
|
|
private void HandleEmitSoundOnUseInHand(EntityUid eUI, EmitSoundOnUseComponent component, UseInHandEvent arg)
|
2021-07-12 13:38:05 +02:00
|
|
|
{
|
2022-03-30 22:01:32 +13:00
|
|
|
// Intentionally not checking whether the interaction has already been handled.
|
2021-07-12 13:38:05 +02:00
|
|
|
TryEmitSound(component);
|
2022-03-30 22:01:32 +13:00
|
|
|
|
|
|
|
|
if (component.Handle)
|
|
|
|
|
arg.Handled = true;
|
2021-07-12 13:38:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleEmitSoundOnThrown(EntityUid eUI, BaseEmitSoundComponent component, ThrownEvent arg)
|
|
|
|
|
{
|
|
|
|
|
TryEmitSound(component);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-30 22:01:32 +13:00
|
|
|
private void HandleEmitSoundOnActivateInWorld(EntityUid eUI, EmitSoundOnActivateComponent component, ActivateInWorldEvent arg)
|
2021-07-12 13:38:05 +02:00
|
|
|
{
|
2022-03-30 22:01:32 +13:00
|
|
|
// Intentionally not checking whether the interaction has already been handled.
|
2021-07-12 13:38:05 +02:00
|
|
|
TryEmitSound(component);
|
2022-03-30 22:01:32 +13:00
|
|
|
|
|
|
|
|
if (component.Handle)
|
|
|
|
|
arg.Handled = true;
|
2021-07-12 13:38:05 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-08 17:17:05 -04:00
|
|
|
private void HandleEmitSoundOnUIOpen(EntityUid eUI, BaseEmitSoundComponent component, AfterActivatableUIOpenEvent arg)
|
|
|
|
|
{
|
|
|
|
|
TryEmitSound(component);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 17:17:29 +13:00
|
|
|
private void TryEmitSound(BaseEmitSoundComponent component)
|
2021-06-19 11:35:56 +02:00
|
|
|
{
|
2022-03-25 17:17:29 +13:00
|
|
|
var audioParams = component.AudioParams.WithPitchScale((float) _random.NextGaussian(1, component.PitchVariation));
|
|
|
|
|
SoundSystem.Play(Filter.Pvs(component.Owner, entityManager: EntityManager), component.Sound.GetSound(), component.Owner, audioParams);
|
2021-06-19 11:35:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|