feat: Improved Entering & Exiting of Hierarchy Objects
This commit is contained in:
parent
cc44e1ea69
commit
4856800f5f
|
@ -49,14 +49,16 @@ public interface IHierarchyObject : IEntity, INameable
|
|||
internal bool ExitHierarchy();
|
||||
|
||||
/// <summary>
|
||||
/// Delegate type for the event triggered when the <see cref="IEntity"/> enters the hierarchy.
|
||||
/// Delegate type for the event triggered when the <see cref="IEntity"/> enters the hierarchy of a <see cref="IGameManager">.
|
||||
/// </summary>
|
||||
/// <param name="sender">The <see cref="IEntity"/> that entered the hierarchy.</param>
|
||||
delegate void OnEnteredHierarchyDelegate(IHierarchyObject sender);
|
||||
/// <param name="gameManager">The <see cref="IGameManager"/> that the <see cref="IEntity"/> has entered it's hierarchy.</param>
|
||||
delegate void OnEnteredHierarchyDelegate(IHierarchyObject sender, IGameManager gameManager);
|
||||
|
||||
/// <summary>
|
||||
/// Delegate type for the event triggered when the <see cref="IEntity"/> exits the hierarchy.
|
||||
/// Delegate type for the event triggered when the <see cref="IEntity"/> exits the hierarchy of a <see cref="IGameManager">.
|
||||
/// </summary>
|
||||
/// <param name="sender">The <see cref="IEntity"/> that exited the hierarchy.</param>
|
||||
delegate void OnExitedHierarchyDelegate(IHierarchyObject sender);
|
||||
/// <param name="gameManager">The <see cref="IGameManager"/> that the <see cref="IEntity"/> has exited it's hierarchy.</param>
|
||||
delegate void OnExitedHierarchyDelegate(IHierarchyObject sender, IGameManager gameManager);
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ public abstract class Behaviour : BehaviourBase
|
|||
OnInitialize();
|
||||
|
||||
if (GameObject.IsInHierarchy)
|
||||
EnteredHierarchy(GameObject);
|
||||
EnteredHierarchy(GameObject, GameObject.GameManager ?? throw new System.Exception("Unexpected Error"));
|
||||
}
|
||||
|
||||
protected virtual void OnFinalize() { }
|
||||
|
@ -44,7 +44,11 @@ public abstract class Behaviour : BehaviourBase
|
|||
BehaviourController.OnUpdate -= Update;
|
||||
BehaviourController.GameObject.OnEnteredHierarchy -= EnteredHierarchy;
|
||||
BehaviourController.GameObject.OnExitedHierarchy -= ExitedHierarchy;
|
||||
|
||||
OnFinalize();
|
||||
|
||||
if (GameObject.IsInHierarchy)
|
||||
ExitedHierarchy(GameObject, GameObject.GameManager ?? throw new System.Exception("Unexpected Error"));
|
||||
}
|
||||
|
||||
protected virtual void OnPreUpdatePreActiveCheck() { }
|
||||
|
@ -93,9 +97,9 @@ public abstract class Behaviour : BehaviourBase
|
|||
OnPreDraw();
|
||||
}
|
||||
|
||||
protected virtual void OnEnteredHierarchy() { }
|
||||
private void EnteredHierarchy(IHierarchyObject sender) => OnEnteredHierarchy();
|
||||
protected virtual void OnEnteredHierarchy(IGameManager gameManager) { }
|
||||
private void EnteredHierarchy(IHierarchyObject sender, IGameManager gameManager) => OnEnteredHierarchy(gameManager);
|
||||
|
||||
protected virtual void OnExitedHierarchy() { }
|
||||
private void ExitedHierarchy(IHierarchyObject sender) => OnExitedHierarchy();
|
||||
protected virtual void OnExitedHierarchy(IGameManager gameManager) { }
|
||||
private void ExitedHierarchy(IHierarchyObject sender, IGameManager gameManager) => OnExitedHierarchy(gameManager);
|
||||
}
|
||||
|
|
|
@ -180,7 +180,7 @@ public class GameManager : BaseEntity, IGameManager
|
|||
Unregister(gameObject);
|
||||
}
|
||||
|
||||
private void OnGameObjectExitedHierarchy(IHierarchyObject sender)
|
||||
private void OnGameObjectExitedHierarchy(IHierarchyObject sender, IGameManager gameManager)
|
||||
{
|
||||
if (sender is IGameObject gameObject)
|
||||
Unregister(gameObject);
|
||||
|
|
|
@ -109,17 +109,17 @@ public class GameObject : BaseEntity, IGameObject
|
|||
return false;
|
||||
|
||||
_gameManager = gameManager;
|
||||
OnEnteredHierarchy?.Invoke(this);
|
||||
OnEnteredHierarchy?.Invoke(this, gameManager);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IHierarchyObject.ExitHierarchy()
|
||||
{
|
||||
if (!IsInHierarchy)
|
||||
if (!IsInHierarchy || _gameManager is not IGameManager gameManager)
|
||||
return false;
|
||||
|
||||
_gameManager = null;
|
||||
OnExitedHierarchy?.Invoke(this);
|
||||
OnExitedHierarchy?.Invoke(this, gameManager);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -124,21 +124,19 @@ public abstract class HierarchyObjectBase : IHierarchyObject
|
|||
|
||||
_gameManager = gameManager;
|
||||
OnEnteringHierarchy(gameManager);
|
||||
OnEnteredHierarchy?.Invoke(this);
|
||||
OnEnteredHierarchy?.Invoke(this, gameManager);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual void OnExitingHierarchy(IGameManager gameManager) { }
|
||||
bool IHierarchyObject.ExitHierarchy()
|
||||
{
|
||||
if (!IsInHierarchy)
|
||||
if (!IsInHierarchy || _gameManager is not IGameManager gameManager)
|
||||
return false;
|
||||
|
||||
IGameManager? previousGameManager = _gameManager;
|
||||
_gameManager = null;
|
||||
if (previousGameManager is not null)
|
||||
OnExitingHierarchy(previousGameManager);
|
||||
OnExitedHierarchy?.Invoke(this);
|
||||
OnExitingHierarchy(gameManager);
|
||||
OnExitedHierarchy?.Invoke(this, gameManager);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue