1
0

TriggerSystem.Proximity.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using Content.Server.Explosion.Components;
  2. using Content.Shared.Trigger;
  3. using Robust.Shared.Physics.Components;
  4. using Robust.Shared.Physics.Events;
  5. using Robust.Shared.Utility;
  6. using Robust.Shared.Timing;
  7. namespace Content.Server.Explosion.EntitySystems;
  8. public sealed partial class TriggerSystem
  9. {
  10. [Dependency] private readonly IGameTiming _timing = default!;
  11. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  12. private void InitializeProximity()
  13. {
  14. SubscribeLocalEvent<TriggerOnProximityComponent, StartCollideEvent>(OnProximityStartCollide);
  15. SubscribeLocalEvent<TriggerOnProximityComponent, EndCollideEvent>(OnProximityEndCollide);
  16. SubscribeLocalEvent<TriggerOnProximityComponent, MapInitEvent>(OnMapInit);
  17. SubscribeLocalEvent<TriggerOnProximityComponent, ComponentShutdown>(OnProximityShutdown);
  18. // Shouldn't need re-anchoring.
  19. SubscribeLocalEvent<TriggerOnProximityComponent, AnchorStateChangedEvent>(OnProximityAnchor);
  20. }
  21. private void OnProximityAnchor(EntityUid uid, TriggerOnProximityComponent component, ref AnchorStateChangedEvent args)
  22. {
  23. component.Enabled = !component.RequiresAnchored ||
  24. args.Anchored;
  25. SetProximityAppearance(uid, component);
  26. if (!component.Enabled)
  27. {
  28. component.Colliding.Clear();
  29. }
  30. // Re-check for contacts as we cleared them.
  31. else if (TryComp<PhysicsComponent>(uid, out var body))
  32. {
  33. _broadphase.RegenerateContacts((uid, body));
  34. }
  35. }
  36. private void OnProximityShutdown(EntityUid uid, TriggerOnProximityComponent component, ComponentShutdown args)
  37. {
  38. component.Colliding.Clear();
  39. }
  40. private void OnMapInit(EntityUid uid, TriggerOnProximityComponent component, MapInitEvent args)
  41. {
  42. component.Enabled = !component.RequiresAnchored ||
  43. Transform(uid).Anchored;
  44. SetProximityAppearance(uid, component);
  45. if (!TryComp<PhysicsComponent>(uid, out var body))
  46. return;
  47. _fixtures.TryCreateFixture(
  48. uid,
  49. component.Shape,
  50. TriggerOnProximityComponent.FixtureID,
  51. hard: false,
  52. body: body,
  53. collisionLayer: component.Layer);
  54. }
  55. private void OnProximityStartCollide(EntityUid uid, TriggerOnProximityComponent component, ref StartCollideEvent args)
  56. {
  57. if (args.OurFixtureId != TriggerOnProximityComponent.FixtureID)
  58. return;
  59. component.Colliding[args.OtherEntity] = args.OtherBody;
  60. }
  61. private static void OnProximityEndCollide(EntityUid uid, TriggerOnProximityComponent component, ref EndCollideEvent args)
  62. {
  63. if (args.OurFixtureId != TriggerOnProximityComponent.FixtureID)
  64. return;
  65. component.Colliding.Remove(args.OtherEntity);
  66. }
  67. private void SetProximityAppearance(EntityUid uid, TriggerOnProximityComponent component)
  68. {
  69. if (EntityManager.TryGetComponent(uid, out AppearanceComponent? appearance))
  70. {
  71. _appearance.SetData(uid, ProximityTriggerVisualState.State, component.Enabled ? ProximityTriggerVisuals.Inactive : ProximityTriggerVisuals.Off, appearance);
  72. }
  73. }
  74. private void Activate(EntityUid uid, EntityUid user, TriggerOnProximityComponent component)
  75. {
  76. DebugTools.Assert(component.Enabled);
  77. var curTime = _timing.CurTime;
  78. if (!component.Repeating)
  79. {
  80. component.Enabled = false;
  81. component.Colliding.Clear();
  82. }
  83. else
  84. {
  85. component.NextTrigger = curTime + component.Cooldown;
  86. }
  87. // Queue a visual update for when the animation is complete.
  88. component.NextVisualUpdate = curTime + component.AnimationDuration;
  89. if (EntityManager.TryGetComponent(uid, out AppearanceComponent? appearance))
  90. {
  91. _appearance.SetData(uid, ProximityTriggerVisualState.State, ProximityTriggerVisuals.Active, appearance);
  92. }
  93. Trigger(uid, user);
  94. }
  95. private void UpdateProximity()
  96. {
  97. var curTime = _timing.CurTime;
  98. var query = EntityQueryEnumerator<TriggerOnProximityComponent>();
  99. while (query.MoveNext(out var uid, out var trigger))
  100. {
  101. if (curTime >= trigger.NextVisualUpdate)
  102. {
  103. // Update the visual state once the animation is done.
  104. trigger.NextVisualUpdate = TimeSpan.MaxValue;
  105. SetProximityAppearance(uid, trigger);
  106. }
  107. if (!trigger.Enabled)
  108. continue;
  109. if (curTime < trigger.NextTrigger)
  110. // The trigger's on cooldown.
  111. continue;
  112. // Check for anything colliding and moving fast enough.
  113. foreach (var (collidingUid, colliding) in trigger.Colliding)
  114. {
  115. if (Deleted(collidingUid))
  116. continue;
  117. if (colliding.LinearVelocity.Length() < trigger.TriggerSpeed)
  118. continue;
  119. // Trigger!
  120. Activate(uid, collidingUid, trigger);
  121. break;
  122. }
  123. }
  124. }
  125. }