Decal system cleanup (#13493)

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
Leon Friedrich
2023-01-16 13:46:22 +13:00
committed by GitHub
parent 3cfd814503
commit dbe99f9fa6
7 changed files with 141 additions and 283 deletions

View File

@@ -15,15 +15,11 @@ namespace Content.Client.Decals
private DecalOverlay _overlay = default!;
// TODO move this data to the component
public readonly Dictionary<EntityUid, SortedDictionary<int, SortedDictionary<uint, Decal>>> DecalRenderIndex = new();
private readonly Dictionary<EntityUid, Dictionary<uint, int>> _decalZIndexIndex = new();
public override void Initialize()
{
base.Initialize();
_overlay = new DecalOverlay(this, _sprites, EntityManager, PrototypeManager);
_overlay = new DecalOverlay(_sprites, EntityManager, PrototypeManager);
_overlayManager.AddOverlay(_overlay);
SubscribeLocalEvent<DecalGridComponent, ComponentHandleState>(OnHandleState);
@@ -42,41 +38,25 @@ namespace Content.Client.Decals
}
}
protected override void OnCompRemove(EntityUid uid, DecalGridComponent component, ComponentRemove args)
{
DecalRenderIndex.Remove(uid);
_decalZIndexIndex.Remove(uid);
base.OnCompRemove(uid, component, args);
}
protected override void OnCompAdd(EntityUid uid, DecalGridComponent component, ComponentAdd args)
{
DecalRenderIndex[uid] = new();
_decalZIndexIndex[uid] = new();
base.OnCompAdd(uid, component, args);
}
public override void Shutdown()
{
base.Shutdown();
_overlayManager.RemoveOverlay(_overlay);
}
protected override bool RemoveDecalHook(EntityUid gridId, uint uid)
protected override void OnDecalRemoved(EntityUid gridId, uint decalId, DecalGridComponent component, Vector2i indices, DecalChunk chunk)
{
RemoveDecalFromRenderIndex(gridId, uid);
return base.RemoveDecalHook(gridId, uid);
}
base.OnDecalRemoved(gridId, decalId, component, indices, chunk);
private void RemoveDecalFromRenderIndex(EntityUid gridId, uint uid)
{
var zIndex = _decalZIndexIndex[gridId][uid];
if (!component.DecalZIndexIndex.Remove(decalId, out var zIndex))
return;
DecalRenderIndex[gridId][zIndex].Remove(uid);
if (DecalRenderIndex[gridId][zIndex].Count == 0)
DecalRenderIndex[gridId].Remove(zIndex);
if (!component.DecalRenderIndex.TryGetValue(zIndex, out var renderIndex))
return;
_decalZIndexIndex[gridId].Remove(uid);
renderIndex.Remove(decalId);
if (renderIndex.Count == 0)
component.DecalRenderIndex.Remove(zIndex);
}
private void OnHandleState(EntityUid gridUid, DecalGridComponent gridComp, ref ComponentHandleState args)
@@ -143,14 +123,8 @@ namespace Content.Client.Decals
private void UpdateChunks(EntityUid gridId, DecalGridComponent gridComp, Dictionary<Vector2i, DecalChunk> updatedGridChunks)
{
var chunkCollection = gridComp.ChunkCollection.ChunkCollection;
if (!ChunkIndex.TryGetValue(gridId, out var chunkIndex) ||
!DecalRenderIndex.TryGetValue(gridId, out var renderIndex) ||
!_decalZIndexIndex.TryGetValue(gridId, out var zIndexIndex))
{
Logger.Error($"Grid missing from dictionaries while updating decal chunks for grid {ToPrettyString(gridId)}");
return;
}
var renderIndex = gridComp.DecalRenderIndex;
var zIndexIndex = gridComp.DecalZIndexIndex;
// Update any existing data / remove decals we didn't receive data for.
foreach (var (indices, newChunkData) in updatedGridChunks)
@@ -161,8 +135,8 @@ namespace Content.Client.Decals
removedUids.ExceptWith(newChunkData.Decals.Keys);
foreach (var removedUid in removedUids)
{
RemoveDecalHook(gridId, removedUid);
chunkIndex.Remove(removedUid);
OnDecalRemoved(gridId, removedUid, gridComp, indices, chunk);
gridComp.DecalIndex.Remove(removedUid);
}
}
@@ -175,7 +149,7 @@ namespace Content.Client.Decals
renderIndex.GetOrNew(decal.ZIndex)[uid] = decal;
zIndexIndex[uid] = decal.ZIndex;
chunkIndex[uid] = indices;
gridComp.DecalIndex[uid] = indices;
}
}
}
@@ -184,20 +158,14 @@ namespace Content.Client.Decals
{
var chunkCollection = gridComp.ChunkCollection.ChunkCollection;
if (!ChunkIndex.TryGetValue(gridId, out var chunkIndex))
{
Logger.Error($"Missing grid in ChunkIndex dictionary while removing chunks from grid {ToPrettyString(gridId)}");
return;
}
foreach (var index in chunks)
{
if (!chunkCollection.TryGetValue(index, out var chunk)) continue;
foreach (var uid in chunk.Decals.Keys)
foreach (var decalId in chunk.Decals.Keys)
{
RemoveDecalHook(gridId, uid);
chunkIndex.Remove(uid);
OnDecalRemoved(gridId, decalId, gridComp, index, chunk);
gridComp.DecalIndex.Remove(decalId);
}
chunkCollection.Remove(index);