1
0

MobStateSystem.StateMachine.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Content.Shared.Database;
  2. using Content.Shared.Mobs.Components;
  3. using Content.Shared._Shitmed.Body.Organ;
  4. namespace Content.Shared.Mobs.Systems;
  5. public partial class MobStateSystem
  6. {
  7. #region Public API
  8. /// <summary>
  9. /// Check if an Entity can be set to a particular MobState
  10. /// </summary>
  11. /// <param name="entity">Target Entity</param>
  12. /// <param name="mobState">MobState to check</param>
  13. /// <param name="component">MobState Component owned by the target</param>
  14. /// <returns>If the entity can be set to that MobState</returns>
  15. public bool HasState(EntityUid entity, MobState mobState, MobStateComponent? component = null)
  16. {
  17. return _mobStateQuery.Resolve(entity, ref component, false) &&
  18. component.AllowedStates.Contains(mobState);
  19. }
  20. /// <summary>
  21. /// Run a MobState update check. This will trigger update events if the state has been changed.
  22. /// </summary>
  23. /// <param name="entity">Target Entity we want to change the MobState of</param>
  24. /// <param name="component">MobState Component attached to the entity</param>
  25. /// <param name="origin">Entity that caused the state update (if applicable)</param>
  26. public void UpdateMobState(EntityUid entity, MobStateComponent? component = null, EntityUid? origin = null)
  27. {
  28. if (!_mobStateQuery.Resolve(entity, ref component))
  29. return;
  30. var ev = new UpdateMobStateEvent { Target = entity, Component = component, Origin = origin };
  31. RaiseLocalEvent(entity, ref ev);
  32. ChangeState(entity, component, ev.State, origin: origin);
  33. }
  34. /// <summary>
  35. /// Change the MobState without triggering UpdateMobState events.
  36. /// WARNING: use this sparingly when you need to override other systems (MobThresholds)
  37. /// </summary>
  38. /// <param name="entity">Target Entity we want to change the MobState of</param>
  39. /// <param name="mobState">The new MobState we want to set</param>
  40. /// <param name="component">MobState Component attached to the entity</param>
  41. /// <param name="origin">Entity that caused the state update (if applicable)</param>
  42. public void ChangeMobState(EntityUid entity, MobState mobState, MobStateComponent? component = null,
  43. EntityUid? origin = null)
  44. {
  45. if (!_mobStateQuery.Resolve(entity, ref component))
  46. return;
  47. ChangeState(entity, component, mobState, origin: origin);
  48. }
  49. #endregion
  50. #region Virtual API
  51. /// <summary>
  52. /// Called when a new MobState is entered.
  53. /// </summary>
  54. /// <param name="entity">The owner of the MobState Component</param>
  55. /// <param name="component">MobState Component owned by the target</param>
  56. /// <param name="state">The new MobState</param>
  57. protected virtual void OnEnterState(EntityUid entity, MobStateComponent component, MobState state)
  58. {
  59. OnStateEnteredSubscribers(entity, component, state);
  60. }
  61. /// <summary>
  62. /// Called when this entity changes MobState
  63. /// </summary>
  64. /// <param name="entity">The owner of the MobState Component</param>
  65. /// <param name="component">MobState Component owned by the target</param>
  66. /// <param name="oldState">The previous MobState</param>
  67. /// <param name="newState">The new MobState</param>
  68. protected virtual void OnStateChanged(EntityUid entity, MobStateComponent component, MobState oldState,
  69. MobState newState)
  70. {
  71. }
  72. /// <summary>
  73. /// Called when a new MobState is exited.
  74. /// </summary>
  75. /// <param name="entity">The owner of the MobState Component</param>
  76. /// <param name="component">MobState Component owned by the target</param>
  77. /// <param name="state">The old MobState</param>
  78. protected virtual void OnExitState(EntityUid entity, MobStateComponent component, MobState state)
  79. {
  80. OnStateExitSubscribers(entity, component, state);
  81. }
  82. #endregion
  83. #region Private Implementation
  84. //Actually change the MobState
  85. private void ChangeState(EntityUid target, MobStateComponent component, MobState newState, EntityUid? origin = null)
  86. {
  87. var oldState = component.CurrentState;
  88. //make sure we are allowed to enter the new state
  89. if (oldState == newState || !component.AllowedStates.Contains(newState))
  90. return;
  91. if (oldState == MobState.Dead && HasComp<DebrainedComponent>(target)) // Shitmed Change
  92. return;
  93. OnExitState(target, component, oldState);
  94. component.CurrentState = newState;
  95. OnEnterState(target, component, newState);
  96. var ev = new MobStateChangedEvent(target, component, oldState, newState, origin);
  97. OnStateChanged(target, component, oldState, newState);
  98. RaiseLocalEvent(target, ev, true);
  99. _adminLogger.Add(LogType.Damaged, oldState == MobState.Alive ? LogImpact.Low : LogImpact.Medium,
  100. $"{ToPrettyString(target):user} state changed from {oldState} to {newState}");
  101. Dirty(target, component);
  102. }
  103. #endregion
  104. }
  105. /// <summary>
  106. /// Event that gets triggered when we want to update the mobstate. This allows for systems to override MobState changes
  107. /// </summary>
  108. /// <param name="Target">The Entity whose MobState is changing</param>
  109. /// <param name="Component">The MobState Component owned by the Target</param>
  110. /// <param name="State">The new MobState we want to set</param>
  111. /// <param name="Origin">Entity that caused the state update (if applicable)</param>
  112. [ByRefEvent]
  113. public record struct UpdateMobStateEvent(EntityUid Target, MobStateComponent Component, MobState State,
  114. EntityUid? Origin = null);