Make flashlights, atmos hardsuit, and RGB use the new layer features (#6253)
* Make flashlights, atmos hardsuit, and RGB use the new layer features * avoid self-conflict * fix rgb not updating on add * cleanup * Update Content.Client/Light/RgbLightControllerSystem.cs Co-authored-by: mirrorcult <lunarautomaton6@gmail.com> * cleanup diff Co-authored-by: mirrorcult <lunarautomaton6@gmail.com>
This commit is contained in:
@@ -1,20 +1,21 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Content.Shared.Item;
|
||||
using Content.Client.Items.Systems;
|
||||
using Content.Shared.Clothing;
|
||||
using Content.Shared.Hands;
|
||||
using Content.Shared.Inventory.Events;
|
||||
using Content.Shared.Light;
|
||||
using Content.Shared.Light.Component;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Timing;
|
||||
using static Robust.Client.GameObjects.SpriteComponent;
|
||||
|
||||
namespace Content.Client.Light
|
||||
{
|
||||
public sealed class RgbLightControllerSystem : SharedRgbLightControllerSystem
|
||||
{
|
||||
[Dependency] private IGameTiming _gameTiming = default!;
|
||||
[Dependency] private ItemSystem _itemSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -23,77 +24,137 @@ namespace Content.Client.Light
|
||||
SubscribeLocalEvent<RgbLightControllerComponent, ComponentHandleState>(OnHandleState);
|
||||
SubscribeLocalEvent<RgbLightControllerComponent, ComponentShutdown>(OnComponentShutdown);
|
||||
SubscribeLocalEvent<RgbLightControllerComponent, ComponentStartup>(OnComponentStart);
|
||||
|
||||
SubscribeLocalEvent<RgbLightControllerComponent, GotUnequippedEvent>(OnGotUnequipped);
|
||||
|
||||
SubscribeLocalEvent<RgbLightControllerComponent, EquipmentVisualsUpdatedEvent>(OnEquipmentVisualsUpdated);
|
||||
SubscribeLocalEvent<RgbLightControllerComponent, HeldVisualsUpdatedEvent>(OnHeldVisualsUpdated);
|
||||
}
|
||||
|
||||
private void OnComponentStart(EntityUid uid, RgbLightControllerComponent rgb, ComponentStartup args)
|
||||
{
|
||||
if (TryComp(uid, out PointLightComponent? light))
|
||||
rgb.OriginalLightColor = light.Color;
|
||||
GetOriginalColors(uid, rgb);
|
||||
|
||||
if (TryComp(uid, out SharedItemComponent? item))
|
||||
rgb.OriginalItemColor = item.Color;
|
||||
|
||||
GetOriginalSpriteColors(uid, rgb);
|
||||
// trigger visuals updated events
|
||||
_itemSystem.VisualsChanged(uid);
|
||||
}
|
||||
|
||||
private void OnComponentShutdown(EntityUid uid, RgbLightControllerComponent rgb, ComponentShutdown args)
|
||||
{
|
||||
if (TryComp(uid, out PointLightComponent? light))
|
||||
light.Color = rgb.OriginalLightColor;
|
||||
ResetOriginalColors(uid, rgb);
|
||||
|
||||
if (TryComp(uid, out SharedItemComponent? item))
|
||||
item.Color = rgb.OriginalItemColor;
|
||||
|
||||
ResetSpriteColors(uid, rgb);
|
||||
// and reset any in-hands or clothing sprites
|
||||
_itemSystem.VisualsChanged(uid);
|
||||
}
|
||||
|
||||
private void OnGotUnequipped(EntityUid uid, RgbLightControllerComponent rgb, GotUnequippedEvent args)
|
||||
{
|
||||
rgb.Holder = null;
|
||||
rgb.HolderLayers = null;
|
||||
}
|
||||
|
||||
private void OnHeldVisualsUpdated(EntityUid uid, RgbLightControllerComponent rgb, HeldVisualsUpdatedEvent args)
|
||||
{
|
||||
if (args.RevealedLayers.Count == 0)
|
||||
{
|
||||
rgb.Holder = null;
|
||||
rgb.HolderLayers = null;
|
||||
return;
|
||||
}
|
||||
|
||||
rgb.Holder = args.User;
|
||||
rgb.HolderLayers = new();
|
||||
|
||||
if (!TryComp(args.User, out SpriteComponent? sprite))
|
||||
return;
|
||||
|
||||
foreach (var key in args.RevealedLayers)
|
||||
{
|
||||
if (!sprite.LayerMapTryGet(key, out var index) || sprite[index] is not Layer layer)
|
||||
continue;
|
||||
|
||||
if (layer.ShaderPrototype == "unshaded")
|
||||
rgb.HolderLayers.Add(key);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEquipmentVisualsUpdated(EntityUid uid, RgbLightControllerComponent rgb, EquipmentVisualsUpdatedEvent args)
|
||||
{
|
||||
rgb.Holder = args.Equipee;
|
||||
rgb.HolderLayers = new();
|
||||
|
||||
if (!TryComp(args.Equipee, out SpriteComponent? sprite))
|
||||
return;
|
||||
|
||||
foreach (var key in args.RevealedLayers)
|
||||
{
|
||||
if (!sprite.LayerMapTryGet(key, out var index) || sprite[index] is not Layer layer)
|
||||
continue;
|
||||
|
||||
if (layer.ShaderPrototype == "unshaded")
|
||||
rgb.HolderLayers.Add(key);
|
||||
}
|
||||
}
|
||||
private void OnHandleState(EntityUid uid, RgbLightControllerComponent rgb, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not RgbLightControllerState state)
|
||||
return;
|
||||
|
||||
ResetSpriteColors(uid, rgb);
|
||||
ResetOriginalColors(uid, rgb);
|
||||
rgb.CycleRate = state.CycleRate;
|
||||
rgb.Layers = state.Layers;
|
||||
GetOriginalColors(uid, rgb);
|
||||
|
||||
// get the new original sprite colors (necessary if rgb.Layers was updated).
|
||||
GetOriginalSpriteColors(uid, rgb);
|
||||
}
|
||||
|
||||
private void GetOriginalSpriteColors(EntityUid uid, RgbLightControllerComponent? rgb = null, SpriteComponent? sprite = null)
|
||||
private void GetOriginalColors(EntityUid uid, RgbLightControllerComponent? rgb = null, PointLightComponent? light = null, SpriteComponent? sprite = null)
|
||||
{
|
||||
if (!Resolve(uid, ref rgb, ref sprite))
|
||||
if (!Resolve(uid, ref rgb, ref sprite, ref light))
|
||||
return;
|
||||
|
||||
rgb.OriginalLightColor = light.Color;
|
||||
rgb.OriginalLayerColors = new();
|
||||
|
||||
var layerCount = sprite.AllLayers.Count();
|
||||
|
||||
// if layers is null, get unshaded layers
|
||||
if (rgb.Layers == null)
|
||||
{
|
||||
rgb.OriginalSpriteColor = sprite.Color;
|
||||
rgb.OriginalLayerColors = null;
|
||||
rgb.Layers = new();
|
||||
|
||||
for (var i = 0; i < layerCount; i++)
|
||||
{
|
||||
if (sprite[i] is Layer layer && layer.ShaderPrototype == "unshaded")
|
||||
{
|
||||
rgb.Layers.Add(i);
|
||||
rgb.OriginalLayerColors[i] = layer.Color;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
var spriteLayerCount = sprite.AllLayers.Count();
|
||||
rgb.OriginalLayerColors = new(rgb.Layers.Count);
|
||||
|
||||
foreach (var layer in rgb.Layers.ToArray())
|
||||
foreach (var index in rgb.Layers.ToArray())
|
||||
{
|
||||
if (layer < spriteLayerCount)
|
||||
rgb.OriginalLayerColors[layer] = sprite[layer].Color;
|
||||
if (index < layerCount)
|
||||
rgb.OriginalLayerColors[index] = sprite[index].Color;
|
||||
else
|
||||
rgb.Layers.Remove(layer);
|
||||
{
|
||||
// admeme fuck-ups or bad yaml?
|
||||
Logger.Warning($"RGB light attempted to use invalid sprite index {index} on entity {ToPrettyString(uid)}");
|
||||
rgb.Layers.Remove(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetSpriteColors(EntityUid uid, RgbLightControllerComponent? rgb = null, SpriteComponent? sprite = null)
|
||||
private void ResetOriginalColors(EntityUid uid, RgbLightControllerComponent? rgb = null, PointLightComponent? light = null, SpriteComponent? sprite = null)
|
||||
{
|
||||
if (!Resolve(uid, ref rgb, ref sprite))
|
||||
if (!Resolve(uid, ref rgb, ref sprite, ref light))
|
||||
return;
|
||||
|
||||
light.Color = rgb.OriginalLightColor;
|
||||
|
||||
if (rgb.Layers == null || rgb.OriginalLayerColors == null)
|
||||
{
|
||||
sprite.Color = rgb.OriginalSpriteColor;
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var (layer, color) in rgb.OriginalLayerColors)
|
||||
{
|
||||
@@ -109,9 +170,7 @@ namespace Content.Client.Light
|
||||
|
||||
light.Color = color;
|
||||
|
||||
if (rgb.Layers == null)
|
||||
sprite.Color = color;
|
||||
else
|
||||
if (rgb.Layers != null)
|
||||
{
|
||||
foreach (var layer in rgb.Layers)
|
||||
{
|
||||
@@ -119,9 +178,14 @@ namespace Content.Client.Light
|
||||
}
|
||||
}
|
||||
|
||||
// not all rgb is hand-held (Hence, not part of EntityQuery)
|
||||
if (TryComp(rgb.Owner, out SharedItemComponent? item))
|
||||
item.Color = color;
|
||||
// is the entity being held by someone?
|
||||
if (rgb.HolderLayers == null || !TryComp(rgb.Holder, out SpriteComponent? holderSprite))
|
||||
continue;
|
||||
|
||||
foreach (var layer in rgb.HolderLayers)
|
||||
{
|
||||
holderSprite.LayerSetColor(layer, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user