Make energy swords use RgbLightController (#7344)

This commit is contained in:
Leon Friedrich
2022-04-16 17:11:48 +12:00
committed by GitHub
parent 1dcaa2d44b
commit a231429cb4
35 changed files with 240 additions and 583 deletions

View File

@@ -1,17 +1,10 @@
using Content.Client.Items.Components;
using Content.Shared.Hands.Components;
using Content.Shared.Light.Component;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
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 System.Collections.Generic;
using static Robust.Client.UserInterface.Controls.BoxContainer;
using static Robust.Shared.GameObjects.SharedSpriteComponent;
namespace Content.Client.Light.Components
{
@@ -32,26 +25,6 @@ namespace Content.Client.Light.Components
[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()
{
return new StatusControl(this);

View File

@@ -1,13 +1,8 @@
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.Client.GameObjects;
using Robust.Shared.GameStates;
using System.Linq;
namespace Content.Client.Light;
@@ -19,59 +14,6 @@ public sealed class HandheldLightSystem : EntitySystem
{
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)
@@ -85,21 +27,13 @@ public sealed class HandheldLightSystem : EntitySystem
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";
_itemSys.VisualsChanged(uid);
}
}
}

View File

@@ -0,0 +1,33 @@
using Content.Shared.Hands.Components;
using static Robust.Shared.GameObjects.SharedSpriteComponent;
namespace Content.Client.Toggleable;
/// <summary>
/// Component that handles the toggling the visuals of some light emitting entity.
/// </summary>
/// <remarks>
/// This will toggle the visibility of layers on an entity's sprite, the in-hand visuals, and the clothing/equipment
/// visuals. This will modify the color of any attached point lights.
/// </remarks>
[RegisterComponent]
public sealed class ToggleableLightVisualsComponent : Component
{
/// <summary>
/// Sprite layer that will have it's visibility toggled when this item is toggled.
/// </summary>
[DataField("spriteLayer")]
public string SpriteLayer = "light";
/// <summary>
/// Layers to add to the sprite of the player that is holding this entity (while the component is toggled on).
/// </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 (while the component is toggled on).
/// </summary>
[DataField("clothingVisuals")]
public readonly Dictionary<string, List<PrototypeLayerData>> ClothingVisuals = new();
}

View File

@@ -0,0 +1,113 @@
using Content.Client.Clothing;
using Content.Client.Items.Systems;
using Content.Shared.Clothing;
using Content.Shared.Hands;
using Content.Shared.Item;
using Content.Shared.Toggleable;
using Robust.Client.GameObjects;
using Robust.Shared.Utility;
using System.Linq;
namespace Content.Client.Toggleable;
public sealed class ToggleableLightVisualsSystem : VisualizerSystem<ToggleableLightVisualsComponent>
{
[Dependency] private readonly SharedItemSystem _itemSys = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ToggleableLightVisualsComponent, GetInhandVisualsEvent>(OnGetHeldVisuals, after: new[] { typeof(ItemSystem) });
SubscribeLocalEvent<ToggleableLightVisualsComponent, GetEquipmentVisualsEvent>(OnGetEquipmentVisuals, after: new[] { typeof(ClothingSystem) });
}
protected override void OnAppearanceChange(EntityUid uid, ToggleableLightVisualsComponent component, ref AppearanceChangeEvent args)
{
if (!args.Component.TryGetData(ToggleableLightVisuals.Enabled, out bool enabled))
return;
var modulate = args.Component.TryGetData(ToggleableLightVisuals.Color, out Color color);
// Update the item's sprite
if (TryComp(uid, out SpriteComponent? sprite) && sprite.LayerMapTryGet(component.SpriteLayer, out var layer))
{
sprite.LayerSetVisible(layer, enabled);
if (modulate)
sprite.LayerSetColor(layer, color);
}
// Update any point-lights
if (TryComp(uid, out PointLightComponent? light))
{
DebugTools.Assert(!light.NetSyncEnabled, "light visualizers require point lights without net-sync");
light.Enabled = enabled;
if (enabled && modulate)
light.Color = color;
}
// update clothing & in-hand visuals.
_itemSys.VisualsChanged(uid);
}
/// <summary>
/// Add the unshaded light overlays to any clothing sprites.
/// </summary>
private void OnGetEquipmentVisuals(EntityUid uid, ToggleableLightVisualsComponent component, GetEquipmentVisualsEvent args)
{
if (!TryComp(uid, out AppearanceComponent? appearance)
|| !appearance.TryGetData(ToggleableLightVisuals.Enabled, out bool enabled)
|| !enabled)
return;
if (!component.ClothingVisuals.TryGetValue(args.Slot, out var layers))
return;
var modulate = appearance.TryGetData(ToggleableLightVisuals.Color, out Color color);
var i = 0;
foreach (var layer in layers)
{
var key = layer.MapKeys?.FirstOrDefault();
if (key == null)
{
key = i == 0 ? $"{args.Slot}-toggle" : $"{args.Slot}-toggle-{i}";
i++;
}
if (modulate)
layer.Color = color;
args.Layers.Add((key, layer));
}
}
private void OnGetHeldVisuals(EntityUid uid, ToggleableLightVisualsComponent component, GetInhandVisualsEvent args)
{
if (!TryComp(uid, out AppearanceComponent? appearance)
|| !appearance.TryGetData(ToggleableLightVisuals.Enabled, out bool enabled)
|| !enabled)
return;
if (!component.InhandVisuals.TryGetValue(args.Location, out var layers))
return;
var modulate = appearance.TryGetData(ToggleableLightVisuals.Color, out Color color);
var i = 0;
var defaultKey = $"inhand-{args.Location.ToString().ToLowerInvariant()}-toggle";
foreach (var layer in layers)
{
var key = layer.MapKeys?.FirstOrDefault();
if (key == null)
{
key = i == 0 ? defaultKey : $"{defaultKey}-{i}";
i++;
}
if (modulate)
layer.Color = color;
args.Layers.Add((key, layer));
}
}
}

View File

@@ -1,96 +0,0 @@
using Content.Shared.Item;
using Content.Shared.Weapons.Melee;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
namespace Content.Client.Weapons.Melee;
public sealed class EnergySwordVisualizer : AppearanceVisualizer
{
public override void OnChangeData(AppearanceComponent component)
{
base.OnChangeData(component);
var entManager = IoCManager.Resolve<IEntityManager>();
component.TryGetData(EnergySwordVisuals.State, out EnergySwordStatus? status);
status ??= EnergySwordStatus.Off;
component.TryGetData(EnergySwordVisuals.Color, out Color? color);
color ??= Color.DodgerBlue;
entManager.TryGetComponent(component.Owner, out SpriteComponent? spriteComponent);
if ((status & EnergySwordStatus.On) != 0x0)
{
TurnOn(component, status.Value, color.Value, entManager, spriteComponent);
}
else
{
TurnOff(component, status.Value, entManager, spriteComponent);
}
}
private void TurnOn(
AppearanceComponent component,
EnergySwordStatus status,
Color color,
IEntityManager entManager,
SpriteComponent? spriteComponent = null)
{
if ((status & EnergySwordStatus.Hacked) != 0x0)
{
if (entManager.TryGetComponent(component.Owner, out SharedItemComponent? itemComponent))
{
itemComponent.EquippedPrefix = "on-rainbow";
}
//todo: figure out how to use the RGBLightControllerSystem to phase out the rainbow sprite AND add lights.
spriteComponent?.LayerSetColor(1, Color.White);
spriteComponent?.LayerSetVisible(1, false);
spriteComponent?.LayerSetState(0, "e_sword_rainbow_on");
}
else
{
if (entManager.TryGetComponent(component.Owner, out SharedItemComponent? itemComponent))
{
itemComponent.EquippedPrefix = "on";
itemComponent.Color = color;
}
spriteComponent?.LayerSetColor(1, color);
spriteComponent?.LayerSetVisible(1, true);
if (entManager.TryGetComponent(component.Owner, out PointLightComponent? pointLightComponent))
{
pointLightComponent.Color = color;
pointLightComponent.Enabled = true;
}
}
}
private void TurnOff(
AppearanceComponent component,
EnergySwordStatus status,
IEntityManager entManager,
SpriteComponent? spriteComponent = null)
{
if (entManager.TryGetComponent(component.Owner, out SharedItemComponent? itemComponent))
{
itemComponent.EquippedPrefix = "off";
}
if ((status & EnergySwordStatus.Hacked) != 0x0)
{
spriteComponent?.LayerSetState(0, "e_sword");
}
else
{
spriteComponent?.LayerSetVisible(1, false);
}
if (entManager.TryGetComponent(component.Owner, out PointLightComponent? pointLightComponent))
{
pointLightComponent.Enabled = false;
}
}
}