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