Skip to content

Commit

Permalink
Try fix NREs
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectroJr committed Jan 13, 2025
1 parent 5bc336b commit c3ec771
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 43 deletions.
38 changes: 30 additions & 8 deletions Robust.Client/GameObjects/Components/Renderable/SpriteComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,8 @@ void ISerializationHooks.AfterDeserialization()
Layers.Clear();
foreach (var datum in layerDatums)
{
var layer = new Layer();
var layer = new Layer((Owner, this), Layers.Count);
Layers.Add(layer);
layer.Owner = (Owner, this);
layer.Index = Layers.Count - 1;
LayerSetData(layer, datum);
}

Expand All @@ -310,7 +308,7 @@ void ISerializationHooks.AfterDeserialization()
[Obsolete("Use SpriteSystem.CopySprite() instead.")]
public void CopyFrom(SpriteComponent other)
{
Sys.CopySprite(other, this);
Sys.CopySprite((other.Owner, other), (Owner, this));
}

[Obsolete("Use LocalMatrix")]
Expand Down Expand Up @@ -1147,11 +1145,20 @@ public sealed class Layer : ISpriteLayer, ISerializationHooks
{
internal SpriteComponent _parent => Owner.Comp;

/// <summary>
/// The entity that this layer belongs to.
/// </summary>
[Access(typeof(SpriteSystem), typeof(SpriteComponent))]
[ViewVariables] public Entity<SpriteComponent> Owner;
[ViewVariables] internal Entity<SpriteComponent> Owner;
// Internal, because I might want to change this in future W/O breaking changes.
// Also, it's possible for SpriteComponent to be null if it is not currently attached to a sprite.

/// <summary>
/// The index of the layer within its layer collection (usually a SpriteComponent).
/// </summary>
[Access(typeof(SpriteSystem), typeof(SpriteComponent))]
[ViewVariables] public int Index;
[ViewVariables] internal int Index;
// Internal, because I might want to change this in future W/O breaking changes.

[ViewVariables] public string? ShaderPrototype;
[ViewVariables] public ShaderInstance? Shader;
Expand Down Expand Up @@ -1320,11 +1327,24 @@ public Vector2 Offset
[ViewVariables(VVAccess.ReadWrite)]
public CopyToShaderParameters? CopyToShaderParameters;

public Layer()
[Obsolete("Use SpriteSystem.AddBlankLayer")]
public Layer(SpriteComponent parent)
{
Owner = (parent.Owner, parent);
}

internal Layer()
{
}

internal Layer(Entity<SpriteComponent> owner, int index)
{
Owner = owner;
Index = index;
}

public Layer(Layer toClone, SpriteComponent _)
[Obsolete] // This should be internal to SpriteSystem
public Layer(Layer toClone, SpriteComponent parent) : this(parent)
{
if (toClone.Shader != null)
{
Expand All @@ -1350,6 +1370,8 @@ public Layer(Layer toClone, SpriteComponent _)
CopyToShaderParameters = new CopyToShaderParameters(copyToShaderParameters);
}

// TODO SPRITE
// Is Layer even serializable?
void ISerializationHooks.AfterDeserialization()
{
UpdateLocalMatrix();
Expand Down
60 changes: 34 additions & 26 deletions Robust.Client/GameObjects/EntitySystems/SpriteSystem.Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,42 +55,50 @@ public void CopySprite(Entity<SpriteComponent?> source, Entity<SpriteComponent?>
if (!Resolve(target.Owner, ref target.Comp))
return;

CopySprite(source.Comp, target.Comp);
}

public void CopySprite(SpriteComponent source, SpriteComponent target)
{
target._baseRsi = source._baseRsi;
target._bounds = source._bounds;
target._visible = source._visible;
target.color = source.color;
target.offset = source.offset;
target.rotation = source.rotation;
target.scale = source.scale;
target.LocalMatrix = Matrix3Helpers.CreateTransform(in target.offset, in target.rotation, in target.scale);
target.drawDepth = source.drawDepth;
target.NoRotation = source.NoRotation;
target.DirectionOverride = source.DirectionOverride;
target.EnableDirectionOverride = source.EnableDirectionOverride;
target.Layers = new List<SpriteComponent.Layer>(source.Layers.Count);
foreach (var otherLayer in source.Layers)
target.Comp._baseRsi = source.Comp._baseRsi;
target.Comp._bounds = source.Comp._bounds;
target.Comp._visible = source.Comp._visible;
target.Comp.color = source.Comp.color;
target.Comp.offset = source.Comp.offset;
target.Comp.rotation = source.Comp.rotation;
target.Comp.scale = source.Comp.scale;
target.Comp.LocalMatrix = Matrix3Helpers.CreateTransform(
in target.Comp.offset,
in target.Comp.rotation,
in target
.Comp.scale);

target.Comp.drawDepth = source.Comp.drawDepth;
target.Comp.NoRotation = source.Comp.NoRotation;
target.Comp.DirectionOverride = source.Comp.DirectionOverride;
target.Comp.EnableDirectionOverride = source.Comp.EnableDirectionOverride;
target.Comp.Layers = new List<SpriteComponent.Layer>(source.Comp.Layers.Count);
foreach (var otherLayer in source.Comp.Layers)
{
target.Layers.Add(new SpriteComponent.Layer(otherLayer, target));
var layer = new SpriteComponent.Layer(otherLayer, target.Comp);
layer.Index = target.Comp.Layers.Count;
layer.Owner = target!;
target.Comp.Layers.Add(layer);
}

target.IsInert = source.IsInert;
target.LayerMap = source.LayerMap.ShallowClone();
target.PostShader = source.PostShader is {Mutable: true} ? source.PostShader.Duplicate() : source.PostShader;
target.RenderOrder = source.RenderOrder;
target.GranularLayersRendering = source.GranularLayersRendering;
target.Comp.IsInert = source.Comp.IsInert;
target.Comp.LayerMap = source.Comp.LayerMap.ShallowClone();
target.Comp.PostShader = source.Comp.PostShader is {Mutable: true}
? source.Comp.PostShader.Duplicate()
: source.Comp.PostShader;

target.Comp.RenderOrder = source.Comp.RenderOrder;
target.Comp.GranularLayersRendering = source.Comp.GranularLayersRendering;

_tree.QueueTreeUpdate(target!);
}

public void RebuildBounds(Entity<SpriteComponent> sprite)
{
// TODO SPRITE
// Maybe the bounds calculation should be deferred?
// The tree update is already deferred anyways.
// And layer.CalculateBoundingBox() is relatively expensive.
// And layer.CalculateBoundingBox() is relatively expensive for sprites like players

var bounds = new Box2();
foreach (var layer in sprite.Comp.Layers)
Expand Down
20 changes: 11 additions & 9 deletions Robust.Client/GameObjects/EntitySystems/SpriteSystem.Layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public int AddLayer(Entity<SpriteComponent?> sprite, Layer layer, int? index = n
return -1;
}

layer.Owner = sprite.Owner!;
layer.Owner = sprite!;

if (index is { } i && i != sprite.Comp.Layers.Count)
{
Expand Down Expand Up @@ -144,8 +144,11 @@ public int AddLayer(Entity<SpriteComponent?> sprite, Layer layer, int? index = n
}
#endif

RebuildBounds(sprite!);
QueueUpdateIsInert(sprite!);
if (!layer.Blank)
{
RebuildBounds(sprite!);
QueueUpdateIsInert(sprite!);
}
return layer.Index;
}

Expand All @@ -162,15 +165,14 @@ public int AddRsiLayer(Entity<SpriteComponent?> sprite, RSI.StateId stateId, RSI
if (!_query.Resolve(sprite.Owner, ref sprite.Comp))
return -1;

var layer = new Layer {State = stateId, RSI = rsi};
rsi ??= sprite.Comp._baseRsi;
var layer = AddBlankLayer(sprite!, index);

if (rsi != null && rsi.TryGetState(stateId, out var state))
layer.AnimationTimeLeft = state.GetDelay(0);
if (rsi != null)
LayerSetRsi(layer, rsi, stateId);
else
Log.Error($"State does not exist in RSI: '{stateId}'. Trace:\n{Environment.StackTrace}");
LayerSetRsiState(layer, stateId);

return AddLayer(sprite, layer, index);
return layer.Index;
}

/// <summary>
Expand Down

0 comments on commit c3ec771

Please sign in to comment.