Files
OldThink/Content.Server/Sound/EmitSoundSystem.cs

143 lines
5.6 KiB
C#
Raw Normal View History

using Content.Server.Explosion.EntitySystems;
using Content.Server.Interaction.Components;
2021-06-27 21:55:18 +02:00
using Content.Server.Sound.Components;
using Content.Server.Throwing;
using Content.Server.UserInterface;
2022-07-29 14:13:12 +12:00
using Content.Server.Popups;
using Content.Shared.Hands;
using Content.Shared.Interaction;
2022-03-13 01:33:23 +13:00
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 Robust.Shared.Random;
namespace Content.Server.Sound
{
/// <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
{
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly ITileDefinitionManager _tileDefMan = default!;
2022-07-29 14:13:12 +12:00
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
2022-07-27 00:46:24 -04:00
[Dependency] private readonly PopupSystem _popupSystem = default!;
/// <inheritdoc />
2022-07-15 03:54:34 -04:00
public override void Update(float frameTime)
{
base.Update(frameTime);
foreach (var soundSpammer in EntityQuery<SpamEmitSoundComponent>())
{
if (!soundSpammer.Enabled)
continue;
2022-07-15 03:54:34 -04:00
soundSpammer.Accumulator += frameTime;
if (soundSpammer.Accumulator < soundSpammer.RollInterval)
{
continue;
}
soundSpammer.Accumulator -= soundSpammer.RollInterval;
if (_random.Prob(soundSpammer.PlayChance))
{
2022-07-27 00:46:24 -04:00
if (soundSpammer.PopUp != null)
_popupSystem.PopupEntity(Loc.GetString(soundSpammer.PopUp), soundSpammer.Owner);
2022-07-15 03:54:34 -04:00
TryEmitSound(soundSpammer);
}
}
}
public override void Initialize()
{
base.Initialize();
2022-08-31 12:24:21 +02:00
SubscribeLocalEvent<EmitSoundOnSpawnComponent, ComponentInit>(HandleEmitSpawnOnInit);
2021-07-12 13:38:05 +02:00
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);
}
2022-08-31 12:24:21 +02:00
private void HandleEmitSpawnOnInit(EntityUid uid, EmitSoundOnSpawnComponent component, ComponentInit args)
{
TryEmitSound(component);
}
private void HandleEmitSoundOnTrigger(EntityUid uid, EmitSoundOnTriggerComponent component, TriggerEvent args)
{
TryEmitSound(component);
2022-06-01 01:39:06 -07:00
args.Handled = true;
}
private void HandleEmitSoundOnLand(EntityUid uid, BaseEmitSoundComponent component, LandEvent arg)
2021-07-12 13:38:05 +02:00
{
if (!TryComp<TransformComponent>(uid, out var xform) ||
2022-06-20 12:14:35 +12:00
!_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;
2021-07-12 13:38:05 +02:00
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
}
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)
{
2022-08-31 12:24:21 +02:00
if (component.Sound == null)
return;
2022-07-29 14:13:12 +12:00
_audioSystem.PlayPvs(component.Sound, component.Owner, component.Sound.Params.AddVolume(-2f));
}
}
}