StepTriggerSystem.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using Content.Shared.Gravity;
  2. using Content.Shared.StepTrigger.Components;
  3. using Content.Shared.Whitelist;
  4. using Robust.Shared.Map.Components;
  5. using Robust.Shared.Physics;
  6. using Robust.Shared.Physics.Components;
  7. using Robust.Shared.Physics.Events;
  8. namespace Content.Shared.StepTrigger.Systems;
  9. public sealed class StepTriggerSystem : EntitySystem
  10. {
  11. [Dependency] private readonly EntityLookupSystem _entityLookup = default!;
  12. [Dependency] private readonly SharedGravitySystem _gravity = default!;
  13. [Dependency] private readonly SharedMapSystem _map = default!;
  14. [Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
  15. public override void Initialize()
  16. {
  17. UpdatesOutsidePrediction = true;
  18. SubscribeLocalEvent<StepTriggerComponent, AfterAutoHandleStateEvent>(TriggerHandleState);
  19. SubscribeLocalEvent<StepTriggerComponent, StartCollideEvent>(OnStartCollide);
  20. SubscribeLocalEvent<StepTriggerComponent, EndCollideEvent>(OnEndCollide);
  21. #if DEBUG
  22. SubscribeLocalEvent<StepTriggerComponent, ComponentStartup>(OnStartup);
  23. }
  24. private void OnStartup(EntityUid uid, StepTriggerComponent component, ComponentStartup args)
  25. {
  26. if (!component.Active)
  27. return;
  28. if (!TryComp(uid, out FixturesComponent? fixtures) || fixtures.FixtureCount == 0)
  29. Log.Warning($"{ToPrettyString(uid)} has an active step trigger without any fixtures.");
  30. #endif
  31. }
  32. public override void Update(float frameTime)
  33. {
  34. var query = GetEntityQuery<PhysicsComponent>();
  35. var enumerator = EntityQueryEnumerator<StepTriggerActiveComponent, StepTriggerComponent, TransformComponent>();
  36. while (enumerator.MoveNext(out var uid, out var active, out var trigger, out var transform))
  37. {
  38. if (!Update(uid, trigger, transform, query))
  39. {
  40. continue;
  41. }
  42. RemCompDeferred(uid, active);
  43. }
  44. }
  45. private bool Update(EntityUid uid, StepTriggerComponent component, TransformComponent transform, EntityQuery<PhysicsComponent> query)
  46. {
  47. if (!component.Active ||
  48. component.Colliding.Count == 0)
  49. {
  50. return true;
  51. }
  52. if (component.Blacklist != null && TryComp<MapGridComponent>(transform.GridUid, out var grid))
  53. {
  54. var positon = _map.LocalToTile(transform.GridUid.Value, grid, transform.Coordinates);
  55. var anch = _map.GetAnchoredEntitiesEnumerator(uid, grid, positon);
  56. while (anch.MoveNext(out var ent))
  57. {
  58. if (ent == uid)
  59. continue;
  60. if (_whitelistSystem.IsBlacklistPass(component.Blacklist, ent.Value))
  61. {
  62. return false;
  63. }
  64. }
  65. }
  66. foreach (var otherUid in component.Colliding)
  67. {
  68. UpdateColliding(uid, component, transform, otherUid, query);
  69. }
  70. return false;
  71. }
  72. private void UpdateColliding(EntityUid uid, StepTriggerComponent component, TransformComponent ownerXform, EntityUid otherUid, EntityQuery<PhysicsComponent> query)
  73. {
  74. if (!query.TryGetComponent(otherUid, out var otherPhysics))
  75. return;
  76. var otherXform = Transform(otherUid);
  77. // TODO: This shouldn't be calculating based on world AABBs.
  78. var ourAabb = _entityLookup.GetAABBNoContainer(uid, ownerXform.LocalPosition, ownerXform.LocalRotation);
  79. var otherAabb = _entityLookup.GetAABBNoContainer(otherUid, otherXform.LocalPosition, otherXform.LocalRotation);
  80. if (!ourAabb.Intersects(otherAabb))
  81. {
  82. if (component.CurrentlySteppedOn.Remove(otherUid))
  83. {
  84. Dirty(uid, component);
  85. }
  86. return;
  87. }
  88. // max 'area of enclosure' between the two aabbs
  89. // this is hard to explain
  90. var intersect = Box2.Area(otherAabb.Intersect(ourAabb));
  91. var ratio = Math.Max(intersect / Box2.Area(otherAabb), intersect / Box2.Area(ourAabb));
  92. if (otherPhysics.LinearVelocity.Length() < component.RequiredTriggeredSpeed
  93. || component.CurrentlySteppedOn.Contains(otherUid)
  94. || ratio < component.IntersectRatio
  95. || !CanTrigger(uid, otherUid, component))
  96. {
  97. return;
  98. }
  99. if (component.StepOn)
  100. {
  101. var evStep = new StepTriggeredOnEvent(uid, otherUid);
  102. RaiseLocalEvent(uid, ref evStep);
  103. }
  104. else
  105. {
  106. var evStep = new StepTriggeredOffEvent(uid, otherUid);
  107. RaiseLocalEvent(uid, ref evStep);
  108. }
  109. component.CurrentlySteppedOn.Add(otherUid);
  110. Dirty(uid, component);
  111. }
  112. private bool CanTrigger(EntityUid uid, EntityUid otherUid, StepTriggerComponent component)
  113. {
  114. if (!component.Active || component.CurrentlySteppedOn.Contains(otherUid))
  115. return false;
  116. // Can't trigger if we don't ignore weightless entities
  117. // and the entity is flying or currently weightless
  118. // Makes sense simulation wise to have this be part of steptrigger directly IMO
  119. if (!component.IgnoreWeightless && TryComp<PhysicsComponent>(otherUid, out var physics) &&
  120. (physics.BodyStatus == BodyStatus.InAir || _gravity.IsWeightless(otherUid, physics)))
  121. return false;
  122. var msg = new StepTriggerAttemptEvent { Source = uid, Tripper = otherUid };
  123. RaiseLocalEvent(uid, ref msg);
  124. return msg.Continue && !msg.Cancelled;
  125. }
  126. private void OnStartCollide(EntityUid uid, StepTriggerComponent component, ref StartCollideEvent args)
  127. {
  128. var otherUid = args.OtherEntity;
  129. if (!args.OtherFixture.Hard)
  130. return;
  131. if (!CanTrigger(uid, otherUid, component))
  132. return;
  133. EnsureComp<StepTriggerActiveComponent>(uid);
  134. if (component.Colliding.Add(otherUid))
  135. {
  136. Dirty(uid, component);
  137. }
  138. }
  139. private void OnEndCollide(EntityUid uid, StepTriggerComponent component, ref EndCollideEvent args)
  140. {
  141. var otherUid = args.OtherEntity;
  142. if (!component.Colliding.Remove(otherUid))
  143. return;
  144. component.CurrentlySteppedOn.Remove(otherUid);
  145. Dirty(uid, component);
  146. if (component.StepOn)
  147. {
  148. var evStepOff = new StepTriggeredOffEvent(uid, otherUid);
  149. RaiseLocalEvent(uid, ref evStepOff);
  150. }
  151. if (component.Colliding.Count == 0)
  152. {
  153. RemCompDeferred<StepTriggerActiveComponent>(uid);
  154. }
  155. }
  156. private void TriggerHandleState(EntityUid uid, StepTriggerComponent component, ref AfterAutoHandleStateEvent args)
  157. {
  158. if (component.Colliding.Count > 0)
  159. {
  160. EnsureComp<StepTriggerActiveComponent>(uid);
  161. }
  162. else
  163. {
  164. RemCompDeferred<StepTriggerActiveComponent>(uid);
  165. }
  166. }
  167. public void SetIntersectRatio(EntityUid uid, float ratio, StepTriggerComponent? component = null)
  168. {
  169. if (!Resolve(uid, ref component))
  170. return;
  171. if (MathHelper.CloseToPercent(component.IntersectRatio, ratio))
  172. return;
  173. component.IntersectRatio = ratio;
  174. Dirty(uid, component);
  175. }
  176. public void SetRequiredTriggerSpeed(EntityUid uid, float speed, StepTriggerComponent? component = null)
  177. {
  178. if (!Resolve(uid, ref component))
  179. return;
  180. if (MathHelper.CloseToPercent(component.RequiredTriggeredSpeed, speed))
  181. return;
  182. component.RequiredTriggeredSpeed = speed;
  183. Dirty(uid, component);
  184. }
  185. public void SetActive(EntityUid uid, bool active, StepTriggerComponent? component = null)
  186. {
  187. if (!Resolve(uid, ref component))
  188. return;
  189. if (active == component.Active)
  190. return;
  191. component.Active = active;
  192. Dirty(uid, component);
  193. }
  194. }
  195. [ByRefEvent]
  196. public struct StepTriggerAttemptEvent
  197. {
  198. public EntityUid Source;
  199. public EntityUid Tripper;
  200. public bool Continue;
  201. /// <summary>
  202. /// Set by systems which wish to cancel the step trigger event, regardless of event ordering.
  203. /// </summary>
  204. public bool Cancelled;
  205. }
  206. /// <summary>
  207. /// Raised when an entity stands on a steptrigger initially (assuming it has both on and off states).
  208. /// </summary>
  209. [ByRefEvent]
  210. public readonly record struct StepTriggeredOnEvent(EntityUid Source, EntityUid Tripper);
  211. /// <summary>
  212. /// Raised when an entity leaves a steptrigger if it has on and off states OR when an entity intersects a steptrigger.
  213. /// </summary>
  214. [ByRefEvent]
  215. public readonly record struct StepTriggeredOffEvent(EntityUid Source, EntityUid Tripper);