1
0

SharedHandsSystem.Interactions.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System.Linq;
  2. using Content.Shared.Examine;
  3. using Content.Shared.Hands.Components;
  4. using Content.Shared.IdentityManagement;
  5. using Content.Shared.Input;
  6. using Content.Shared.Interaction;
  7. using Content.Shared.Inventory.VirtualItem;
  8. using Content.Shared.Localizations;
  9. using Robust.Shared.Input.Binding;
  10. using Robust.Shared.Map;
  11. using Robust.Shared.Player;
  12. using Robust.Shared.Utility;
  13. namespace Content.Shared.Hands.EntitySystems;
  14. public abstract partial class SharedHandsSystem : EntitySystem
  15. {
  16. private void InitializeInteractions()
  17. {
  18. SubscribeAllEvent<RequestSetHandEvent>(HandleSetHand);
  19. SubscribeAllEvent<RequestActivateInHandEvent>(HandleActivateItemInHand);
  20. SubscribeAllEvent<RequestHandInteractUsingEvent>(HandleInteractUsingInHand);
  21. SubscribeAllEvent<RequestUseInHandEvent>(HandleUseInHand);
  22. SubscribeAllEvent<RequestMoveHandItemEvent>(HandleMoveItemFromHand);
  23. SubscribeAllEvent<RequestHandAltInteractEvent>(HandleHandAltInteract);
  24. SubscribeLocalEvent<HandsComponent, GetUsedEntityEvent>(OnGetUsedEntity);
  25. SubscribeLocalEvent<HandsComponent, ExaminedEvent>(HandleExamined);
  26. CommandBinds.Builder
  27. .Bind(ContentKeyFunctions.UseItemInHand, InputCmdHandler.FromDelegate(HandleUseItem, handle: false, outsidePrediction: false))
  28. .Bind(ContentKeyFunctions.AltUseItemInHand, InputCmdHandler.FromDelegate(HandleAltUseInHand, handle: false, outsidePrediction: false))
  29. .Bind(ContentKeyFunctions.SwapHands, InputCmdHandler.FromDelegate(SwapHandsPressed, handle: false, outsidePrediction: false))
  30. .Bind(ContentKeyFunctions.Drop, new PointerInputCmdHandler(DropPressed))
  31. .Register<SharedHandsSystem>();
  32. }
  33. #region Event and Key-binding Handlers
  34. private void HandleAltUseInHand(ICommonSession? session)
  35. {
  36. if (session?.AttachedEntity != null)
  37. TryUseItemInHand(session.AttachedEntity.Value, true);
  38. }
  39. private void HandleUseItem(ICommonSession? session)
  40. {
  41. if (session?.AttachedEntity != null)
  42. TryUseItemInHand(session.AttachedEntity.Value);
  43. }
  44. private void HandleMoveItemFromHand(RequestMoveHandItemEvent msg, EntitySessionEventArgs args)
  45. {
  46. if (args.SenderSession.AttachedEntity != null)
  47. TryMoveHeldEntityToActiveHand(args.SenderSession.AttachedEntity.Value, msg.HandName);
  48. }
  49. private void HandleUseInHand(RequestUseInHandEvent msg, EntitySessionEventArgs args)
  50. {
  51. if (args.SenderSession.AttachedEntity != null)
  52. TryUseItemInHand(args.SenderSession.AttachedEntity.Value);
  53. }
  54. private void HandleActivateItemInHand(RequestActivateInHandEvent msg, EntitySessionEventArgs args)
  55. {
  56. if (args.SenderSession.AttachedEntity != null)
  57. TryActivateItemInHand(args.SenderSession.AttachedEntity.Value, null, msg.HandName);
  58. }
  59. private void HandleInteractUsingInHand(RequestHandInteractUsingEvent msg, EntitySessionEventArgs args)
  60. {
  61. if (args.SenderSession.AttachedEntity != null)
  62. TryInteractHandWithActiveHand(args.SenderSession.AttachedEntity.Value, msg.HandName);
  63. }
  64. private void HandleHandAltInteract(RequestHandAltInteractEvent msg, EntitySessionEventArgs args)
  65. {
  66. if (args.SenderSession.AttachedEntity != null)
  67. TryUseItemInHand(args.SenderSession.AttachedEntity.Value, true, handName: msg.HandName);
  68. }
  69. private void SwapHandsPressed(ICommonSession? session)
  70. {
  71. if (!TryComp(session?.AttachedEntity, out HandsComponent? component))
  72. return;
  73. if (!_actionBlocker.CanInteract(session.AttachedEntity.Value, null))
  74. return;
  75. if (component.ActiveHand == null || component.Hands.Count < 2)
  76. return;
  77. var newActiveIndex = component.SortedHands.IndexOf(component.ActiveHand.Name) + 1;
  78. var nextHand = component.SortedHands[newActiveIndex % component.Hands.Count];
  79. TrySetActiveHand(session.AttachedEntity.Value, nextHand, component);
  80. }
  81. private bool DropPressed(ICommonSession? session, EntityCoordinates coords, EntityUid netEntity)
  82. {
  83. if (TryComp(session?.AttachedEntity, out HandsComponent? hands) && hands.ActiveHand != null)
  84. TryDrop(session.AttachedEntity.Value, hands.ActiveHand, coords, handsComp: hands);
  85. // always send to server.
  86. return false;
  87. }
  88. #endregion
  89. public bool TryActivateItemInHand(EntityUid uid, HandsComponent? handsComp = null, string? handName = null)
  90. {
  91. if (!Resolve(uid, ref handsComp, false))
  92. return false;
  93. Hand? hand;
  94. if (handName == null || !handsComp.Hands.TryGetValue(handName, out hand))
  95. hand = handsComp.ActiveHand;
  96. if (hand?.HeldEntity is not { } held)
  97. return false;
  98. return _interactionSystem.InteractionActivate(uid, held);
  99. }
  100. public bool TryInteractHandWithActiveHand(EntityUid uid, string handName, HandsComponent? handsComp = null)
  101. {
  102. if (!Resolve(uid, ref handsComp, false))
  103. return false;
  104. if (handsComp.ActiveHandEntity == null)
  105. return false;
  106. if (!handsComp.Hands.TryGetValue(handName, out var hand))
  107. return false;
  108. if (hand.HeldEntity == null)
  109. return false;
  110. _interactionSystem.InteractUsing(uid, handsComp.ActiveHandEntity.Value, hand.HeldEntity.Value, Transform(hand.HeldEntity.Value).Coordinates);
  111. return true;
  112. }
  113. public bool TryUseItemInHand(EntityUid uid, bool altInteract = false, HandsComponent? handsComp = null, string? handName = null)
  114. {
  115. if (!Resolve(uid, ref handsComp, false))
  116. return false;
  117. Hand? hand;
  118. if (handName == null || !handsComp.Hands.TryGetValue(handName, out hand))
  119. hand = handsComp.ActiveHand;
  120. if (hand?.HeldEntity is not { } held)
  121. return false;
  122. if (altInteract)
  123. return _interactionSystem.AltInteract(uid, held);
  124. else
  125. return _interactionSystem.UseInHandInteraction(uid, held);
  126. }
  127. /// <summary>
  128. /// Moves an entity from one hand to the active hand.
  129. /// </summary>
  130. public bool TryMoveHeldEntityToActiveHand(EntityUid uid, string handName, bool checkActionBlocker = true, HandsComponent? handsComp = null)
  131. {
  132. if (!Resolve(uid, ref handsComp))
  133. return false;
  134. if (handsComp.ActiveHand == null || !handsComp.ActiveHand.IsEmpty)
  135. return false;
  136. if (!handsComp.Hands.TryGetValue(handName, out var hand))
  137. return false;
  138. if (!CanDropHeld(uid, hand, checkActionBlocker))
  139. return false;
  140. var entity = hand.HeldEntity!.Value;
  141. if (!CanPickupToHand(uid, entity, handsComp.ActiveHand, checkActionBlocker, handsComp))
  142. return false;
  143. DoDrop(uid, hand, false, handsComp, log:false);
  144. DoPickup(uid, handsComp.ActiveHand, entity, handsComp, log: false);
  145. return true;
  146. }
  147. private void OnGetUsedEntity(EntityUid uid, HandsComponent component, ref GetUsedEntityEvent args)
  148. {
  149. if (args.Handled)
  150. return;
  151. if (component.ActiveHandEntity.HasValue)
  152. {
  153. // allow for the item to return a different entity, e.g. virtual items
  154. RaiseLocalEvent(component.ActiveHandEntity.Value, ref args);
  155. }
  156. args.Used ??= component.ActiveHandEntity;
  157. }
  158. //TODO: Actually shows all items/clothing/etc.
  159. private void HandleExamined(EntityUid examinedUid, HandsComponent handsComp, ExaminedEvent args)
  160. {
  161. var heldItemNames = EnumerateHeld(examinedUid, handsComp)
  162. .Where(entity => !HasComp<VirtualItemComponent>(entity))
  163. .Select(item => FormattedMessage.EscapeText(Identity.Name(item, EntityManager)))
  164. .Select(itemName => Loc.GetString("comp-hands-examine-wrapper", ("item", itemName)))
  165. .ToList();
  166. var locKey = heldItemNames.Count != 0 ? "comp-hands-examine" : "comp-hands-examine-empty";
  167. var locUser = ("user", Identity.Entity(examinedUid, EntityManager));
  168. var locItems = ("items", ContentLocalizationManager.FormatList(heldItemNames));
  169. using (args.PushGroup(nameof(HandsComponent)))
  170. {
  171. args.PushMarkup(Loc.GetString(locKey, locUser, locItems));
  172. }
  173. }
  174. }