DoAfterEvent.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Robust.Shared.Serialization;
  2. namespace Content.Shared.DoAfter;
  3. /// <summary>
  4. /// Base type for events that get raised when a do-after is canceled or finished.
  5. /// </summary>
  6. [Serializable, NetSerializable]
  7. [ImplicitDataDefinitionForInheritors]
  8. public abstract partial class DoAfterEvent : HandledEntityEventArgs
  9. {
  10. /// <summary>
  11. /// The do after that triggered this event. This will be set by the do after system before the event is raised.
  12. /// </summary>
  13. [NonSerialized]
  14. public DoAfter DoAfter = default!;
  15. //TODO: User pref to toggle repeat on specific doafters
  16. /// <summary>
  17. /// If set to true while handling this event, then the DoAfter will automatically be repeated.
  18. /// </summary>
  19. public bool Repeat = false;
  20. /// <summary>
  21. /// Duplicate the current event. This is used by state handling, and should copy by value unless the reference
  22. /// types are immutable.
  23. /// </summary>
  24. public abstract DoAfterEvent Clone();
  25. #region Convenience properties
  26. public bool Cancelled => DoAfter.Cancelled;
  27. public EntityUid User => DoAfter.Args.User;
  28. public EntityUid? Target => DoAfter.Args.Target;
  29. public EntityUid? Used => DoAfter.Args.Used;
  30. public DoAfterArgs Args => DoAfter.Args;
  31. #endregion
  32. /// <summary>
  33. /// Check whether this event is "the same" as another event for duplicate checking.
  34. /// </summary>
  35. public virtual bool IsDuplicate(DoAfterEvent other)
  36. {
  37. return GetType() == other.GetType();
  38. }
  39. }
  40. /// <summary>
  41. /// Blank / empty event for simple do afters that carry no information.
  42. /// </summary>
  43. /// <remarks>
  44. /// This just exists as a convenience to avoid having to re-implement Clone() for every simply DoAfterEvent.
  45. /// If an event actually contains data, it should actually override Clone().
  46. /// </remarks>
  47. [Serializable, NetSerializable]
  48. public abstract partial class SimpleDoAfterEvent : DoAfterEvent
  49. {
  50. // TODO: Find some way to enforce that inheritors don't store data?
  51. // Alternatively, I just need to allow generics to be networked.
  52. // E.g., then a SimpleDoAfter<TEvent> would just raise a TEvent event.
  53. // But afaik generic event types currently can't be serialized for networking or YAML.
  54. public override DoAfterEvent Clone() => this;
  55. }
  56. // Placeholder for obsolete async do afters
  57. [Serializable, NetSerializable]
  58. [Obsolete("Dont use async DoAfters")]
  59. public sealed partial class AwaitedDoAfterEvent : SimpleDoAfterEvent
  60. {
  61. }
  62. /// <summary>
  63. /// This event will optionally get raised every tick while a do-after is in progress to check whether the do-after
  64. /// should be canceled.
  65. /// </summary>
  66. public sealed partial class DoAfterAttemptEvent<TEvent> : CancellableEntityEventArgs where TEvent : DoAfterEvent
  67. {
  68. /// <summary>
  69. /// The do after that triggered this event.
  70. /// </summary>
  71. public readonly DoAfter DoAfter;
  72. /// <summary>
  73. /// The event that the DoAfter will raise after successfully finishing. Given that this event has the data
  74. /// required to perform the interaction, it should also contain the data required to validate/attempt the
  75. /// interaction.
  76. /// </summary>
  77. public readonly TEvent Event;
  78. public DoAfterAttemptEvent(DoAfter doAfter, TEvent @event)
  79. {
  80. DoAfter = doAfter;
  81. Event = @event;
  82. }
  83. }