1
0

WhistleSystem.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Content.Shared.Coordinates;
  2. using Content.Shared.Humanoid;
  3. using Content.Shared.Interaction.Events;
  4. using Content.Shared.Stealth.Components;
  5. using JetBrains.Annotations;
  6. using Robust.Shared.Timing;
  7. namespace Content.Shared.Whistle;
  8. public sealed class WhistleSystem : EntitySystem
  9. {
  10. [Dependency] private readonly EntityLookupSystem _entityLookup = default!;
  11. [Dependency] private readonly IGameTiming _timing = default!;
  12. [Dependency] private readonly SharedTransformSystem _transform = default!;
  13. public override void Initialize()
  14. {
  15. base.Initialize();
  16. SubscribeLocalEvent<WhistleComponent, UseInHandEvent>(OnUseInHand);
  17. }
  18. private void ExclamateTarget(EntityUid target, WhistleComponent component)
  19. {
  20. SpawnAttachedTo(component.Effect, target.ToCoordinates());
  21. }
  22. public void OnUseInHand(EntityUid uid, WhistleComponent component, UseInHandEvent args)
  23. {
  24. if (args.Handled || !_timing.IsFirstTimePredicted)
  25. return;
  26. args.Handled = TryMakeLoudWhistle(uid, args.User, component);
  27. }
  28. public bool TryMakeLoudWhistle(EntityUid uid, EntityUid owner, WhistleComponent? component = null)
  29. {
  30. if (!Resolve(uid, ref component, false) || component.Distance <= 0)
  31. return false;
  32. MakeLoudWhistle(uid, owner, component);
  33. return true;
  34. }
  35. private void MakeLoudWhistle(EntityUid uid, EntityUid owner, WhistleComponent component)
  36. {
  37. StealthComponent? stealth = null;
  38. foreach (var iterator in
  39. _entityLookup.GetEntitiesInRange<HumanoidAppearanceComponent>(_transform.GetMapCoordinates(uid), component.Distance))
  40. {
  41. //Avoid pinging invisible entities
  42. if (TryComp(iterator, out stealth) && stealth.Enabled)
  43. continue;
  44. //We don't want to ping user of whistle
  45. if (iterator.Owner == owner)
  46. continue;
  47. ExclamateTarget(iterator, component);
  48. }
  49. }
  50. }