1
0

GasOverlayChunk.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Content.Shared.Atmos.EntitySystems;
  2. using Robust.Shared.Serialization;
  3. using Robust.Shared.Timing;
  4. using Robust.Shared.Utility;
  5. using static Content.Shared.Atmos.EntitySystems.SharedGasTileOverlaySystem;
  6. namespace Content.Shared.Atmos
  7. {
  8. [Serializable, NetSerializable]
  9. [Access(typeof(SharedGasTileOverlaySystem))]
  10. public sealed class GasOverlayChunk
  11. {
  12. /// <summary>
  13. /// The index of this chunk
  14. /// </summary>
  15. public readonly Vector2i Index;
  16. public readonly Vector2i Origin;
  17. public GasOverlayData[] TileData = new GasOverlayData[ChunkSize * ChunkSize];
  18. [NonSerialized]
  19. public GameTick LastUpdate;
  20. public GasOverlayChunk(Vector2i index)
  21. {
  22. Index = index;
  23. Origin = Index * ChunkSize;
  24. }
  25. public GasOverlayChunk(GasOverlayChunk data)
  26. {
  27. Index = data.Index;
  28. Origin = data.Origin;
  29. // This does not clone the opacity array. However, this chunk cloning is only used by the client,
  30. // which never modifies that directly. So this should be fine.
  31. Array.Copy(data.TileData, TileData, data.TileData.Length);
  32. }
  33. /// <summary>
  34. /// Resolve a data index into <see cref="TileData"/> for the given grid index.
  35. /// </summary>
  36. public int GetDataIndex(Vector2i gridIndices)
  37. {
  38. DebugTools.Assert(InBounds(gridIndices));
  39. return (gridIndices.X - Origin.X) + (gridIndices.Y - Origin.Y) * ChunkSize;
  40. }
  41. private bool InBounds(Vector2i gridIndices)
  42. {
  43. return gridIndices.X >= Origin.X &&
  44. gridIndices.Y >= Origin.Y &&
  45. gridIndices.X < Origin.X + ChunkSize &&
  46. gridIndices.Y < Origin.Y + ChunkSize;
  47. }
  48. }
  49. public struct GasChunkEnumerator
  50. {
  51. private readonly GasOverlayData[] _tileData;
  52. private int _index = -1;
  53. public int X = ChunkSize - 1;
  54. public int Y = -1;
  55. public GasChunkEnumerator(GasOverlayChunk chunk)
  56. {
  57. _tileData = chunk.TileData;
  58. }
  59. public bool MoveNext(out GasOverlayData gas)
  60. {
  61. while (++_index < _tileData.Length)
  62. {
  63. X += 1;
  64. if (X >= ChunkSize)
  65. {
  66. X = 0;
  67. Y += 1;
  68. }
  69. gas = _tileData[_index];
  70. if (!gas.Equals(default))
  71. return true;
  72. }
  73. gas = default;
  74. return false;
  75. }
  76. }
  77. }