From 5b39b2d8b5a16a64fc0c37133be4c681a8bf221b Mon Sep 17 00:00:00 2001 From: ElectroJr Date: Mon, 13 Jan 2025 21:19:22 +1300 Subject: [PATCH] Fix LayerSetRsi --- .../Components/Renderable/SpriteComponent.cs | 4 +- .../SpriteSystem.LayerSetters.cs | 60 +++++++++++-------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/Robust.Client/GameObjects/Components/Renderable/SpriteComponent.cs b/Robust.Client/GameObjects/Components/Renderable/SpriteComponent.cs index f29bf20f205..6ad25799f60 100644 --- a/Robust.Client/GameObjects/Components/Renderable/SpriteComponent.cs +++ b/Robust.Client/GameObjects/Components/Renderable/SpriteComponent.cs @@ -1157,13 +1157,13 @@ public enum DirectionOffset : byte public sealed class Layer : ISpriteLayer, ISerializationHooks { - [ViewVariables] private readonly SpriteComponent _parent; + [ViewVariables] internal readonly SpriteComponent _parent; [ViewVariables] public string? ShaderPrototype; [ViewVariables] public ShaderInstance? Shader; [ViewVariables] public Texture? Texture; - private RSI? _rsi; + internal RSI? _rsi; [ViewVariables] public RSI? RSI { get => _rsi; diff --git a/Robust.Client/GameObjects/EntitySystems/SpriteSystem.LayerSetters.cs b/Robust.Client/GameObjects/EntitySystems/SpriteSystem.LayerSetters.cs index 20b3907ed9b..7fe6c802923 100644 --- a/Robust.Client/GameObjects/EntitySystems/SpriteSystem.LayerSetters.cs +++ b/Robust.Client/GameObjects/EntitySystems/SpriteSystem.LayerSetters.cs @@ -166,8 +166,40 @@ public void LayerSetRsiState(Entity sprite, int index, StateId if (!TryGetLayer(sprite, index, out var layer, true)) return; - layer.SetState(state); - RebuildBounds(sprite!); + LayerSetRsiState(sprite!, layer, state); + } + + public void LayerSetRsiState(Entity sprite, Layer layer, StateId state, bool refresh = false) + { + if (layer._parent != sprite.Comp) + throw new InvalidOperationException($"The given layer does not belong this entity."); + + if (layer.StateId == state && !refresh) + return; + + layer.StateId = state; + + if (!layer.StateId.IsValid) + { + layer._actualState = null; + } + else if (layer.ActualRsi is not {} rsi) + { + Log.Error($"{ToPrettyString(sprite)} has no RSI to pull new state from! Trace:\n{Environment.StackTrace}"); + layer._actualState = GetFallbackState(); + } + else if (!rsi.TryGetState(layer.StateId, out layer._actualState)) + { + layer._actualState = GetFallbackState(); + Log.Error($"{ToPrettyString(sprite)}'s state '{state}' does not exist in RSI {rsi.Path}. Trace:\n{Environment.StackTrace}"); + } + + layer.AnimationFrame = 0; + layer.AnimationTime = 0; + layer.AnimationTimeLeft = layer._actualState?.GetDelay(0) ?? 0f; + + RebuildBounds(sprite); + QueueUpdateIsInert(sprite); } public void LayerSetRsiState(Entity sprite, string key, StateId state) @@ -200,28 +232,8 @@ public void LayerSetRsi(Entity sprite, int index, RSI? rsi, St if (!TryGetLayer(sprite, index, out var layer, true)) return; - layer.RSI = rsi; - if (state != null) - layer.StateId = state.Value; - - layer.AnimationFrame = 0; - layer.AnimationTime = 0; - - var actualRsi = layer.RSI ?? sprite.Comp.BaseRSI; - if (actualRsi == null) - { - Log.Error($"Entity {ToPrettyString(sprite)} has no RSI to pull new state from! Trace:\n{Environment.StackTrace}"); - } - else - { - if (actualRsi.TryGetState(layer.StateId, out layer._actualState)) - layer.AnimationTimeLeft = layer._actualState.GetDelay(0); - else - Log.Error($"Entity {ToPrettyString(sprite)}'s state '{state}' does not exist in RSI {actualRsi.Path}. Trace:\n{Environment.StackTrace}"); - } - - RebuildBounds(sprite!); - QueueUpdateIsInert(sprite!); + layer._rsi = rsi; + LayerSetRsiState(sprite!, layer, state ?? layer.StateId, refresh: true); } public void LayerSetRsi(Entity sprite, string key, RSI? rsi, StateId? state = null)