diff --git a/Engine.Core/Abstract/BaseEntity.cs b/Engine.Core/Abstract/BaseEntity.cs index 499f4cf..ac68da3 100644 --- a/Engine.Core/Abstract/BaseEntity.cs +++ b/Engine.Core/Abstract/BaseEntity.cs @@ -1,5 +1,7 @@ using System; +using Syntriax.Engine.Core.Exceptions; + namespace Syntriax.Engine.Core.Abstract; public abstract class BaseEntity : IEntity @@ -72,6 +74,8 @@ public abstract class BaseEntity : IEntity UnassignInternal(); + _stateEnable = null!; + _stateEnable.Unassign(); OnUnassigned?.Invoke(this); return true; } @@ -82,6 +86,8 @@ public abstract class BaseEntity : IEntity if (IsInitialized) return false; + NotAssignedException.Check(this, _stateEnable); + InitializeInternal(); IsInitialized = true; diff --git a/Engine.Core/GameManager.cs b/Engine.Core/GameManager.cs index de387eb..d19c3e3 100644 --- a/Engine.Core/GameManager.cs +++ b/Engine.Core/GameManager.cs @@ -63,6 +63,10 @@ public class GameManager : BaseEntity, IGameManager { _hierarchyObjects.Add(hierarchyObject); hierarchyObject.EnterHierarchy(this); + + if (!hierarchyObject.Initialize()) + throw new Exception($"{nameof(hierarchyObject)} can't be finalized"); + OnHierarchyObjectRegistered?.Invoke(this, hierarchyObject); } } @@ -85,6 +89,10 @@ public class GameManager : BaseEntity, IGameManager { _hierarchyObjects.Remove(hierarchyObject); hierarchyObject.ExitHierarchy(); + + if (!hierarchyObject.Finalize()) + throw new Exception($"{nameof(hierarchyObject)} can't be finalized"); + OnHierarchyObjectUnRegistered?.Invoke(this, hierarchyObject); } } @@ -138,6 +146,9 @@ public class GameManager : BaseEntity, IGameManager if (!gameObject.EnterHierarchy(this)) throw new Exception($"{nameof(gameObject)} can't enter the hierarchy"); + if (!gameObject.Initialize()) + throw new Exception($"{nameof(gameObject)} can't be initialized"); + OnHierarchyObjectRegistered?.Invoke(this, gameObject); OnGameObjectRegistered?.Invoke(this, gameObject); } @@ -156,6 +167,9 @@ public class GameManager : BaseEntity, IGameManager if (!gameObject.ExitHierarchy()) throw new Exception($"{nameof(gameObject)} can't exit the hierarchy"); + if (!gameObject.Finalize()) + throw new Exception($"{nameof(gameObject)} can't be finalized"); + OnHierarchyObjectUnRegistered?.Invoke(this, gameObject); OnGameObjectUnRegistered?.Invoke(this, gameObject); } diff --git a/Engine.Core/GameObject.cs b/Engine.Core/GameObject.cs index 4162aef..814936e 100644 --- a/Engine.Core/GameObject.cs +++ b/Engine.Core/GameObject.cs @@ -18,7 +18,6 @@ public class GameObject : BaseEntity, IGameObject private ITransform _transform = null!; private IBehaviourController _behaviourController = null!; - private IStateEnable _stateEnable = null!; private IGameManager? _gameManager = null; private string _name = nameof(GameObject); @@ -47,13 +46,12 @@ public class GameObject : BaseEntity, IGameObject NotAssignedException.Check(this, _transform); NotAssignedException.Check(this, _behaviourController); - NotAssignedException.Check(this, _stateEnable); NotAssignedException.Check(this, _gameManager); } public void Update() { - if (!_stateEnable.Enabled) + if (!StateEnable.Enabled) return; OnUpdated?.Invoke(this); @@ -91,7 +89,6 @@ public class GameObject : BaseEntity, IGameObject { base.UnassignInternal(); - _stateEnable = null!; _transform = null!; _behaviourController = null!; _gameManager = null!; @@ -104,8 +101,8 @@ public class GameObject : BaseEntity, IGameObject controller.BehaviourController.OnBehaviourAdded += OnBehaviourAdded; controller.BehaviourController.OnBehaviourRemoved += OnBehaviourRemoved; } - private void OnBehaviourRemoved(IBehaviourController _, IBehaviour behaviour) { if (IsInitialized) behaviour.Initialize(); } - private void OnBehaviourAdded(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(); } bool IHierarchyObject.EnterHierarchy(IGameManager gameManager) {