SingularityAttractorSystem.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Content.Server.Physics.Components;
  2. using Content.Server.Power.EntitySystems;
  3. using Content.Server.Singularity.Components;
  4. using Content.Shared.Singularity.Components;
  5. using Robust.Shared.Map;
  6. using Robust.Shared.Timing;
  7. using System.Numerics;
  8. namespace Content.Server.Singularity.EntitySystems;
  9. /// <summary>
  10. /// Handles singularity attractors.
  11. /// </summary>
  12. public sealed class SingularityAttractorSystem : EntitySystem
  13. {
  14. [Dependency] private readonly IGameTiming _timing = default!;
  15. [Dependency] private readonly SharedTransformSystem _transform = default!;
  16. /// <summary>
  17. /// The minimum range at which the attraction will act.
  18. /// Prevents division by zero problems.
  19. /// </summary>
  20. public const float MinAttractRange = 0.00001f;
  21. public override void Initialize()
  22. {
  23. base.Initialize();
  24. SubscribeLocalEvent<SingularityAttractorComponent, MapInitEvent>(OnMapInit);
  25. }
  26. /// <summary>
  27. /// Updates the pulse cooldowns of all singularity attractors.
  28. /// If they are off cooldown it makes them emit an attraction pulse and reset their cooldown.
  29. /// </summary>
  30. /// <param name="frameTime">The time elapsed since the last set of updates.</param>
  31. public override void Update(float frameTime)
  32. {
  33. if (!_timing.IsFirstTimePredicted)
  34. return;
  35. var query = EntityQueryEnumerator<SingularityAttractorComponent, TransformComponent>();
  36. var now = _timing.CurTime;
  37. while (query.MoveNext(out var uid, out var attractor, out var xform))
  38. {
  39. if (attractor.LastPulseTime + attractor.TargetPulsePeriod <= now)
  40. Update(uid, attractor, xform);
  41. }
  42. }
  43. /// <summary>
  44. /// Makes an attractor attract all singularities and puts it on cooldown.
  45. /// </summary>
  46. /// <param name="uid">The uid of the attractor to make pulse.</param>
  47. /// <param name="attractor">The state of the attractor to make pulse.</param>
  48. /// <param name="xform">The transform of the attractor to make pulse.</param>
  49. private void Update(EntityUid uid, SingularityAttractorComponent? attractor = null, TransformComponent? xform = null)
  50. {
  51. if (!Resolve(uid, ref attractor, ref xform))
  52. return;
  53. if (!this.IsPowered(uid, EntityManager))
  54. return;
  55. attractor.LastPulseTime = _timing.CurTime;
  56. var mapPos = xform.Coordinates.ToMap(EntityManager, _transform);
  57. if (mapPos == MapCoordinates.Nullspace)
  58. return;
  59. var query = EntityQuery<SingularityComponent, RandomWalkComponent, TransformComponent>();
  60. foreach (var (singulo, walk, singuloXform) in query)
  61. {
  62. var singuloMapPos = singuloXform.Coordinates.ToMap(EntityManager, _transform);
  63. if (singuloMapPos.MapId != mapPos.MapId)
  64. continue;
  65. var biasBy = mapPos.Position - singuloMapPos.Position;
  66. var length = biasBy.Length();
  67. if (length <= MinAttractRange)
  68. return;
  69. biasBy = Vector2.Normalize(biasBy) * (attractor.BaseRange / length);
  70. walk.BiasVector += biasBy;
  71. }
  72. }
  73. /// <summary>
  74. /// Resets the pulse timings of the attractor when the component starts up.
  75. /// </summary>
  76. /// <param name="uid">The uid of the attractor to start up.</param>
  77. /// <param name="comp">The state of the attractor to start up.</param>
  78. /// <param name="args">The startup prompt arguments.</param>
  79. private void OnMapInit(Entity<SingularityAttractorComponent> ent, ref MapInitEvent args)
  80. {
  81. ent.Comp.LastPulseTime = _timing.CurTime;
  82. }
  83. }