decal system & crayons (#5183)
Co-authored-by: Paul <ritter.paul1+git@googlemail.com>
This commit is contained in:
60
Content.Client/Decals/DecalOverlay.cs
Normal file
60
Content.Client/Decals/DecalOverlay.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.Decals;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Utility;
|
||||
using Robust.Shared.Enums;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.Maths;
|
||||
|
||||
namespace Content.Client.Decals
|
||||
{
|
||||
public class DecalOverlay : Overlay
|
||||
{
|
||||
private readonly DecalSystem _system;
|
||||
private readonly IMapManager _mapManager;
|
||||
private readonly IPrototypeManager _prototypeManager;
|
||||
|
||||
public override OverlaySpace Space => OverlaySpace.WorldSpaceBelowEntities;
|
||||
|
||||
public DecalOverlay(DecalSystem system, IMapManager mapManager, IPrototypeManager prototypeManager)
|
||||
{
|
||||
_system = system;
|
||||
_mapManager = mapManager;
|
||||
_prototypeManager = prototypeManager;
|
||||
}
|
||||
|
||||
protected override void Draw(in OverlayDrawArgs args)
|
||||
{
|
||||
var handle = args.WorldHandle;
|
||||
|
||||
Dictionary<string, SpriteSpecifier> cachedTextures = new();
|
||||
|
||||
SpriteSpecifier GetSpriteSpecifier(string id)
|
||||
{
|
||||
if (cachedTextures.TryGetValue(id, out var spriteSpecifier))
|
||||
return spriteSpecifier;
|
||||
|
||||
spriteSpecifier = _prototypeManager.Index<DecalPrototype>(id).Sprite;
|
||||
cachedTextures.Add(id, spriteSpecifier);
|
||||
return spriteSpecifier;
|
||||
}
|
||||
|
||||
foreach (var (gridId, zIndexDictionary) in _system.DecalRenderIndex)
|
||||
{
|
||||
var grid = _mapManager.GetGrid(gridId);
|
||||
handle.SetTransform(grid.WorldMatrix);
|
||||
foreach (var (_, decals) in zIndexDictionary)
|
||||
{
|
||||
foreach (var (_, decal) in decals)
|
||||
{
|
||||
var spriteSpecifier = GetSpriteSpecifier(decal.Id);
|
||||
handle.DrawTexture(spriteSpecifier.Frame0(), decal.Coordinates, decal.Angle, decal.Color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
114
Content.Client/Decals/DecalSystem.cs
Normal file
114
Content.Client/Decals/DecalSystem.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.Decals;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
|
||||
namespace Content.Client.Decals
|
||||
{
|
||||
public class DecalSystem : SharedDecalSystem
|
||||
{
|
||||
[Dependency] private readonly IOverlayManager _overlayManager = default!;
|
||||
|
||||
private DecalOverlay _overlay = default!;
|
||||
public Dictionary<GridId, SortedDictionary<int, SortedDictionary<uint, Decal>>> DecalRenderIndex = new();
|
||||
private Dictionary<GridId, Dictionary<uint, int>> DecalZIndexIndex = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_overlay = new DecalOverlay(this, MapManager, PrototypeManager);
|
||||
_overlayManager.AddOverlay(_overlay);
|
||||
|
||||
SubscribeNetworkEvent<DecalChunkUpdateEvent>(OnChunkUpdate);
|
||||
SubscribeLocalEvent<GridInitializeEvent>(OnGridInitialize);
|
||||
SubscribeLocalEvent<GridRemovalEvent>(OnGridRemoval);
|
||||
}
|
||||
|
||||
public void ToggleOverlay()
|
||||
{
|
||||
if (_overlayManager.HasOverlay<DecalOverlay>())
|
||||
{
|
||||
_overlayManager.RemoveOverlay(_overlay);
|
||||
}
|
||||
else
|
||||
{
|
||||
_overlayManager.AddOverlay(_overlay);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnGridRemoval(GridRemovalEvent ev)
|
||||
{
|
||||
DecalRenderIndex.Remove(ev.GridId);
|
||||
DecalZIndexIndex.Remove(ev.GridId);
|
||||
}
|
||||
|
||||
private void OnGridInitialize(GridInitializeEvent ev)
|
||||
{
|
||||
DecalRenderIndex[ev.GridId] = new();
|
||||
DecalZIndexIndex[ev.GridId] = new();
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
_overlayManager.RemoveOverlay(_overlay);
|
||||
}
|
||||
|
||||
protected override bool RemoveDecalHook(GridId gridId, uint uid)
|
||||
{
|
||||
RemoveDecalFromRenderIndex(gridId, uid);
|
||||
|
||||
return base.RemoveDecalHook(gridId, uid);
|
||||
}
|
||||
|
||||
private void RemoveDecalFromRenderIndex(GridId gridId, uint uid)
|
||||
{
|
||||
var zIndex = DecalZIndexIndex[gridId][uid];
|
||||
|
||||
DecalRenderIndex[gridId][zIndex].Remove(uid);
|
||||
if (DecalRenderIndex[gridId][zIndex].Count == 0)
|
||||
DecalRenderIndex[gridId].Remove(zIndex);
|
||||
|
||||
DecalZIndexIndex[gridId].Remove(uid);
|
||||
}
|
||||
|
||||
private void OnChunkUpdate(DecalChunkUpdateEvent ev)
|
||||
{
|
||||
foreach (var (gridId, gridChunks) in ev.Data)
|
||||
{
|
||||
foreach (var (indices, newChunkData) in gridChunks)
|
||||
{
|
||||
var chunkCollection = ChunkCollection(gridId);
|
||||
if (chunkCollection.TryGetValue(indices, out var chunk))
|
||||
{
|
||||
var removedUids = new HashSet<uint>(chunk.Keys);
|
||||
removedUids.ExceptWith(newChunkData.Keys);
|
||||
foreach (var removedUid in removedUids)
|
||||
{
|
||||
RemoveDecalFromRenderIndex(gridId, removedUid);
|
||||
}
|
||||
}
|
||||
foreach (var (uid, decal) in newChunkData)
|
||||
{
|
||||
if(!DecalRenderIndex[gridId].ContainsKey(decal.ZIndex))
|
||||
DecalRenderIndex[gridId][decal.ZIndex] = new();
|
||||
|
||||
if (DecalZIndexIndex.TryGetValue(gridId, out var values) && values.TryGetValue(uid, out var zIndex))
|
||||
{
|
||||
DecalRenderIndex[gridId][zIndex].Remove(uid);
|
||||
}
|
||||
|
||||
DecalRenderIndex[gridId][decal.ZIndex][uid] = decal;
|
||||
DecalZIndexIndex[gridId][uid] = decal.ZIndex;
|
||||
|
||||
ChunkIndex[gridId][uid] = indices;
|
||||
}
|
||||
chunkCollection[indices] = newChunkData;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Content.Client/Decals/ToggleDecalCommand.cs
Normal file
15
Content.Client/Decals/ToggleDecalCommand.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Robust.Shared.Console;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Client.Decals;
|
||||
|
||||
public class ToggleDecalCommand : IConsoleCommand
|
||||
{
|
||||
public string Command => "toggledecals";
|
||||
public string Description => "Toggles decaloverlay";
|
||||
public string Help => $"{Command}";
|
||||
public void Execute(IConsoleShell shell, string argStr, string[] args)
|
||||
{
|
||||
EntitySystem.Get<DecalSystem>().ToggleOverlay();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user