GasTileOverlayComponent.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using Robust.Shared.GameStates;
  2. using Robust.Shared.Serialization;
  3. using Robust.Shared.Timing;
  4. namespace Content.Shared.Atmos.Components;
  5. [RegisterComponent, NetworkedComponent]
  6. public sealed partial class GasTileOverlayComponent : Component
  7. {
  8. /// <summary>
  9. /// The tiles that have had their atmos data updated since last tick
  10. /// </summary>
  11. public readonly HashSet<Vector2i> InvalidTiles = new();
  12. /// <summary>
  13. /// Gas data stored in chunks to make PVS / bubbling easier.
  14. /// </summary>
  15. public readonly Dictionary<Vector2i, GasOverlayChunk> Chunks = new();
  16. /// <summary>
  17. /// Tick at which PVS was last toggled. Ensures that all players receive a full update when toggling PVS.
  18. /// </summary>
  19. public GameTick ForceTick { get; set; }
  20. }
  21. [Serializable, NetSerializable]
  22. public sealed class GasTileOverlayState(Dictionary<Vector2i, GasOverlayChunk> chunks) : ComponentState
  23. {
  24. public readonly Dictionary<Vector2i, GasOverlayChunk> Chunks = chunks;
  25. }
  26. [Serializable, NetSerializable]
  27. public sealed class GasTileOverlayDeltaState(
  28. Dictionary<Vector2i, GasOverlayChunk> modifiedChunks,
  29. HashSet<Vector2i> allChunks)
  30. : ComponentState, IComponentDeltaState<GasTileOverlayState>
  31. {
  32. public readonly Dictionary<Vector2i, GasOverlayChunk> ModifiedChunks = modifiedChunks;
  33. public readonly HashSet<Vector2i> AllChunks = allChunks;
  34. public void ApplyToFullState(GasTileOverlayState state)
  35. {
  36. foreach (var key in state.Chunks.Keys)
  37. {
  38. if (!AllChunks.Contains(key))
  39. state.Chunks.Remove(key);
  40. }
  41. foreach (var (chunk, data) in ModifiedChunks)
  42. {
  43. state.Chunks[chunk] = new(data);
  44. }
  45. }
  46. public GasTileOverlayState CreateNewFullState(GasTileOverlayState state)
  47. {
  48. var chunks = new Dictionary<Vector2i, GasOverlayChunk>(AllChunks.Count);
  49. foreach (var (chunk, data) in ModifiedChunks)
  50. {
  51. chunks[chunk] = new(data);
  52. }
  53. foreach (var (chunk, data) in state.Chunks)
  54. {
  55. if (AllChunks.Contains(chunk))
  56. chunks.TryAdd(chunk, new(data));
  57. }
  58. return new GasTileOverlayState(chunks);
  59. }
  60. }