1
0

ImplanterSystem.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System.Linq;
  2. using Content.Server.Popups;
  3. using Content.Shared.DoAfter;
  4. using Content.Shared.IdentityManagement;
  5. using Content.Shared.Implants;
  6. using Content.Shared.Implants.Components;
  7. using Content.Shared.Interaction;
  8. using Content.Shared.Popups;
  9. using Robust.Shared.Containers;
  10. namespace Content.Server.Implants;
  11. public sealed partial class ImplanterSystem : SharedImplanterSystem
  12. {
  13. [Dependency] private readonly PopupSystem _popup = default!;
  14. [Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
  15. [Dependency] private readonly SharedContainerSystem _container = default!;
  16. public override void Initialize()
  17. {
  18. base.Initialize();
  19. InitializeImplanted();
  20. SubscribeLocalEvent<ImplanterComponent, AfterInteractEvent>(OnImplanterAfterInteract);
  21. SubscribeLocalEvent<ImplanterComponent, ImplantEvent>(OnImplant);
  22. SubscribeLocalEvent<ImplanterComponent, DrawEvent>(OnDraw);
  23. }
  24. private void OnImplanterAfterInteract(EntityUid uid, ImplanterComponent component, AfterInteractEvent args)
  25. {
  26. if (args.Target == null || !args.CanReach || args.Handled)
  27. return;
  28. var target = args.Target.Value;
  29. if (!CheckTarget(target, component.Whitelist, component.Blacklist))
  30. return;
  31. //TODO: Rework when surgery is in for implant cases
  32. if (component.CurrentMode == ImplanterToggleMode.Draw && !component.ImplantOnly)
  33. {
  34. TryDraw(component, args.User, target, uid);
  35. }
  36. else
  37. {
  38. if (!CanImplant(args.User, target, uid, component, out var implant, out _))
  39. {
  40. // no popup if implant doesn't exist
  41. if (implant == null)
  42. return;
  43. // show popup to the user saying implant failed
  44. var name = Identity.Name(target, EntityManager, args.User);
  45. var msg = Loc.GetString("implanter-component-implant-failed", ("implant", implant), ("target", name));
  46. _popup.PopupEntity(msg, target, args.User);
  47. // prevent further interaction since popup was shown
  48. args.Handled = true;
  49. return;
  50. }
  51. //Implant self instantly, otherwise try to inject the target.
  52. if (args.User == target)
  53. Implant(target, target, uid, component);
  54. else
  55. TryImplant(component, args.User, target, uid);
  56. }
  57. args.Handled = true;
  58. }
  59. /// <summary>
  60. /// Attempt to implant someone else.
  61. /// </summary>
  62. /// <param name="component">Implanter component</param>
  63. /// <param name="user">The entity using the implanter</param>
  64. /// <param name="target">The entity being implanted</param>
  65. /// <param name="implanter">The implanter being used</param>
  66. public void TryImplant(ImplanterComponent component, EntityUid user, EntityUid target, EntityUid implanter)
  67. {
  68. var args = new DoAfterArgs(EntityManager, user, component.ImplantTime, new ImplantEvent(), implanter, target: target, used: implanter)
  69. {
  70. BreakOnDamage = true,
  71. BreakOnMove = true,
  72. NeedHand = true,
  73. };
  74. if (!_doAfter.TryStartDoAfter(args))
  75. return;
  76. _popup.PopupEntity(Loc.GetString("injector-component-injecting-user"), target, user);
  77. var userName = Identity.Entity(user, EntityManager);
  78. _popup.PopupEntity(Loc.GetString("implanter-component-implanting-target", ("user", userName)), user, target, PopupType.LargeCaution);
  79. }
  80. /// <summary>
  81. /// Try to remove an implant and store it in an implanter
  82. /// </summary>
  83. /// <param name="component">Implanter component</param>
  84. /// <param name="user">The entity using the implanter</param>
  85. /// <param name="target">The entity getting their implant removed</param>
  86. /// <param name="implanter">The implanter being used</param>
  87. //TODO: Remove when surgery is in
  88. public void TryDraw(ImplanterComponent component, EntityUid user, EntityUid target, EntityUid implanter)
  89. {
  90. var args = new DoAfterArgs(EntityManager, user, component.DrawTime, new DrawEvent(), implanter, target: target, used: implanter)
  91. {
  92. BreakOnDamage = true,
  93. BreakOnMove = true,
  94. NeedHand = true,
  95. };
  96. if (_doAfter.TryStartDoAfter(args))
  97. _popup.PopupEntity(Loc.GetString("injector-component-injecting-user"), target, user);
  98. }
  99. private void OnImplant(EntityUid uid, ImplanterComponent component, ImplantEvent args)
  100. {
  101. if (args.Cancelled || args.Handled || args.Target == null || args.Used == null)
  102. return;
  103. Implant(args.User, args.Target.Value, args.Used.Value, component);
  104. args.Handled = true;
  105. }
  106. private void OnDraw(EntityUid uid, ImplanterComponent component, DrawEvent args)
  107. {
  108. if (args.Cancelled || args.Handled || args.Used == null || args.Target == null)
  109. return;
  110. Draw(args.Used.Value, args.User, args.Target.Value, component);
  111. args.Handled = true;
  112. }
  113. }