DecalSystem.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using Content.Client.Decals.Overlays;
  2. using Content.Shared.Decals;
  3. using Robust.Client.GameObjects;
  4. using Robust.Client.Graphics;
  5. using Robust.Shared.GameStates;
  6. using Robust.Shared.Utility;
  7. using static Content.Shared.Decals.DecalGridComponent;
  8. namespace Content.Client.Decals
  9. {
  10. public sealed class DecalSystem : SharedDecalSystem
  11. {
  12. [Dependency] private readonly IOverlayManager _overlayManager = default!;
  13. [Dependency] private readonly SpriteSystem _sprites = default!;
  14. private DecalOverlay _overlay = default!;
  15. private HashSet<uint> _removedUids = new();
  16. private readonly List<Vector2i> _removedChunks = new();
  17. public override void Initialize()
  18. {
  19. base.Initialize();
  20. _overlay = new DecalOverlay(_sprites, EntityManager, PrototypeManager);
  21. _overlayManager.AddOverlay(_overlay);
  22. SubscribeLocalEvent<DecalGridComponent, ComponentHandleState>(OnHandleState);
  23. SubscribeNetworkEvent<DecalChunkUpdateEvent>(OnChunkUpdate);
  24. }
  25. public void ToggleOverlay()
  26. {
  27. if (_overlayManager.HasOverlay<DecalOverlay>())
  28. {
  29. _overlayManager.RemoveOverlay(_overlay);
  30. }
  31. else
  32. {
  33. _overlayManager.AddOverlay(_overlay);
  34. }
  35. }
  36. public override void Shutdown()
  37. {
  38. base.Shutdown();
  39. _overlayManager.RemoveOverlay(_overlay);
  40. }
  41. protected override void OnDecalRemoved(EntityUid gridId, uint decalId, DecalGridComponent component, Vector2i indices, DecalChunk chunk)
  42. {
  43. base.OnDecalRemoved(gridId, decalId, component, indices, chunk);
  44. DebugTools.Assert(chunk.Decals.ContainsKey(decalId));
  45. chunk.Decals.Remove(decalId);
  46. }
  47. private void OnHandleState(EntityUid gridUid, DecalGridComponent gridComp, ref ComponentHandleState args)
  48. {
  49. // is this a delta or full state?
  50. _removedChunks.Clear();
  51. Dictionary<Vector2i, DecalChunk> modifiedChunks;
  52. switch (args.Current)
  53. {
  54. case DecalGridDeltaState delta:
  55. {
  56. modifiedChunks = delta.ModifiedChunks;
  57. foreach (var key in gridComp.ChunkCollection.ChunkCollection.Keys)
  58. {
  59. if (!delta.AllChunks.Contains(key))
  60. _removedChunks.Add(key);
  61. }
  62. break;
  63. }
  64. case DecalGridState state:
  65. {
  66. modifiedChunks = state.Chunks;
  67. foreach (var key in gridComp.ChunkCollection.ChunkCollection.Keys)
  68. {
  69. if (!state.Chunks.ContainsKey(key))
  70. _removedChunks.Add(key);
  71. }
  72. break;
  73. }
  74. default:
  75. return;
  76. }
  77. if (_removedChunks.Count > 0)
  78. RemoveChunks(gridUid, gridComp, _removedChunks);
  79. if (modifiedChunks.Count > 0)
  80. UpdateChunks(gridUid, gridComp, modifiedChunks);
  81. }
  82. private void OnChunkUpdate(DecalChunkUpdateEvent ev)
  83. {
  84. foreach (var (netGrid, updatedGridChunks) in ev.Data)
  85. {
  86. if (updatedGridChunks.Count == 0)
  87. continue;
  88. var gridId = GetEntity(netGrid);
  89. if (!TryComp(gridId, out DecalGridComponent? gridComp))
  90. {
  91. Log.Error($"Received decal information for an entity without a decal component: {ToPrettyString(gridId)}");
  92. continue;
  93. }
  94. UpdateChunks(gridId, gridComp, updatedGridChunks);
  95. }
  96. // Now we'll cull old chunks out of range as the server will send them to us anyway.
  97. foreach (var (netGrid, chunks) in ev.RemovedChunks)
  98. {
  99. if (chunks.Count == 0)
  100. continue;
  101. var gridId = GetEntity(netGrid);
  102. if (!TryComp(gridId, out DecalGridComponent? gridComp))
  103. {
  104. Log.Error($"Received decal information for an entity without a decal component: {ToPrettyString(gridId)}");
  105. continue;
  106. }
  107. RemoveChunks(gridId, gridComp, chunks);
  108. }
  109. }
  110. private void UpdateChunks(EntityUid gridId, DecalGridComponent gridComp, Dictionary<Vector2i, DecalChunk> updatedGridChunks)
  111. {
  112. var chunkCollection = gridComp.ChunkCollection.ChunkCollection;
  113. // Update any existing data / remove decals we didn't receive data for.
  114. foreach (var (indices, newChunkData) in updatedGridChunks)
  115. {
  116. if (chunkCollection.TryGetValue(indices, out var chunk))
  117. {
  118. _removedUids.Clear();
  119. _removedUids.UnionWith(chunk.Decals.Keys);
  120. _removedUids.ExceptWith(newChunkData.Decals.Keys);
  121. foreach (var removedUid in _removedUids)
  122. {
  123. OnDecalRemoved(gridId, removedUid, gridComp, indices, chunk);
  124. gridComp.DecalIndex.Remove(removedUid);
  125. }
  126. }
  127. chunkCollection[indices] = newChunkData;
  128. foreach (var (uid, decal) in newChunkData.Decals)
  129. {
  130. gridComp.DecalIndex[uid] = indices;
  131. }
  132. }
  133. }
  134. private void RemoveChunks(EntityUid gridId, DecalGridComponent gridComp, IEnumerable<Vector2i> chunks)
  135. {
  136. var chunkCollection = gridComp.ChunkCollection.ChunkCollection;
  137. foreach (var index in chunks)
  138. {
  139. if (!chunkCollection.TryGetValue(index, out var chunk))
  140. continue;
  141. foreach (var decalId in chunk.Decals.Keys)
  142. {
  143. OnDecalRemoved(gridId, decalId, gridComp, index, chunk);
  144. gridComp.DecalIndex.Remove(decalId);
  145. }
  146. chunkCollection.Remove(index);
  147. }
  148. }
  149. }
  150. }