From 4856800f5f06616770c04953ed143147d57372fa Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sun, 3 Nov 2024 20:13:25 +0300 Subject: [PATCH] feat: Improved Entering & Exiting of Hierarchy Objects --- .../Abstract/Assignable/IHierarchyObject.cs | 10 ++++++---- Engine.Core/Behaviour.cs | 14 +++++++++----- Engine.Core/GameManager.cs | 2 +- Engine.Core/GameObject.cs | 6 +++--- Engine.Core/HierarchyObjectBase.cs | 10 ++++------ 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/Engine.Core/Abstract/Assignable/IHierarchyObject.cs b/Engine.Core/Abstract/Assignable/IHierarchyObject.cs index 61fe5b3..be67532 100644 --- a/Engine.Core/Abstract/Assignable/IHierarchyObject.cs +++ b/Engine.Core/Abstract/Assignable/IHierarchyObject.cs @@ -49,14 +49,16 @@ public interface IHierarchyObject : IEntity, INameable internal bool ExitHierarchy(); /// - /// Delegate type for the event triggered when the enters the hierarchy. + /// Delegate type for the event triggered when the enters the hierarchy of a . /// /// The that entered the hierarchy. - delegate void OnEnteredHierarchyDelegate(IHierarchyObject sender); + /// The that the has entered it's hierarchy. + delegate void OnEnteredHierarchyDelegate(IHierarchyObject sender, IGameManager gameManager); /// - /// Delegate type for the event triggered when the exits the hierarchy. + /// Delegate type for the event triggered when the exits the hierarchy of a . /// /// The that exited the hierarchy. - delegate void OnExitedHierarchyDelegate(IHierarchyObject sender); + /// The that the has exited it's hierarchy. + delegate void OnExitedHierarchyDelegate(IHierarchyObject sender, IGameManager gameManager); } diff --git a/Engine.Core/Behaviour.cs b/Engine.Core/Behaviour.cs index 4b20863..a16cea0 100644 --- a/Engine.Core/Behaviour.cs +++ b/Engine.Core/Behaviour.cs @@ -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); } diff --git a/Engine.Core/GameManager.cs b/Engine.Core/GameManager.cs index 73e25a7..af7c6bd 100644 --- a/Engine.Core/GameManager.cs +++ b/Engine.Core/GameManager.cs @@ -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); diff --git a/Engine.Core/GameObject.cs b/Engine.Core/GameObject.cs index a82e692..794db4d 100644 --- a/Engine.Core/GameObject.cs +++ b/Engine.Core/GameObject.cs @@ -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; } } diff --git a/Engine.Core/HierarchyObjectBase.cs b/Engine.Core/HierarchyObjectBase.cs index ea855f2..9400f43 100644 --- a/Engine.Core/HierarchyObjectBase.cs +++ b/Engine.Core/HierarchyObjectBase.cs @@ -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; } }