refactor: Improved & Fixed Issues with Transforms with Parents

This commit is contained in:
Syntriax 2024-11-10 19:18:44 +03:00
parent ea94bed00d
commit 81625abd25
5 changed files with 33 additions and 4 deletions

View File

@ -99,7 +99,7 @@ public interface ITransform : IAssignableGameObject, IEnumerable<ITransform>
delegate void OnPositionChangedDelegate(ITransform sender);
delegate void OnScaleChangedDelegate(ITransform sender);
delegate void OnRotationChangedDelegate(ITransform sender);
delegate void OnParentChangedDelegate(ITransform sender, ITransform? newParent);
delegate void OnParentChangedDelegate(ITransform sender, ITransform? previousParent, ITransform? newParent);
delegate void OnChildrenAddedDelegate(ITransform sender, ITransform childrenAdded);
delegate void OnChildrenRemovedDelegate(ITransform sender, ITransform childrenRemoved);
}

View File

@ -140,6 +140,9 @@ public class GameManager : BaseEntity, IGameManager
gameObject.OnFinalized += OnGameObjectFinalize;
gameObject.OnExitedHierarchy += OnGameObjectExitedHierarchy;
foreach (ITransform child in gameObject.Transform.Children)
Register(child.GameObject);
if (!gameObject.Initialize())
throw new Exception($"{nameof(gameObject)} can't be initialized");
@ -161,6 +164,9 @@ public class GameManager : BaseEntity, IGameManager
gameObject.OnFinalized -= OnGameObjectFinalize;
gameObject.OnExitedHierarchy -= OnGameObjectExitedHierarchy;
foreach (ITransform child in gameObject.Transform.Children)
Unregister(child.GameObject);
_gameObjects.Remove(gameObject);
_hierarchyObjects.Remove(gameObject);

View File

@ -71,6 +71,8 @@ public class GameObject : BaseEntity, IGameObject
_transform = transform;
OnTransformAssigned?.Invoke(this);
transform.OnParentChanged += OnParentChangedInternal;
OnParentChangedInternal(transform, null, transform.Parent);
return true;
}
@ -88,6 +90,8 @@ public class GameObject : BaseEntity, IGameObject
{
base.UnassignInternal();
_transform.OnParentChanged -= OnParentChangedInternal;
OnParentChangedInternal(_transform, _transform.Parent, null);
_transform = null!;
_behaviourController = null!;
_gameManager = null!;
@ -103,6 +107,25 @@ public class GameObject : BaseEntity, IGameObject
private void OnBehaviourRemoved(IBehaviourController _, IBehaviour behaviour) { if (IsInitialized) behaviour.Finalize(); }
private void OnBehaviourAdded(IBehaviourController _, IBehaviour behaviour) { if (!IsInitialized) behaviour.Initialize(); }
private void OnParentChangedInternal(ITransform sender, ITransform? previousParent, ITransform? newParent)
{
if (previousParent is not null)
{
previousParent.OnParentChanged -= OnParentChangedInternal;
previousParent.GameObject.OnEnteredHierarchy -= EnterHierarchyInternal;
previousParent.GameObject.OnExitedHierarchy -= ExitHierarchyInternal;
}
if (newParent is not null)
{
newParent.OnParentChanged += OnParentChangedInternal;
newParent.GameObject.OnEnteredHierarchy += EnterHierarchyInternal;
newParent.GameObject.OnExitedHierarchy += ExitHierarchyInternal;
}
}
private void EnterHierarchyInternal(IHierarchyObject sender, IGameManager gameManager) => ((IHierarchyObject)this).EnterHierarchy(gameManager);
private void ExitHierarchyInternal(IHierarchyObject sender, IGameManager gameManager) => ((IHierarchyObject)this).ExitHierarchy();
bool IHierarchyObject.EnterHierarchy(IGameManager gameManager)
{
if (IsInHierarchy)

View File

@ -150,7 +150,7 @@ public class Transform : ITransform
UpdateLocalScale();
UpdateLocalRotation();
OnParentChanged?.Invoke(this, previousParent);
OnParentChanged?.Invoke(this, previousParent, transform);
}
public void AddChild(ITransform transform)
@ -175,7 +175,7 @@ public class Transform : ITransform
public IEnumerator<ITransform> GetEnumerator() => _children.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _children.GetEnumerator();
private void NotifyChildrenOnParentChange(ITransform transform, ITransform? previousParent)
private void NotifyChildrenOnParentChange(ITransform transform, ITransform? previousParent, ITransform? newParent)
{
// TODO No idea how logical this is to propagate this to the children the way I'm doing right now.
// I was originally gonna just call `child.OnParentChanged?.Invoke(child, child.Parent);` but seems an unnecessary call too?

View File

@ -46,7 +46,7 @@ public abstract class Collider2DBehaviourBase : Behaviour, ICollider2D
Transform.OnParentChanged += UpdateRigidBody2D;
}
private void UpdateRigidBody2D(ITransform _1, ITransform? _2)
private void UpdateRigidBody2D(ITransform sender, ITransform? previousParent, ITransform? newParent)
{
BehaviourController.TryGetBehaviourInParent(out _rigidBody2D);
}