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:
Leon Friedrich
2022-03-30 18:57:35 +13:00
committed by GitHub
parent 5c7a1534db
commit 4bc73ac591
43 changed files with 365 additions and 99 deletions

View File

@@ -1,4 +1,5 @@
using Content.Client.Items.Components;
using Content.Shared.Hands.Components;
using Content.Shared.Light.Component;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
@@ -6,9 +7,11 @@ using Robust.Client.UserInterface.Controls;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Maths;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Timing;
using Robust.Shared.ViewVariables;
using System.Collections.Generic;
using static Robust.Client.UserInterface.Controls.BoxContainer;
using static Robust.Shared.GameObjects.SharedSpriteComponent;
namespace Content.Client.Light.Components
{
@@ -17,6 +20,37 @@ namespace Content.Client.Light.Components
public sealed class HandheldLightComponent : SharedHandheldLightComponent, IItemStatus
{
public byte? Level;
public bool Activated;
/// <summary>
/// Whether to automatically set item-prefixes when toggling the flashlight.
/// </summary>
/// <remarks>
/// Flashlights should probably be using explicit unshaded sprite, in-hand and clothing layers, this is
/// mostly here for backwards compatibility.
/// </remarks>
[DataField("addPrefix")]
public bool AddPrefix = false;
/// <summary>
/// Sprite layer that will have it's visibility toggled when this item is toggled.
/// </summary>
[DataField("layer")]
public string Layer = "light";
/// <summary>
/// Layers to add to the sprite of the player that is holding this entity.
/// </summary>
[DataField("inhandVisuals")]
public Dictionary<HandLocation, List<PrototypeLayerData>> InhandVisuals = new();
/// <summary>
/// Layers to add to the sprite of the player that is wearing this entity.
/// </summary>
[DataField("clothingVisuals")]
public readonly Dictionary<string, List<PrototypeLayerData>> ClothingVisuals = new();
public Color Color { get; internal set; }
public Control MakeControl()
{

View File

@@ -1,16 +1,77 @@
using Content.Client.Clothing;
using Content.Client.Items.Systems;
using Content.Client.Light.Components;
using Content.Shared.Clothing;
using Content.Shared.Hands;
using Content.Shared.Item;
using Content.Shared.Light.Component;
using Robust.Shared.GameObjects;
using Robust.Client.GameObjects;
using Robust.Shared.GameStates;
using System.Linq;
namespace Content.Client.Light;
public sealed class HandheldLightSystem : EntitySystem
{
[Dependency] private readonly ItemSystem _itemSys = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<HandheldLightComponent, ComponentHandleState>(OnHandleState);
SubscribeLocalEvent<HandheldLightComponent, GetInhandVisualsEvent>(OnGetHeldVisuals, after: new[] { typeof(ItemSystem) } );
SubscribeLocalEvent<HandheldLightComponent, GetEquipmentVisualsEvent>(OnGetEquipmentVisuals, after: new[] { typeof(ClothingSystem)});
}
/// <summary>
/// Add the unshaded light overlays to any clothing sprites.
/// </summary>
private void OnGetEquipmentVisuals(EntityUid uid, HandheldLightComponent component, GetEquipmentVisualsEvent args)
{
if (!component.Activated)
return;
if (!component.ClothingVisuals.TryGetValue(args.Slot, out var layers))
return;
var i = 0;
foreach (var layer in layers)
{
var key = layer.MapKeys?.FirstOrDefault();
if (key == null)
{
key = i == 0 ? $"{args.Slot}-light" : $"{args.Slot}-light-{i}";
i++;
}
args.Layers.Add((key, layer));
}
}
/// <summary>
/// Add the unshaded light overlays to any in-hand sprites.
/// </summary>
private void OnGetHeldVisuals(EntityUid uid, HandheldLightComponent component, GetInhandVisualsEvent args)
{
if (!component.Activated)
return;
if (!component.InhandVisuals.TryGetValue(args.Location, out var layers))
return;
var i = 0;
var defaultKey = $"inhand-{args.Location.ToString().ToLowerInvariant()}-light";
foreach (var layer in layers)
{
var key = layer.MapKeys?.FirstOrDefault();
if (key == null)
{
key = i == 0 ? defaultKey : $"{defaultKey}-{i}";
i++;
}
args.Layers.Add((key, layer));
}
}
private void OnHandleState(EntityUid uid, HandheldLightComponent component, ref ComponentHandleState args)
@@ -19,5 +80,26 @@ public sealed class HandheldLightSystem : EntitySystem
return;
component.Level = state.Charge;
if (state.Activated == component.Activated)
return;
component.Activated = state.Activated;
_itemSys.VisualsChanged(uid);
if (TryComp(component.Owner, out SpriteComponent? sprite))
{
sprite.LayerSetVisible(component.Layer, state.Activated);
}
if (TryComp(uid, out PointLightComponent? light))
{
light.Enabled = state.Activated;
}
// really hand-held lights should be using a separate unshaded layer. (see FlashlightVisualizer)
// this prefix stuff is largely for backwards compatibility with RSIs/yamls that have not been updated.
if (component.AddPrefix && TryComp(uid, out SharedItemComponent? item))
item.EquippedPrefix = state.Activated ? "on" : "off";
}
}

View File

@@ -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);
}
}
}