diff --git a/Engine.Core/Abstract/IBehaviourController.cs b/Engine.Core/Abstract/IBehaviourController.cs index 9c4bc1f..c5ba6da 100644 --- a/Engine.Core/Abstract/IBehaviourController.cs +++ b/Engine.Core/Abstract/IBehaviourController.cs @@ -6,7 +6,7 @@ namespace Syntriax.Engine.Core.Abstract; /// /// Represents a controller for managing s and notify them accordingly about the engine's updates. Connected to an . /// -public interface IBehaviourController : IAssignableGameObject, IEnumerable +public interface IBehaviourController : IInitialize, IAssignableGameObject, IEnumerable { /// /// Event triggered before the update of s. diff --git a/Engine.Core/BehaviourController.cs b/Engine.Core/BehaviourController.cs index 0f36423..44e1969 100644 --- a/Engine.Core/BehaviourController.cs +++ b/Engine.Core/BehaviourController.cs @@ -5,6 +5,7 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using Syntriax.Engine.Core.Abstract; +using Syntriax.Engine.Core.Exceptions; namespace Syntriax.Engine.Core; @@ -17,17 +18,37 @@ public class BehaviourController : IBehaviourController public event IBehaviourController.OnBehaviourAddedDelegate? OnBehaviourAdded = null; public event IBehaviourController.OnBehaviourRemovedDelegate? OnBehaviourRemoved = null; - public event IAssignable.OnUnassignedDelegate? OnUnassigned; public event IAssignableGameObject.OnGameObjectAssignedDelegate? OnGameObjectAssigned = null; + public event IInitialize.OnInitializedDelegate? OnInitialized = null; + public event IInitialize.OnFinalizedDelegate? OnFinalized = null; + + public event IAssignable.OnUnassignedDelegate? OnUnassigned = null; private readonly IList behaviours = new List(Constants.BEHAVIOURS_SIZE_INITIAL); private IGameObject _gameObject = null!; - + private bool _initialized = false; public IGameObject GameObject => _gameObject; + + public bool IsInitialized + { + get => _initialized; + private set + { + if (value == _initialized) + return; + + _initialized = value; + if (value) + OnInitialized?.Invoke(this); + else + OnFinalized?.Invoke(this); + } + } + public T AddBehaviour(T behaviour) where T : class, IBehaviour { InsertBehaviourByPriority(behaviour); @@ -121,9 +142,36 @@ public class BehaviourController : IBehaviourController return true; } + + public bool Initialize() + { + if (IsInitialized) + return false; + + NotAssignedException.Check(this, _gameObject); + + foreach (IBehaviour behaviour in behaviours) + behaviour.Initialize(); + + IsInitialized = true; + return true; + } + + public bool Finalize() + { + if (!IsInitialized) + return false; + + foreach (IBehaviour behaviour in behaviours) + behaviour.Finalize(); + + IsInitialized = false; + return true; + } + public bool Unassign() { - if (GameObject is not null && GameObject.IsInitialized) + if (IsInitialized) return false; _gameObject = null!; diff --git a/Engine.Core/GameManager.cs b/Engine.Core/GameManager.cs index 8fe4591..56f2e10 100644 --- a/Engine.Core/GameManager.cs +++ b/Engine.Core/GameManager.cs @@ -140,12 +140,12 @@ public class GameManager : BaseEntity, IGameManager gameObject.OnFinalized += OnGameObjectFinalize; gameObject.OnExitedHierarchy += OnGameObjectExitedHierarchy; - foreach (ITransform child in gameObject.Transform.Children) - Register(child.GameObject); - if (!gameObject.Initialize()) throw new Exception($"{nameof(gameObject)} can't be initialized"); + foreach (ITransform child in gameObject.Transform.Children) + Register(child.GameObject); + _gameObjects.Add(gameObject); _hierarchyObjects.Add(gameObject); diff --git a/Engine.Core/GameObject.cs b/Engine.Core/GameObject.cs index c952724..942ed15 100644 --- a/Engine.Core/GameObject.cs +++ b/Engine.Core/GameObject.cs @@ -46,6 +46,9 @@ public class GameObject : BaseEntity, IGameObject NotAssignedException.Check(this, _transform); NotAssignedException.Check(this, _behaviourController); + + if (!_behaviourController.Initialize()) + throw new System.Exception($"Failed to Initialize {BehaviourController.GetType().Name} on {Transform.GameObject.Name}"); } public void Update() @@ -60,8 +63,8 @@ public class GameObject : BaseEntity, IGameObject { base.FinalizeInternal(); - foreach (IBehaviour behaviour in _behaviourController.GetBehaviours()) - behaviour.Finalize(); + if (!_behaviourController.Finalize()) + throw new System.Exception($"Failed to Finalize {BehaviourController.GetType().Name} on {Transform.GameObject.Name}"); } public bool Assign(ITransform transform)