Files
tbd-station-14/Content.Client/Sprite/RandomSpriteSystem.cs
Ed 986ac589b4 Fashion is my profession - Procedural jumpsuit variants (#25888)
* add color field to clothing layers

* add support to randomsprite

* bababa

* finalize spriting work

* add to game

* fix

* remove space

* edit patelle, +1 decor variant

* added only pants, some sprite fix

* inflation

* fix mixed

* not tested commit

* Revert "not tested commit"

This reverts commit 4a904df3452263e87c9cb819ab5d8cf411ebe468.

* naked human is fun

* update

* add new style

* some sprite pixel tweak

* Update meta.json
2024-03-15 18:37:12 +11:00

85 lines
2.9 KiB
C#

using Content.Client.Clothing;
using Content.Shared.Clothing.Components;
using Content.Shared.Sprite;
using Robust.Client.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Reflection;
namespace Content.Client.Sprite;
public sealed class RandomSpriteSystem : SharedRandomSpriteSystem
{
[Dependency] private readonly IReflectionManager _reflection = default!;
[Dependency] private readonly ClientClothingSystem _clothing = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RandomSpriteComponent, ComponentHandleState>(OnHandleState);
}
private void OnHandleState(EntityUid uid, RandomSpriteComponent component, ref ComponentHandleState args)
{
if (args.Current is not RandomSpriteColorComponentState state)
return;
if (state.Selected.Equals(component.Selected))
return;
component.Selected.Clear();
component.Selected.EnsureCapacity(state.Selected.Count);
foreach (var layer in state.Selected)
{
component.Selected.Add(layer.Key, layer.Value);
}
UpdateSpriteComponentAppearance(uid, component);
UpdateClothingComponentAppearance(uid, component);
}
private void UpdateClothingComponentAppearance(EntityUid uid, RandomSpriteComponent component, ClothingComponent? clothing = null)
{
if (!Resolve(uid, ref clothing, false))
return;
if (clothing.ClothingVisuals == null)
return;
foreach (var slotPair in clothing.ClothingVisuals)
{
foreach (var keyColorPair in component.Selected)
{
_clothing.SetLayerColor(clothing, slotPair.Key, keyColorPair.Key, keyColorPair.Value.Color);
_clothing.SetLayerState(clothing, slotPair.Key, keyColorPair.Key, keyColorPair.Value.State);
}
}
}
private void UpdateSpriteComponentAppearance(EntityUid uid, RandomSpriteComponent component, SpriteComponent? sprite = null)
{
if (!Resolve(uid, ref sprite, false))
return;
foreach (var layer in component.Selected)
{
int index;
if (_reflection.TryParseEnumReference(layer.Key, out var @enum))
{
if (!sprite.LayerMapTryGet(@enum, out index, logError: true))
continue;
}
else if (!sprite.LayerMapTryGet(layer.Key, out index))
{
if (layer.Key is not { } strKey || !int.TryParse(strKey, out index))
{
Log.Error($"Invalid key `{layer.Key}` for entity with random sprite {ToPrettyString(uid)}");
continue;
}
}
sprite.LayerSetState(index, layer.Value.State);
sprite.LayerSetColor(index, layer.Value.Color ?? Color.White);
}
}
}