TileAnomalySystem.cs 3.5 KB

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