EntityAnomalySystem.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Content.Shared.Anomaly;
  2. using Content.Shared.Anomaly.Components;
  3. using Content.Shared.Anomaly.Effects;
  4. using Content.Shared.Anomaly.Effects.Components;
  5. using Robust.Shared.Map.Components;
  6. using Robust.Shared.Physics.Components;
  7. using Robust.Shared.Random;
  8. namespace Content.Server.Anomaly.Effects;
  9. public sealed class EntityAnomalySystem : SharedEntityAnomalySystem
  10. {
  11. [Dependency] private readonly SharedAnomalySystem _anomaly = default!;
  12. [Dependency] private readonly IRobustRandom _random = default!;
  13. [Dependency] private readonly SharedMapSystem _mapSystem = default!;
  14. private EntityQuery<PhysicsComponent> _physicsQuery;
  15. /// <inheritdoc/>
  16. public override void Initialize()
  17. {
  18. _physicsQuery = GetEntityQuery<PhysicsComponent>();
  19. SubscribeLocalEvent<EntitySpawnAnomalyComponent, AnomalyPulseEvent>(OnPulse);
  20. SubscribeLocalEvent<EntitySpawnAnomalyComponent, AnomalySupercriticalEvent>(OnSupercritical);
  21. SubscribeLocalEvent<EntitySpawnAnomalyComponent, AnomalyStabilityChangedEvent>(OnStabilityChanged);
  22. SubscribeLocalEvent<EntitySpawnAnomalyComponent, AnomalySeverityChangedEvent>(OnSeverityChanged);
  23. SubscribeLocalEvent<EntitySpawnAnomalyComponent, AnomalyShutdownEvent>(OnShutdown);
  24. }
  25. private void OnPulse(Entity<EntitySpawnAnomalyComponent> component, ref AnomalyPulseEvent args)
  26. {
  27. foreach (var entry in component.Comp.Entries)
  28. {
  29. if (!entry.Settings.SpawnOnPulse)
  30. continue;
  31. SpawnEntities(component, entry, args.Stability, args.Severity, args.PowerModifier);
  32. }
  33. }
  34. private void OnSupercritical(Entity<EntitySpawnAnomalyComponent> component, ref AnomalySupercriticalEvent args)
  35. {
  36. foreach (var entry in component.Comp.Entries)
  37. {
  38. if (!entry.Settings.SpawnOnSuperCritical)
  39. continue;
  40. SpawnEntities(component, entry, 1, 1, args.PowerModifier);
  41. }
  42. }
  43. private void OnShutdown(Entity<EntitySpawnAnomalyComponent> component, ref AnomalyShutdownEvent args)
  44. {
  45. foreach (var entry in component.Comp.Entries)
  46. {
  47. if (!entry.Settings.SpawnOnShutdown || args.Supercritical)
  48. continue;
  49. SpawnEntities(component, entry, 1, 1, 1);
  50. }
  51. }
  52. private void OnStabilityChanged(Entity<EntitySpawnAnomalyComponent> component, ref AnomalyStabilityChangedEvent args)
  53. {
  54. foreach (var entry in component.Comp.Entries)
  55. {
  56. if (!entry.Settings.SpawnOnStabilityChanged)
  57. continue;
  58. SpawnEntities(component, entry, args.Stability, args.Severity, 1);
  59. }
  60. }
  61. private void OnSeverityChanged(Entity<EntitySpawnAnomalyComponent> component, ref AnomalySeverityChangedEvent args)
  62. {
  63. foreach (var entry in component.Comp.Entries)
  64. {
  65. if (!entry.Settings.SpawnOnSeverityChanged)
  66. continue;
  67. SpawnEntities(component, entry, args.Stability, args.Severity, 1);
  68. }
  69. }
  70. private void SpawnEntities(Entity<EntitySpawnAnomalyComponent> anomaly, EntitySpawnSettingsEntry entry, float stability, float severity, float powerMod)
  71. {
  72. var xform = Transform(anomaly);
  73. if (!TryComp(xform.GridUid, out MapGridComponent? grid))
  74. return;
  75. var tiles = _anomaly.GetSpawningPoints(anomaly, stability, severity, entry.Settings, powerMod);
  76. if (tiles == null)
  77. return;
  78. foreach (var tileref in tiles)
  79. {
  80. Spawn(_random.Pick(entry.Spawns), _mapSystem.ToCenterCoordinates(tileref, grid));
  81. }
  82. }
  83. }