refactor: Improved & Fixed Issues with Transforms with Parents
This commit is contained in:
parent
ea94bed00d
commit
81625abd25
|
@ -99,7 +99,7 @@ public interface ITransform : IAssignableGameObject, IEnumerable<ITransform>
|
||||||
delegate void OnPositionChangedDelegate(ITransform sender);
|
delegate void OnPositionChangedDelegate(ITransform sender);
|
||||||
delegate void OnScaleChangedDelegate(ITransform sender);
|
delegate void OnScaleChangedDelegate(ITransform sender);
|
||||||
delegate void OnRotationChangedDelegate(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 OnChildrenAddedDelegate(ITransform sender, ITransform childrenAdded);
|
||||||
delegate void OnChildrenRemovedDelegate(ITransform sender, ITransform childrenRemoved);
|
delegate void OnChildrenRemovedDelegate(ITransform sender, ITransform childrenRemoved);
|
||||||
}
|
}
|
||||||
|
|
|
@ -140,6 +140,9 @@ public class GameManager : BaseEntity, IGameManager
|
||||||
gameObject.OnFinalized += OnGameObjectFinalize;
|
gameObject.OnFinalized += OnGameObjectFinalize;
|
||||||
gameObject.OnExitedHierarchy += OnGameObjectExitedHierarchy;
|
gameObject.OnExitedHierarchy += OnGameObjectExitedHierarchy;
|
||||||
|
|
||||||
|
foreach (ITransform child in gameObject.Transform.Children)
|
||||||
|
Register(child.GameObject);
|
||||||
|
|
||||||
if (!gameObject.Initialize())
|
if (!gameObject.Initialize())
|
||||||
throw new Exception($"{nameof(gameObject)} can't be initialized");
|
throw new Exception($"{nameof(gameObject)} can't be initialized");
|
||||||
|
|
||||||
|
@ -161,6 +164,9 @@ public class GameManager : BaseEntity, IGameManager
|
||||||
gameObject.OnFinalized -= OnGameObjectFinalize;
|
gameObject.OnFinalized -= OnGameObjectFinalize;
|
||||||
gameObject.OnExitedHierarchy -= OnGameObjectExitedHierarchy;
|
gameObject.OnExitedHierarchy -= OnGameObjectExitedHierarchy;
|
||||||
|
|
||||||
|
foreach (ITransform child in gameObject.Transform.Children)
|
||||||
|
Unregister(child.GameObject);
|
||||||
|
|
||||||
_gameObjects.Remove(gameObject);
|
_gameObjects.Remove(gameObject);
|
||||||
_hierarchyObjects.Remove(gameObject);
|
_hierarchyObjects.Remove(gameObject);
|
||||||
|
|
||||||
|
|
|
@ -71,6 +71,8 @@ public class GameObject : BaseEntity, IGameObject
|
||||||
|
|
||||||
_transform = transform;
|
_transform = transform;
|
||||||
OnTransformAssigned?.Invoke(this);
|
OnTransformAssigned?.Invoke(this);
|
||||||
|
transform.OnParentChanged += OnParentChangedInternal;
|
||||||
|
OnParentChangedInternal(transform, null, transform.Parent);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,6 +90,8 @@ public class GameObject : BaseEntity, IGameObject
|
||||||
{
|
{
|
||||||
base.UnassignInternal();
|
base.UnassignInternal();
|
||||||
|
|
||||||
|
_transform.OnParentChanged -= OnParentChangedInternal;
|
||||||
|
OnParentChangedInternal(_transform, _transform.Parent, null);
|
||||||
_transform = null!;
|
_transform = null!;
|
||||||
_behaviourController = null!;
|
_behaviourController = null!;
|
||||||
_gameManager = null!;
|
_gameManager = null!;
|
||||||
|
@ -103,6 +107,25 @@ public class GameObject : BaseEntity, IGameObject
|
||||||
private void OnBehaviourRemoved(IBehaviourController _, IBehaviour behaviour) { if (IsInitialized) behaviour.Finalize(); }
|
private void OnBehaviourRemoved(IBehaviourController _, IBehaviour behaviour) { if (IsInitialized) behaviour.Finalize(); }
|
||||||
private void OnBehaviourAdded(IBehaviourController _, IBehaviour behaviour) { if (!IsInitialized) behaviour.Initialize(); }
|
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)
|
bool IHierarchyObject.EnterHierarchy(IGameManager gameManager)
|
||||||
{
|
{
|
||||||
if (IsInHierarchy)
|
if (IsInHierarchy)
|
||||||
|
|
|
@ -150,7 +150,7 @@ public class Transform : ITransform
|
||||||
UpdateLocalScale();
|
UpdateLocalScale();
|
||||||
UpdateLocalRotation();
|
UpdateLocalRotation();
|
||||||
|
|
||||||
OnParentChanged?.Invoke(this, previousParent);
|
OnParentChanged?.Invoke(this, previousParent, transform);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddChild(ITransform transform)
|
public void AddChild(ITransform transform)
|
||||||
|
@ -175,7 +175,7 @@ public class Transform : ITransform
|
||||||
public IEnumerator<ITransform> GetEnumerator() => _children.GetEnumerator();
|
public IEnumerator<ITransform> GetEnumerator() => _children.GetEnumerator();
|
||||||
IEnumerator IEnumerable.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.
|
// 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?
|
// I was originally gonna just call `child.OnParentChanged?.Invoke(child, child.Parent);` but seems an unnecessary call too?
|
||||||
|
|
|
@ -46,7 +46,7 @@ public abstract class Collider2DBehaviourBase : Behaviour, ICollider2D
|
||||||
Transform.OnParentChanged += UpdateRigidBody2D;
|
Transform.OnParentChanged += UpdateRigidBody2D;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateRigidBody2D(ITransform _1, ITransform? _2)
|
private void UpdateRigidBody2D(ITransform sender, ITransform? previousParent, ITransform? newParent)
|
||||||
{
|
{
|
||||||
BehaviourController.TryGetBehaviourInParent(out _rigidBody2D);
|
BehaviourController.TryGetBehaviourInParent(out _rigidBody2D);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue