feat: Improved Entering & Exiting of Hierarchy Objects

This commit is contained in:
Syntriax 2024-11-03 20:13:25 +03:00
parent cc44e1ea69
commit 4856800f5f
5 changed files with 23 additions and 19 deletions

View File

@ -49,14 +49,16 @@ public interface IHierarchyObject : IEntity, INameable
internal bool ExitHierarchy(); internal bool ExitHierarchy();
/// <summary> /// <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> /// </summary>
/// <param name="sender">The <see cref="IEntity"/> that entered the hierarchy.</param> /// <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> /// <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> /// </summary>
/// <param name="sender">The <see cref="IEntity"/> that exited the hierarchy.</param> /// <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);
} }

View File

@ -33,7 +33,7 @@ public abstract class Behaviour : BehaviourBase
OnInitialize(); OnInitialize();
if (GameObject.IsInHierarchy) if (GameObject.IsInHierarchy)
EnteredHierarchy(GameObject); EnteredHierarchy(GameObject, GameObject.GameManager ?? throw new System.Exception("Unexpected Error"));
} }
protected virtual void OnFinalize() { } protected virtual void OnFinalize() { }
@ -44,7 +44,11 @@ public abstract class Behaviour : BehaviourBase
BehaviourController.OnUpdate -= Update; BehaviourController.OnUpdate -= Update;
BehaviourController.GameObject.OnEnteredHierarchy -= EnteredHierarchy; BehaviourController.GameObject.OnEnteredHierarchy -= EnteredHierarchy;
BehaviourController.GameObject.OnExitedHierarchy -= ExitedHierarchy; BehaviourController.GameObject.OnExitedHierarchy -= ExitedHierarchy;
OnFinalize(); OnFinalize();
if (GameObject.IsInHierarchy)
ExitedHierarchy(GameObject, GameObject.GameManager ?? throw new System.Exception("Unexpected Error"));
} }
protected virtual void OnPreUpdatePreActiveCheck() { } protected virtual void OnPreUpdatePreActiveCheck() { }
@ -93,9 +97,9 @@ public abstract class Behaviour : BehaviourBase
OnPreDraw(); OnPreDraw();
} }
protected virtual void OnEnteredHierarchy() { } protected virtual void OnEnteredHierarchy(IGameManager gameManager) { }
private void EnteredHierarchy(IHierarchyObject sender) => OnEnteredHierarchy(); private void EnteredHierarchy(IHierarchyObject sender, IGameManager gameManager) => OnEnteredHierarchy(gameManager);
protected virtual void OnExitedHierarchy() { } protected virtual void OnExitedHierarchy(IGameManager gameManager) { }
private void ExitedHierarchy(IHierarchyObject sender) => OnExitedHierarchy(); private void ExitedHierarchy(IHierarchyObject sender, IGameManager gameManager) => OnExitedHierarchy(gameManager);
} }

View File

@ -180,7 +180,7 @@ public class GameManager : BaseEntity, IGameManager
Unregister(gameObject); Unregister(gameObject);
} }
private void OnGameObjectExitedHierarchy(IHierarchyObject sender) private void OnGameObjectExitedHierarchy(IHierarchyObject sender, IGameManager gameManager)
{ {
if (sender is IGameObject gameObject) if (sender is IGameObject gameObject)
Unregister(gameObject); Unregister(gameObject);

View File

@ -109,17 +109,17 @@ public class GameObject : BaseEntity, IGameObject
return false; return false;
_gameManager = gameManager; _gameManager = gameManager;
OnEnteredHierarchy?.Invoke(this); OnEnteredHierarchy?.Invoke(this, gameManager);
return true; return true;
} }
bool IHierarchyObject.ExitHierarchy() bool IHierarchyObject.ExitHierarchy()
{ {
if (!IsInHierarchy) if (!IsInHierarchy || _gameManager is not IGameManager gameManager)
return false; return false;
_gameManager = null; _gameManager = null;
OnExitedHierarchy?.Invoke(this); OnExitedHierarchy?.Invoke(this, gameManager);
return true; return true;
} }
} }

View File

@ -124,21 +124,19 @@ public abstract class HierarchyObjectBase : IHierarchyObject
_gameManager = gameManager; _gameManager = gameManager;
OnEnteringHierarchy(gameManager); OnEnteringHierarchy(gameManager);
OnEnteredHierarchy?.Invoke(this); OnEnteredHierarchy?.Invoke(this, gameManager);
return true; return true;
} }
protected virtual void OnExitingHierarchy(IGameManager gameManager) { } protected virtual void OnExitingHierarchy(IGameManager gameManager) { }
bool IHierarchyObject.ExitHierarchy() bool IHierarchyObject.ExitHierarchy()
{ {
if (!IsInHierarchy) if (!IsInHierarchy || _gameManager is not IGameManager gameManager)
return false; return false;
IGameManager? previousGameManager = _gameManager;
_gameManager = null; _gameManager = null;
if (previousGameManager is not null) OnExitingHierarchy(gameManager);
OnExitingHierarchy(previousGameManager); OnExitedHierarchy?.Invoke(this, gameManager);
OnExitedHierarchy?.Invoke(this);
return true; return true;
} }
} }