ActionEvents.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using Content.Shared.Hands;
  2. using Content.Shared.Inventory;
  3. using Content.Shared.Inventory.Events;
  4. using Robust.Shared.Map;
  5. using Robust.Shared.Serialization;
  6. namespace Content.Shared.Actions;
  7. /// <summary>
  8. /// Event raised directed at items or clothing when they are equipped or held. In order for an item to grant actions some
  9. /// system can subscribe to this event and add actions to the <see cref="Actions"/> list.
  10. /// </summary>
  11. /// <remarks>
  12. /// Note that a system could also just manually add actions as a result of a <see cref="GotEquippedEvent"/> or <see
  13. /// cref="GotEquippedHandEvent"/>. This exists mostly as a convenience event, while also helping to keep
  14. /// action-granting logic separate from general equipment behavior.
  15. /// </remarks>
  16. public sealed class GetItemActionsEvent : EntityEventArgs
  17. {
  18. private readonly ActionContainerSystem _system;
  19. public readonly SortedSet<EntityUid> Actions = new();
  20. /// <summary>
  21. /// User equipping the item.
  22. /// </summary>
  23. public EntityUid User;
  24. /// <summary>
  25. /// The entity that is being asked to provide the actions. This is used as a default argument to <see cref="AddAction(ref System.Nullable{Robust.Shared.GameObjects.EntityUid},string,Robust.Shared.GameObjects.EntityUid)"/>.
  26. /// I.e., if a new action needs to be spawned, then it will be inserted into this entity unless otherwise specified.
  27. /// </summary>
  28. public EntityUid Provider;
  29. /// <summary>
  30. /// Slot flags for the inventory slot that this item got equipped to. Null if not in a slot (i.e., if equipped to hands).
  31. /// </summary>
  32. public SlotFlags? SlotFlags;
  33. /// <summary>
  34. /// If true, the item was equipped to a users hands.
  35. /// </summary>
  36. public bool InHands => SlotFlags == null;
  37. public GetItemActionsEvent(ActionContainerSystem system, EntityUid user, EntityUid provider, SlotFlags? slotFlags = null)
  38. {
  39. _system = system;
  40. User = user;
  41. Provider = provider;
  42. SlotFlags = slotFlags;
  43. }
  44. /// <summary>
  45. /// Grant the given action. If the EntityUid does not refer to a valid action entity, it will create a new action and
  46. /// store it in <see cref="container"/>.
  47. /// </summary>
  48. public void AddAction(ref EntityUid? actionId, string prototypeId, EntityUid container)
  49. {
  50. if (_system.EnsureAction(container, ref actionId, prototypeId))
  51. Actions.Add(actionId.Value);
  52. }
  53. /// <summary>
  54. /// Grant the given action. If the EntityUid does not refer to a valid action entity, it will create a new action and
  55. /// store it in <see cref="Provider"/>.
  56. /// </summary>
  57. public void AddAction(ref EntityUid? actionId, string prototypeId)
  58. {
  59. AddAction(ref actionId, prototypeId, Provider);
  60. }
  61. public void AddAction(EntityUid? actionId)
  62. {
  63. if (actionId != null)
  64. Actions.Add(actionId.Value);
  65. }
  66. }
  67. /// <summary>
  68. /// Event used to communicate with the server that a client wishes to perform some action.
  69. /// </summary>
  70. [Serializable, NetSerializable]
  71. public sealed class RequestPerformActionEvent : EntityEventArgs
  72. {
  73. public readonly NetEntity Action;
  74. public readonly NetEntity? EntityTarget;
  75. public readonly NetCoordinates? EntityCoordinatesTarget;
  76. public RequestPerformActionEvent(NetEntity action)
  77. {
  78. Action = action;
  79. }
  80. public RequestPerformActionEvent(NetEntity action, NetEntity entityTarget)
  81. {
  82. Action = action;
  83. EntityTarget = entityTarget;
  84. }
  85. public RequestPerformActionEvent(NetEntity action, NetCoordinates entityCoordinatesTarget)
  86. {
  87. Action = action;
  88. EntityCoordinatesTarget = entityCoordinatesTarget;
  89. }
  90. public RequestPerformActionEvent(NetEntity action, NetEntity entityTarget, NetCoordinates entityCoordinatesTarget)
  91. {
  92. Action = action;
  93. EntityTarget = entityTarget;
  94. EntityCoordinatesTarget = entityCoordinatesTarget;
  95. }
  96. }
  97. /// <summary>
  98. /// This is the type of event that gets raised when an <see cref="InstantAction"/> is performed. The <see
  99. /// cref="Performer"/> field is automatically filled out by the <see cref="SharedActionsSystem"/>.
  100. /// </summary>
  101. /// <remarks>
  102. /// To define a new action for some system, you need to create an event that inherits from this class.
  103. /// </remarks>
  104. public abstract partial class InstantActionEvent : BaseActionEvent { }
  105. /// <summary>
  106. /// This is the type of event that gets raised when an <see cref="EntityTargetAction"/> is performed. The <see
  107. /// cref="Performer"/> and <see cref="Target"/> fields will automatically be filled out by the <see
  108. /// cref="SharedActionsSystem"/>.
  109. /// </summary>
  110. /// <remarks>
  111. /// To define a new action for some system, you need to create an event that inherits from this class.
  112. /// </remarks>
  113. public abstract partial class EntityTargetActionEvent : BaseActionEvent
  114. {
  115. /// <summary>
  116. /// The entity that the user targeted.
  117. /// </summary>
  118. public EntityUid Target;
  119. }
  120. /// <summary>
  121. /// This is the type of event that gets raised when an <see cref="WorldTargetAction"/> is performed. The <see
  122. /// cref="Performer"/> and <see cref="Target"/> fields will automatically be filled out by the <see
  123. /// cref="SharedActionsSystem"/>.
  124. /// </summary>
  125. /// <remarks>
  126. /// To define a new action for some system, you need to create an event that inherits from this class.
  127. /// </remarks>
  128. public abstract partial class WorldTargetActionEvent : BaseActionEvent
  129. {
  130. /// <summary>
  131. /// The coordinates of the location that the user targeted.
  132. /// </summary>
  133. public EntityCoordinates Target;
  134. }
  135. /// <summary>
  136. /// This is the type of event that gets raised when an <see cref="EntityWorldTargetActionComponent"/> is performed.
  137. /// The <see cref="BaseActionEvent.Performer"/>, <see cref="Entity"/>, and <see cref="Coords"/>
  138. /// fields will automatically be filled out by the <see cref="SharedActionsSystem"/>.
  139. /// </summary>
  140. /// <remarks>
  141. /// To define a new action for some system, you need to create an event that inherits from this class.
  142. /// </remarks>
  143. public abstract partial class EntityWorldTargetActionEvent : BaseActionEvent
  144. {
  145. /// <summary>
  146. /// The entity that the user targeted.
  147. /// </summary>
  148. public EntityUid? Entity;
  149. /// <summary>
  150. /// The coordinates of the location that the user targeted.
  151. /// </summary>
  152. public EntityCoordinates? Coords;
  153. }
  154. /// <summary>
  155. /// Base class for events that are raised when an action gets performed. This should not generally be used outside of the action
  156. /// system.
  157. /// </summary>
  158. [ImplicitDataDefinitionForInheritors]
  159. public abstract partial class BaseActionEvent : HandledEntityEventArgs
  160. {
  161. /// <summary>
  162. /// The user performing the action.
  163. /// </summary>
  164. public EntityUid Performer;
  165. /// <summary>
  166. /// The action the event belongs to.
  167. /// </summary>
  168. public Entity<BaseActionComponent> Action;
  169. /// <summary>
  170. /// Should we toggle the action entity?
  171. /// </summary>
  172. public bool Toggle;
  173. }