AtmosphereSystem.Map.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Content.Server.Atmos.Components;
  2. using Content.Shared.Atmos;
  3. using Content.Shared.Atmos.Components;
  4. using Robust.Shared.GameStates;
  5. using Robust.Shared.Map.Components;
  6. using Robust.Shared.Utility;
  7. namespace Content.Server.Atmos.EntitySystems;
  8. public partial class AtmosphereSystem
  9. {
  10. private void InitializeMap()
  11. {
  12. SubscribeLocalEvent<MapAtmosphereComponent, ComponentInit>(OnMapStartup);
  13. SubscribeLocalEvent<MapAtmosphereComponent, ComponentRemove>(OnMapRemove);
  14. SubscribeLocalEvent<MapAtmosphereComponent, ComponentGetState>(OnMapGetState);
  15. SubscribeLocalEvent<GridAtmosphereComponent, EntParentChangedMessage>(OnGridParentChanged);
  16. }
  17. private void OnMapStartup(EntityUid uid, MapAtmosphereComponent component, ComponentInit args)
  18. {
  19. component.Mixture.MarkImmutable();
  20. component.Overlay = _gasTileOverlaySystem.GetOverlayData(component.Mixture);
  21. }
  22. private void OnMapRemove(EntityUid uid, MapAtmosphereComponent component, ComponentRemove args)
  23. {
  24. if (!TerminatingOrDeleted(uid))
  25. RefreshAllGridMapAtmospheres(uid);
  26. }
  27. private void OnMapGetState(EntityUid uid, MapAtmosphereComponent component, ref ComponentGetState args)
  28. {
  29. args.State = new MapAtmosphereComponentState(component.Overlay);
  30. }
  31. public void SetMapAtmosphere(EntityUid uid, bool space, GasMixture mixture)
  32. {
  33. DebugTools.Assert(HasComp<MapComponent>(uid));
  34. var component = EnsureComp<MapAtmosphereComponent>(uid);
  35. SetMapGasMixture(uid, mixture, component, false);
  36. SetMapSpace(uid, space, component, false);
  37. RefreshAllGridMapAtmospheres(uid);
  38. }
  39. public void SetMapGasMixture(EntityUid uid, GasMixture mixture, MapAtmosphereComponent? component = null, bool updateTiles = true)
  40. {
  41. if (!Resolve(uid, ref component))
  42. return;
  43. if (!mixture.Immutable)
  44. {
  45. mixture = mixture.Clone();
  46. mixture.MarkImmutable();
  47. }
  48. component.Mixture = mixture;
  49. component.Overlay = _gasTileOverlaySystem.GetOverlayData(component.Mixture);
  50. Dirty(uid, component);
  51. if (updateTiles)
  52. RefreshAllGridMapAtmospheres(uid);
  53. }
  54. public void SetMapSpace(EntityUid uid, bool space, MapAtmosphereComponent? component = null, bool updateTiles = true)
  55. {
  56. if (!Resolve(uid, ref component))
  57. return;
  58. if (component.Space == space)
  59. return;
  60. component.Space = space;
  61. if (updateTiles)
  62. RefreshAllGridMapAtmospheres(uid);
  63. }
  64. /// <summary>
  65. /// Forces a refresh of all MapAtmosphere tiles on every grid on a map.
  66. /// </summary>
  67. public void RefreshAllGridMapAtmospheres(EntityUid map)
  68. {
  69. DebugTools.Assert(HasComp<MapComponent>(map));
  70. var enumerator = AllEntityQuery<GridAtmosphereComponent, TransformComponent>();
  71. while (enumerator.MoveNext(out var grid, out var atmos, out var xform))
  72. {
  73. if (xform.MapUid == map)
  74. RefreshMapAtmosphereTiles((grid, atmos));
  75. }
  76. }
  77. /// <summary>
  78. /// Forces a refresh of all MapAtmosphere tiles on a given grid.
  79. /// </summary>
  80. private void RefreshMapAtmosphereTiles(Entity<GridAtmosphereComponent?> grid)
  81. {
  82. if (!Resolve(grid.Owner, ref grid.Comp))
  83. return;
  84. var atmos = grid.Comp;
  85. foreach (var tile in atmos.MapTiles)
  86. {
  87. RemoveMapAtmos(atmos, tile);
  88. atmos.InvalidatedCoords.Add(tile.GridIndices);
  89. }
  90. atmos.MapTiles.Clear();
  91. }
  92. /// <summary>
  93. /// Handles updating map-atmospheres when grids move across maps.
  94. /// </summary>
  95. private void OnGridParentChanged(Entity<GridAtmosphereComponent> grid, ref EntParentChangedMessage args)
  96. {
  97. // Do nothing if detaching to nullspace
  98. if (!args.Transform.ParentUid.IsValid())
  99. return;
  100. // Avoid doing work if moving from a space-map to another space-map.
  101. if (args.OldParent == null
  102. || HasComp<MapAtmosphereComponent>(args.OldParent)
  103. || HasComp<MapAtmosphereComponent>(args.Transform.ParentUid))
  104. {
  105. RefreshMapAtmosphereTiles((grid, grid));
  106. }
  107. }
  108. }