1
0

HotPotatoSystem.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Content.Server.Audio;
  2. using Content.Server.Explosion.EntitySystems;
  3. using Content.Shared.Damage.Systems;
  4. using Content.Shared.Hands.Components;
  5. using Content.Shared.Hands.EntitySystems;
  6. using Content.Shared.HotPotato;
  7. using Content.Shared.Popups;
  8. using Content.Shared.Weapons.Melee.Events;
  9. namespace Content.Server.HotPotato;
  10. public sealed class HotPotatoSystem : SharedHotPotatoSystem
  11. {
  12. [Dependency] private readonly SharedHandsSystem _hands = default!;
  13. [Dependency] private readonly SharedPopupSystem _popup = default!;
  14. [Dependency] private readonly AmbientSoundSystem _ambientSound = default!;
  15. [Dependency] private readonly DamageOnHoldingSystem _damageOnHolding = default!;
  16. public override void Initialize()
  17. {
  18. base.Initialize();
  19. SubscribeLocalEvent<HotPotatoComponent, ActiveTimerTriggerEvent>(OnActiveTimer);
  20. SubscribeLocalEvent<HotPotatoComponent, MeleeHitEvent>(OnMeleeHit);
  21. }
  22. private void OnActiveTimer(EntityUid uid, HotPotatoComponent comp, ref ActiveTimerTriggerEvent args)
  23. {
  24. EnsureComp<ActiveHotPotatoComponent>(uid);
  25. comp.CanTransfer = false;
  26. _ambientSound.SetAmbience(uid, true);
  27. _damageOnHolding.SetEnabled(uid, true);
  28. Dirty(uid, comp);
  29. }
  30. private void OnMeleeHit(EntityUid uid, HotPotatoComponent comp, MeleeHitEvent args)
  31. {
  32. if (!HasComp<ActiveHotPotatoComponent>(uid))
  33. return;
  34. comp.CanTransfer = true;
  35. foreach (var hitEntity in args.HitEntities)
  36. {
  37. if (!TryComp<HandsComponent>(hitEntity, out var hands))
  38. continue;
  39. if (!_hands.IsHolding(hitEntity, uid, out _, hands) && _hands.TryForcePickupAnyHand(hitEntity, uid, handsComp: hands))
  40. {
  41. _popup.PopupEntity(Loc.GetString("hot-potato-passed",
  42. ("from", args.User), ("to", hitEntity)), uid, PopupType.Medium);
  43. break;
  44. }
  45. _popup.PopupEntity(Loc.GetString("hot-potato-failed",
  46. ("to", hitEntity)), uid, PopupType.Medium);
  47. break;
  48. }
  49. comp.CanTransfer = false;
  50. Dirty(uid, comp);
  51. }
  52. }