fix: Issues With Behaviours Entering / Leaving the Hierarchy Callbacks Not Firing

This commit is contained in:
Syntriax 2024-11-10 20:21:53 +03:00
parent 81625abd25
commit 1545291942
4 changed files with 60 additions and 9 deletions

View File

@ -6,7 +6,7 @@ namespace Syntriax.Engine.Core.Abstract;
/// <summary>
/// Represents a controller for managing <see cref="IBehaviour"/>s and notify them accordingly about the engine's updates. Connected to an <see cref="IGameObject"/>.
/// </summary>
public interface IBehaviourController : IAssignableGameObject, IEnumerable<IBehaviour>
public interface IBehaviourController : IInitialize, IAssignableGameObject, IEnumerable<IBehaviour>
{
/// <summary>
/// Event triggered before the update of <see cref="IBehaviour"/>s.

View File

@ -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<IBehaviour> behaviours = new List<IBehaviour>(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>(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!;

View File

@ -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);

View File

@ -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<IBehaviour>())
behaviour.Finalize();
if (!_behaviourController.Finalize())
throw new System.Exception($"Failed to Finalize {BehaviourController.GetType().Name} on {Transform.GameObject.Name}");
}
public bool Assign(ITransform transform)