Gas tile overlay rejig (#9619)
This commit is contained in:
@@ -1,99 +1,97 @@
|
||||
using Content.Shared.Atmos.EntitySystems;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.Utility;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using static Content.Shared.Atmos.EntitySystems.SharedGasTileOverlaySystem;
|
||||
|
||||
namespace Content.Shared.Atmos
|
||||
{
|
||||
[Serializable, NetSerializable]
|
||||
[Access(typeof(SharedGasTileOverlaySystem))]
|
||||
public sealed class GasOverlayChunk
|
||||
{
|
||||
/// <summary>
|
||||
/// Grid for this chunk
|
||||
/// The index of this chunk
|
||||
/// </summary>
|
||||
public EntityUid GridIndices { get; }
|
||||
public readonly Vector2i Index;
|
||||
public readonly Vector2i Origin;
|
||||
|
||||
/// <summary>
|
||||
/// Origin of this chunk
|
||||
/// </summary>
|
||||
public Vector2i Vector2i { get; }
|
||||
public GasOverlayData?[][] TileData = new GasOverlayData?[ChunkSize][];
|
||||
|
||||
public SharedGasTileOverlaySystem.GasOverlayData[,] TileData = new SharedGasTileOverlaySystem.GasOverlayData[SharedGasTileOverlaySystem.ChunkSize, SharedGasTileOverlaySystem.ChunkSize];
|
||||
[NonSerialized]
|
||||
public GameTick LastUpdate;
|
||||
|
||||
public GameTick LastUpdate { get; private set; }
|
||||
|
||||
public GasOverlayChunk(EntityUid gridIndices, Vector2i vector2i)
|
||||
public GasOverlayChunk(Vector2i index)
|
||||
{
|
||||
GridIndices = gridIndices;
|
||||
Vector2i = vector2i;
|
||||
}
|
||||
Index = index;
|
||||
Origin = Index * ChunkSize;
|
||||
|
||||
public void Dirty(GameTick currentTick)
|
||||
{
|
||||
LastUpdate = currentTick;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flags Dirty if the data is different.
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="indices"></param>
|
||||
public void Update(SharedGasTileOverlaySystem.GasOverlayData data, Vector2i indices)
|
||||
{
|
||||
DebugTools.Assert(InBounds(indices));
|
||||
var (offsetX, offsetY) = (indices.X - Vector2i.X,
|
||||
indices.Y - Vector2i.Y);
|
||||
|
||||
TileData[offsetX, offsetY] = data;
|
||||
}
|
||||
|
||||
public void Update(SharedGasTileOverlaySystem.GasOverlayData data, byte x, byte y)
|
||||
{
|
||||
DebugTools.Assert(x < SharedGasTileOverlaySystem.ChunkSize && y < SharedGasTileOverlaySystem.ChunkSize);
|
||||
|
||||
TileData[x, y] = data;
|
||||
}
|
||||
|
||||
public IEnumerable<SharedGasTileOverlaySystem.GasOverlayData> GetAllData()
|
||||
{
|
||||
for (var x = 0; x < SharedGasTileOverlaySystem.ChunkSize; x++)
|
||||
// For whatever reason, net serialize does not like multi_D arrays. So Jagged it is.
|
||||
for (var i = 0; i < ChunkSize; i++)
|
||||
{
|
||||
for (var y = 0; y < SharedGasTileOverlaySystem.ChunkSize; y++)
|
||||
TileData[i] = new GasOverlayData?[ChunkSize];
|
||||
}
|
||||
}
|
||||
|
||||
public GasOverlayData? GetData(Vector2i gridIndices)
|
||||
{
|
||||
DebugTools.Assert(InBounds(gridIndices));
|
||||
return TileData[gridIndices.X - Origin.X][gridIndices.Y - Origin.Y];
|
||||
}
|
||||
|
||||
public GasOverlayData? SetData(Vector2i gridIndices, GasOverlayData? data)
|
||||
{
|
||||
DebugTools.Assert(InBounds(gridIndices));
|
||||
return TileData[gridIndices.X - Origin.X][gridIndices.Y - Origin.Y] = data;
|
||||
}
|
||||
|
||||
private bool InBounds(Vector2i gridIndices)
|
||||
{
|
||||
return gridIndices.X >= Origin.X &&
|
||||
gridIndices.Y >= Origin.Y &&
|
||||
gridIndices.X < Origin.X + ChunkSize &&
|
||||
gridIndices.Y < Origin.Y + ChunkSize;
|
||||
}
|
||||
}
|
||||
|
||||
public struct GasChunkEnumerator
|
||||
{
|
||||
private GasOverlayChunk _chunk;
|
||||
public int X = 0;
|
||||
public int Y = -1;
|
||||
private GasOverlayData?[] _column;
|
||||
|
||||
|
||||
public GasChunkEnumerator(GasOverlayChunk chunk)
|
||||
{
|
||||
_chunk = chunk;
|
||||
_column = _chunk.TileData[0];
|
||||
}
|
||||
|
||||
public bool MoveNext([NotNullWhen(true)] out GasOverlayData? gas)
|
||||
{
|
||||
while (X < ChunkSize)
|
||||
{
|
||||
// We want to increment Y before returning, but we also want it to match the current Y coordinate for
|
||||
// the returned gas, so using a slightly different logic for the Y loop.
|
||||
while (Y < ChunkSize - 1)
|
||||
{
|
||||
yield return TileData[x, y];
|
||||
Y++;
|
||||
gas = _column[Y];
|
||||
|
||||
if (gas != null)
|
||||
return true;
|
||||
}
|
||||
|
||||
X++;
|
||||
if (X < ChunkSize)
|
||||
_column = _chunk.TileData[X];
|
||||
Y = -1;
|
||||
}
|
||||
}
|
||||
|
||||
public void GetData(List<(Vector2i, SharedGasTileOverlaySystem.GasOverlayData)> existingData, HashSet<Vector2i> indices)
|
||||
{
|
||||
foreach (var index in indices)
|
||||
{
|
||||
existingData.Add((index, GetData(index)));
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Vector2i> GetAllIndices()
|
||||
{
|
||||
for (var x = 0; x < SharedGasTileOverlaySystem.ChunkSize; x++)
|
||||
{
|
||||
for (var y = 0; y < SharedGasTileOverlaySystem.ChunkSize; y++)
|
||||
{
|
||||
yield return new Vector2i(Vector2i.X + x, Vector2i.Y + y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public SharedGasTileOverlaySystem.GasOverlayData GetData(Vector2i indices)
|
||||
{
|
||||
DebugTools.Assert(InBounds(indices));
|
||||
return TileData[indices.X - Vector2i.X, indices.Y - Vector2i.Y];
|
||||
}
|
||||
|
||||
private bool InBounds(Vector2i indices)
|
||||
{
|
||||
if (indices.X < Vector2i.X || indices.Y < Vector2i.Y) return false;
|
||||
if (indices.X >= Vector2i.X + SharedGasTileOverlaySystem.ChunkSize || indices.Y >= Vector2i.Y + SharedGasTileOverlaySystem.ChunkSize) return false;
|
||||
return true;
|
||||
gas = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user