Decal state handling (#12624)

This commit is contained in:
Leon Friedrich
2022-12-19 08:28:46 +13:00
committed by GitHub
parent 2759ef009e
commit 8f352f87c2
8 changed files with 312 additions and 99 deletions

View File

@@ -1,7 +1,9 @@
using Content.Shared.Decals;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Shared.GameStates;
using Robust.Shared.Utility;
using static Content.Shared.Decals.DecalGridComponent;
namespace Content.Client.Decals
{
@@ -21,6 +23,7 @@ namespace Content.Client.Decals
_overlay = new DecalOverlay(this, _sprites, EntityManager, PrototypeManager);
_overlayManager.AddOverlay(_overlay);
SubscribeLocalEvent<DecalGridComponent, ComponentHandleState>(OnHandleState);
SubscribeNetworkEvent<DecalChunkUpdateEvent>(OnChunkUpdate);
SubscribeLocalEvent<GridInitializeEvent>(OnGridInitialize);
SubscribeLocalEvent<GridRemovalEvent>(OnGridRemoval);
@@ -73,6 +76,35 @@ namespace Content.Client.Decals
_decalZIndexIndex[gridId].Remove(uid);
}
private void OnHandleState(EntityUid gridUid, DecalGridComponent gridComp, ref ComponentHandleState args)
{
if (args.Current is not DecalGridState state)
return;
// is this a delta or full state?
var removedChunks = new List<Vector2i>();
if (!state.FullState)
{
foreach (var key in gridComp.ChunkCollection.ChunkCollection.Keys)
{
if (!state.AllChunks!.Contains(key))
removedChunks.Add(key);
}
}
else
{
foreach (var key in gridComp.ChunkCollection.ChunkCollection.Keys)
{
if (!state.Chunks.ContainsKey(key))
removedChunks.Add(key);
}
}
RemoveChunks(gridUid, gridComp, removedChunks);
UpdateChunks(gridUid, gridComp, state.Chunks);
return;
}
private void OnChunkUpdate(DecalChunkUpdateEvent ev)
{
foreach (var (gridId, updatedGridChunks) in ev.Data)
@@ -85,37 +117,7 @@ namespace Content.Client.Decals
continue;
}
var chunkCollection = gridComp.ChunkCollection.ChunkCollection;
var chunkIndex = ChunkIndex[gridId];
var renderIndex = DecalRenderIndex[gridId];
var zIndexIndex = _decalZIndexIndex[gridId];
// Update any existing data / remove decals we didn't receive data for.
foreach (var (indices, newChunkData) in updatedGridChunks)
{
if (chunkCollection.TryGetValue(indices, out var chunk))
{
var removedUids = new HashSet<uint>(chunk.Keys);
removedUids.ExceptWith(newChunkData.Keys);
foreach (var removedUid in removedUids)
{
RemoveDecalHook(gridId, removedUid);
chunkIndex.Remove(removedUid);
}
}
chunkCollection[indices] = newChunkData;
foreach (var (uid, decal) in newChunkData)
{
if (zIndexIndex.TryGetValue(uid, out var zIndex))
renderIndex[zIndex].Remove(uid);
renderIndex.GetOrNew(decal.ZIndex)[uid] = decal;
zIndexIndex[uid] = decal.ZIndex;
chunkIndex[uid] = indices;
}
}
UpdateChunks(gridId, gridComp, updatedGridChunks);
}
// Now we'll cull old chunks out of range as the server will send them to us anyway.
@@ -129,21 +131,61 @@ namespace Content.Client.Decals
continue;
}
var chunkCollection = gridComp.ChunkCollection.ChunkCollection;
var chunkIndex = ChunkIndex[gridId];
RemoveChunks(gridId, gridComp, chunks);
}
}
foreach (var index in chunks)
private void UpdateChunks(EntityUid gridId, DecalGridComponent gridComp, Dictionary<Vector2i, DecalChunk> updatedGridChunks)
{
var chunkCollection = gridComp.ChunkCollection.ChunkCollection;
var chunkIndex = ChunkIndex[gridId];
var renderIndex = DecalRenderIndex[gridId];
var zIndexIndex = _decalZIndexIndex[gridId];
// Update any existing data / remove decals we didn't receive data for.
foreach (var (indices, newChunkData) in updatedGridChunks)
{
if (chunkCollection.TryGetValue(indices, out var chunk))
{
if (!chunkCollection.TryGetValue(index, out var chunk)) continue;
foreach (var (uid, _) in chunk)
var removedUids = new HashSet<uint>(chunk.Decals.Keys);
removedUids.ExceptWith(newChunkData.Decals.Keys);
foreach (var removedUid in removedUids)
{
RemoveDecalHook(gridId, uid);
chunkIndex.Remove(uid);
RemoveDecalHook(gridId, removedUid);
chunkIndex.Remove(removedUid);
}
chunkCollection.Remove(index);
}
chunkCollection[indices] = newChunkData;
foreach (var (uid, decal) in newChunkData.Decals)
{
if (zIndexIndex.TryGetValue(uid, out var zIndex))
renderIndex[zIndex].Remove(uid);
renderIndex.GetOrNew(decal.ZIndex)[uid] = decal;
zIndexIndex[uid] = decal.ZIndex;
chunkIndex[uid] = indices;
}
}
}
private void RemoveChunks(EntityUid gridId, DecalGridComponent gridComp, IEnumerable<Vector2i> chunks)
{
var chunkCollection = gridComp.ChunkCollection.ChunkCollection;
var chunkIndex = ChunkIndex[gridId];
foreach (var index in chunks)
{
if (!chunkCollection.TryGetValue(index, out var chunk)) continue;
foreach (var uid in chunk.Decals.Keys)
{
RemoveDecalHook(gridId, uid);
chunkIndex.Remove(uid);
}
chunkCollection.Remove(index);
}
}
}