fix: Transform2D fields & parental transform references not working properly fixed

This commit is contained in:
Syntriax 2025-04-06 11:21:59 +03:00
parent 901585d4bb
commit 04d325f38b

View File

@ -92,10 +92,13 @@ public class Transform2D : Behaviour, ITransform2D
return; return;
Vector2D previousScale = _scale; Vector2D previousScale = _scale;
Vector2D previousPosition = _position;
_localScale = value; _localScale = value;
UpdateScale(); UpdateScale();
UpdatePosition();
OnScaleChanged?.Invoke(this, previousScale); OnScaleChanged?.Invoke(this, previousScale);
OnPositionChanged?.Invoke(this, previousPosition);
} }
} }
@ -120,13 +123,9 @@ public class Transform2D : Behaviour, ITransform2D
if (parentTransform is null) if (parentTransform is null)
return; return;
float previousRotation = Rotation;
UpdatePosition(); UpdatePosition();
UpdateRotation();
OnPositionChanged?.Invoke(this, previousPosition); OnPositionChanged?.Invoke(this, previousPosition);
OnRotationChanged?.Invoke(this, previousRotation);
} }
private void RecalculateScale(ITransform2D _, Vector2D previousScale) private void RecalculateScale(ITransform2D _, Vector2D previousScale)
@ -134,9 +133,13 @@ public class Transform2D : Behaviour, ITransform2D
if (parentTransform is null) if (parentTransform is null)
return; return;
Vector2D previousPosition = _position;
UpdateScale(); UpdateScale();
UpdatePosition();
OnScaleChanged?.Invoke(this, previousScale); OnScaleChanged?.Invoke(this, previousScale);
OnPositionChanged?.Invoke(this, previousPosition);
} }
private void RecalculateRotation(ITransform2D _, float previousRotation) private void RecalculateRotation(ITransform2D _, float previousRotation)
@ -146,11 +149,11 @@ public class Transform2D : Behaviour, ITransform2D
Vector2D previousPosition = Position; Vector2D previousPosition = Position;
UpdatePosition();
UpdateRotation(); UpdateRotation();
UpdatePosition();
OnPositionChanged?.Invoke(this, previousPosition);
OnRotationChanged?.Invoke(this, previousRotation); OnRotationChanged?.Invoke(this, previousRotation);
OnPositionChanged?.Invoke(this, previousPosition);
} }
private void UpdateLocalPosition() private void UpdateLocalPosition()
@ -203,16 +206,16 @@ public class Transform2D : Behaviour, ITransform2D
protected override void InitializeInternal() protected override void InitializeInternal()
{ {
UpdateParent(BehaviourController.HierarchyObject.Parent); UpdateReferences(HierarchyObject.Parent);
BehaviourController.HierarchyObject.OnParentChanged += OnParentChanged; HierarchyObject.OnParentChanged += OnParentChanged;
} }
protected override void FinalizeInternal() protected override void FinalizeInternal()
{ {
BehaviourController.HierarchyObject.OnParentChanged -= OnParentChanged; HierarchyObject.OnParentChanged -= OnParentChanged;
} }
private void UpdateParent(IHierarchyObject? parent) private void UpdateReferences(IHierarchyObject? parent)
{ {
ITransform2D? previousParent = parentTransform; ITransform2D? previousParent = parentTransform;
if (previousParent is not null) if (previousParent is not null)
@ -222,6 +225,7 @@ public class Transform2D : Behaviour, ITransform2D
previousParent.OnScaleChanged -= RecalculateScale; previousParent.OnScaleChanged -= RecalculateScale;
previousParent.OnRotationChanged -= RecalculateRotation; previousParent.OnRotationChanged -= RecalculateRotation;
previousParent.BehaviourController.HierarchyObject.OnParentChanged -= OnParentChanged; previousParent.BehaviourController.HierarchyObject.OnParentChanged -= OnParentChanged;
previousParent.BehaviourController.OnBehaviourAdded -= LookForTransform2D;
} }
parentTransform = parent?.BehaviourController.GetBehaviour<ITransform2D>(); parentTransform = parent?.BehaviourController.GetBehaviour<ITransform2D>();
@ -238,6 +242,8 @@ public class Transform2D : Behaviour, ITransform2D
UpdateScale(); UpdateScale();
UpdateRotation(); UpdateRotation();
} }
else if (HierarchyObject.Parent is not null)
HierarchyObject.Parent.BehaviourController.OnBehaviourAdded += LookForTransform2D;
UpdateLocalPosition(); UpdateLocalPosition();
UpdateLocalScale(); UpdateLocalScale();
@ -248,8 +254,16 @@ public class Transform2D : Behaviour, ITransform2D
OnRotationChanged?.Invoke(this, Rotation); OnRotationChanged?.Invoke(this, Rotation);
} }
private void LookForTransform2D(IBehaviourController sender, IBehaviour behaviourAdded)
{
if (behaviourAdded is not ITransform2D transform2D)
return;
UpdateReferences(HierarchyObject.Parent);
}
private void OnParentChanged(IHierarchyObject sender, IHierarchyObject? previousParent, IHierarchyObject? newParent) private void OnParentChanged(IHierarchyObject sender, IHierarchyObject? previousParent, IHierarchyObject? newParent)
{ {
UpdateParent(newParent); UpdateReferences(newParent);
} }
} }