Gas overlay chunking (#1678)

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
This commit is contained in:
metalgearsloth
2020-08-18 00:12:21 +10:00
committed by GitHub
parent 0e6f55a23d
commit f54ba4b6d5
16 changed files with 786 additions and 306 deletions

View File

@@ -1,13 +1,19 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using Content.Client.Atmos;
using Content.Client.GameObjects.Components.Atmos;
using Content.Shared.Atmos;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.GameObjects.EntitySystems.Atmos;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Client.GameObjects.EntitySystems;
using Robust.Client.Graphics;
using Robust.Client.Interfaces.Graphics.Overlays;
using Robust.Client.Interfaces.ResourceManagement;
using Robust.Client.ResourceManagement;
using Robust.Client.Utility;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
@@ -16,8 +22,9 @@ using Robust.Shared.Utility;
namespace Content.Client.GameObjects.EntitySystems
{
[UsedImplicitly]
public sealed class GasTileOverlaySystem : SharedGasTileOverlaySystem
internal sealed class GasTileOverlaySystem : SharedGasTileOverlaySystem
{
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IResourceCache _resourceCache = default!;
private readonly Dictionary<float, Color> _fireCache = new Dictionary<float, Color>();
@@ -36,19 +43,20 @@ namespace Content.Client.GameObjects.EntitySystems
private readonly float[][] _fireFrameDelays = new float[FireStates][];
private readonly int[] _fireFrameCounter = new int[FireStates];
private readonly Texture[][] _fireFrames = new Texture[FireStates][];
private Dictionary<GridId, Dictionary<MapIndices, GasOverlayData>> _overlay = new Dictionary<GridId, Dictionary<MapIndices, GasOverlayData>>();
private Dictionary<GridId, Dictionary<MapIndices, GasOverlayChunk>> _tileData =
new Dictionary<GridId, Dictionary<MapIndices, GasOverlayChunk>>();
public override void Initialize()
{
base.Initialize();
SubscribeNetworkEvent(new EntityEventHandler<GasTileOverlayMessage>(OnTileOverlayMessage));
SubscribeNetworkEvent<GasOverlayMessage>(HandleGasOverlayMessage);
_mapManager.OnGridRemoved += OnGridRemoved;
for (var i = 0; i < Atmospherics.TotalNumberOfGases; i++)
{
var gas = Atmospherics.GetGas(i);
switch (gas.GasOverlay)
var overlay = Atmospherics.GetOverlay(i);
switch (overlay)
{
case SpriteSpecifier.Rsi animated:
var rsi = _resourceCache.GetResource<RSIResource>(animated.RsiPath).RSI;
@@ -82,13 +90,77 @@ namespace Content.Client.GameObjects.EntitySystems
_fireFrameDelays[i] = state.GetDelays();
_fireFrameCounter[i] = 0;
}
var overlayManager = IoCManager.Resolve<IOverlayManager>();
if(!overlayManager.HasOverlay(nameof(GasTileOverlay)))
overlayManager.AddOverlay(new GasTileOverlay());
}
private void HandleGasOverlayMessage(GasOverlayMessage message)
{
foreach (var (indices, data) in message.OverlayData)
{
var chunk = GetOrCreateChunk(message.GridId, indices);
chunk.Update(data, indices);
}
}
// Slightly different to the server-side system version
private GasOverlayChunk GetOrCreateChunk(GridId gridId, MapIndices indices)
{
if (!_tileData.TryGetValue(gridId, out var chunks))
{
chunks = new Dictionary<MapIndices, GasOverlayChunk>();
_tileData[gridId] = chunks;
}
var chunkIndices = GetGasChunkIndices(indices);
if (!chunks.TryGetValue(chunkIndices, out var chunk))
{
chunk = new GasOverlayChunk(gridId, chunkIndices);
chunks[chunkIndices] = chunk;
}
return chunk;
}
public override void Shutdown()
{
base.Shutdown();
_mapManager.OnGridRemoved -= OnGridRemoved;
var overlayManager = IoCManager.Resolve<IOverlayManager>();
if(!overlayManager.HasOverlay(nameof(GasTileOverlay)))
overlayManager.RemoveOverlay(nameof(GasTileOverlay));
}
private void OnGridRemoved(GridId gridId)
{
if (_tileData.ContainsKey(gridId))
{
_tileData.Remove(gridId);
}
}
public bool HasData(GridId gridId)
{
return _tileData.ContainsKey(gridId);
}
public (Texture, Color color)[] GetOverlays(GridId gridIndex, MapIndices indices)
{
if (!_overlay.TryGetValue(gridIndex, out var tiles) || !tiles.TryGetValue(indices, out var overlays))
if (!_tileData.TryGetValue(gridIndex, out var chunks))
return Array.Empty<(Texture, Color)>();
var chunkIndex = GetGasChunkIndices(indices);
if (!chunks.TryGetValue(chunkIndex, out var chunk))
return Array.Empty<(Texture, Color)>();
var overlays = chunk.GetData(indices);
if (overlays.Gas == null)
return Array.Empty<(Texture, Color)>();
var fire = overlays.FireState != 0;
var length = overlays.Gas.Length + (fire ? 1 : 0);
@@ -112,23 +184,6 @@ namespace Content.Client.GameObjects.EntitySystems
return list;
}
private void OnTileOverlayMessage(GasTileOverlayMessage ev)
{
if(ev.ClearAllOtherOverlays)
_overlay.Clear();
foreach (var data in ev.OverlayData)
{
if (!_overlay.TryGetValue(data.GridIndex, out var gridOverlays))
{
gridOverlays = new Dictionary<MapIndices, GasOverlayData>();
_overlay.Add(data.GridIndex, gridOverlays);
}
gridOverlays[data.GridIndices] = data.Data;
}
}
public override void FrameUpdate(float frameTime)
{
base.FrameUpdate(frameTime);