Skip to content

Commit

Permalink
Fix LayerSetRsi
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectroJr committed Jan 13, 2025
1 parent 2c8a421 commit 5b39b2d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,40 @@ public void LayerSetRsiState(Entity<SpriteComponent?> 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<SpriteComponent> 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<SpriteComponent?> sprite, string key, StateId state)
Expand Down Expand Up @@ -200,28 +232,8 @@ public void LayerSetRsi(Entity<SpriteComponent?> 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<SpriteComponent?> sprite, string key, RSI? rsi, StateId? state = null)
Expand Down

0 comments on commit 5b39b2d

Please sign in to comment.