BalloonPopperSystem.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Content.Server.Hands.Systems;
  2. using Content.Server.Popups;
  3. using Content.Shared.IdentityManagement;
  4. using Content.Shared.Popups;
  5. using Content.Shared.Tag;
  6. using Content.Shared.Weapons.Melee.Events;
  7. using Content.Shared.Throwing;
  8. using Robust.Shared.Audio.Systems;
  9. namespace Content.Server.Weapons.Melee.Balloon;
  10. /// <summary>
  11. /// This handles popping ballons when attacked with <see cref="BalloonPopperComponent"/>
  12. /// </summary>
  13. public sealed class BalloonPopperSystem : EntitySystem
  14. {
  15. [Dependency] private readonly SharedAudioSystem _audio = default!;
  16. [Dependency] private readonly HandsSystem _hands = default!;
  17. [Dependency] private readonly PopupSystem _popup = default!;
  18. [Dependency] private readonly TagSystem _tag = default!;
  19. /// <inheritdoc/>
  20. public override void Initialize()
  21. {
  22. SubscribeLocalEvent<BalloonPopperComponent, MeleeHitEvent>(OnMeleeHit);
  23. SubscribeLocalEvent<BalloonPopperComponent, ThrowDoHitEvent>(OnThrowHit);
  24. }
  25. private void OnMeleeHit(EntityUid uid, BalloonPopperComponent component, MeleeHitEvent args)
  26. {
  27. foreach (var entity in args.HitEntities)
  28. {
  29. foreach (var held in _hands.EnumerateHeld(entity))
  30. {
  31. if (_tag.HasTag(held, component.BalloonTag))
  32. PopBallooon(uid, held, component);
  33. }
  34. if (_tag.HasTag(entity, component.BalloonTag))
  35. PopBallooon(uid, entity, component);
  36. }
  37. }
  38. private void OnThrowHit(EntityUid uid, BalloonPopperComponent component, ThrowDoHitEvent args)
  39. {
  40. foreach (var held in _hands.EnumerateHeld(args.Target))
  41. {
  42. if (_tag.HasTag(held, component.BalloonTag))
  43. PopBallooon(uid, held, component);
  44. }
  45. }
  46. /// <summary>
  47. /// Pops a target balloon, making a popup and playing a sound.
  48. /// </summary>
  49. public void PopBallooon(EntityUid popper, EntityUid balloon, BalloonPopperComponent? component = null)
  50. {
  51. if (!Resolve(popper, ref component))
  52. return;
  53. _audio.PlayPvs(component.PopSound, balloon);
  54. _popup.PopupCoordinates(Loc.GetString("melee-balloon-pop",
  55. ("balloon", Identity.Entity(balloon, EntityManager))), Transform(balloon).Coordinates, PopupType.Large);
  56. QueueDel(balloon);
  57. }
  58. }