1
0

EmitSoundSystem.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Content.Server.Explosion.EntitySystems;
  2. using Content.Server.Sound.Components;
  3. using Content.Shared.UserInterface;
  4. using Content.Shared.Sound;
  5. using Content.Shared.Sound.Components;
  6. using Robust.Shared.Timing;
  7. using Robust.Shared.Network;
  8. namespace Content.Server.Sound;
  9. public sealed class EmitSoundSystem : SharedEmitSoundSystem
  10. {
  11. [Dependency] private readonly IGameTiming _timing = default!;
  12. [Dependency] private readonly INetManager _net = default!;
  13. public override void Update(float frameTime)
  14. {
  15. base.Update(frameTime);
  16. var query = EntityQueryEnumerator<SpamEmitSoundComponent>();
  17. while (query.MoveNext(out var uid, out var soundSpammer))
  18. {
  19. if (!soundSpammer.Enabled)
  20. continue;
  21. if (_timing.CurTime >= soundSpammer.NextSound)
  22. {
  23. if (soundSpammer.PopUp != null)
  24. Popup.PopupEntity(Loc.GetString(soundSpammer.PopUp), uid);
  25. TryEmitSound(uid, soundSpammer, predict: false);
  26. SpamEmitSoundReset((uid, soundSpammer));
  27. }
  28. }
  29. }
  30. public override void Initialize()
  31. {
  32. base.Initialize();
  33. SubscribeLocalEvent<EmitSoundOnTriggerComponent, TriggerEvent>(HandleEmitSoundOnTrigger);
  34. SubscribeLocalEvent<SpamEmitSoundComponent, MapInitEvent>(HandleSpamEmitSoundMapInit);
  35. }
  36. private void HandleEmitSoundOnTrigger(EntityUid uid, EmitSoundOnTriggerComponent component, TriggerEvent args)
  37. {
  38. TryEmitSound(uid, component, args.User, false);
  39. args.Handled = true;
  40. }
  41. private void HandleSpamEmitSoundMapInit(Entity<SpamEmitSoundComponent> entity, ref MapInitEvent args)
  42. {
  43. SpamEmitSoundReset(entity);
  44. // Prewarm so multiple entities have more variation.
  45. entity.Comp.NextSound -= Random.Next(entity.Comp.MaxInterval);
  46. Dirty(entity);
  47. }
  48. private void SpamEmitSoundReset(Entity<SpamEmitSoundComponent> entity)
  49. {
  50. if (_net.IsClient)
  51. return;
  52. entity.Comp.NextSound = _timing.CurTime + ((entity.Comp.MinInterval < entity.Comp.MaxInterval)
  53. ? Random.Next(entity.Comp.MinInterval, entity.Comp.MaxInterval)
  54. : entity.Comp.MaxInterval);
  55. Dirty(entity);
  56. }
  57. public override void SetEnabled(Entity<SpamEmitSoundComponent?> entity, bool enabled)
  58. {
  59. if (!Resolve(entity, ref entity.Comp, false))
  60. return;
  61. if (entity.Comp.Enabled == enabled)
  62. return;
  63. entity.Comp.Enabled = enabled;
  64. if (enabled)
  65. SpamEmitSoundReset((entity, entity.Comp));
  66. }
  67. }