fix: Issues With Behaviours Entering / Leaving the Hierarchy Callbacks Not Firing
This commit is contained in:
parent
81625abd25
commit
1545291942
|
@ -6,7 +6,7 @@ namespace Syntriax.Engine.Core.Abstract;
|
||||||
/// <summary>
|
/// <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"/>.
|
/// 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>
|
/// </summary>
|
||||||
public interface IBehaviourController : IAssignableGameObject, IEnumerable<IBehaviour>
|
public interface IBehaviourController : IInitialize, IAssignableGameObject, IEnumerable<IBehaviour>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Event triggered before the update of <see cref="IBehaviour"/>s.
|
/// Event triggered before the update of <see cref="IBehaviour"/>s.
|
||||||
|
|
|
@ -5,6 +5,7 @@ using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
using Syntriax.Engine.Core.Abstract;
|
using Syntriax.Engine.Core.Abstract;
|
||||||
|
using Syntriax.Engine.Core.Exceptions;
|
||||||
|
|
||||||
namespace Syntriax.Engine.Core;
|
namespace Syntriax.Engine.Core;
|
||||||
|
|
||||||
|
@ -17,17 +18,37 @@ public class BehaviourController : IBehaviourController
|
||||||
|
|
||||||
public event IBehaviourController.OnBehaviourAddedDelegate? OnBehaviourAdded = null;
|
public event IBehaviourController.OnBehaviourAddedDelegate? OnBehaviourAdded = null;
|
||||||
public event IBehaviourController.OnBehaviourRemovedDelegate? OnBehaviourRemoved = null;
|
public event IBehaviourController.OnBehaviourRemovedDelegate? OnBehaviourRemoved = null;
|
||||||
public event IAssignable.OnUnassignedDelegate? OnUnassigned;
|
|
||||||
public event IAssignableGameObject.OnGameObjectAssignedDelegate? OnGameObjectAssigned = null;
|
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 readonly IList<IBehaviour> behaviours = new List<IBehaviour>(Constants.BEHAVIOURS_SIZE_INITIAL);
|
||||||
|
|
||||||
private IGameObject _gameObject = null!;
|
private IGameObject _gameObject = null!;
|
||||||
|
private bool _initialized = false;
|
||||||
|
|
||||||
public IGameObject GameObject => _gameObject;
|
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
|
public T AddBehaviour<T>(T behaviour) where T : class, IBehaviour
|
||||||
{
|
{
|
||||||
InsertBehaviourByPriority(behaviour);
|
InsertBehaviourByPriority(behaviour);
|
||||||
|
@ -121,9 +142,36 @@ public class BehaviourController : IBehaviourController
|
||||||
return true;
|
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()
|
public bool Unassign()
|
||||||
{
|
{
|
||||||
if (GameObject is not null && GameObject.IsInitialized)
|
if (IsInitialized)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
_gameObject = null!;
|
_gameObject = null!;
|
||||||
|
|
|
@ -140,12 +140,12 @@ public class GameManager : BaseEntity, IGameManager
|
||||||
gameObject.OnFinalized += OnGameObjectFinalize;
|
gameObject.OnFinalized += OnGameObjectFinalize;
|
||||||
gameObject.OnExitedHierarchy += OnGameObjectExitedHierarchy;
|
gameObject.OnExitedHierarchy += OnGameObjectExitedHierarchy;
|
||||||
|
|
||||||
foreach (ITransform child in gameObject.Transform.Children)
|
|
||||||
Register(child.GameObject);
|
|
||||||
|
|
||||||
if (!gameObject.Initialize())
|
if (!gameObject.Initialize())
|
||||||
throw new Exception($"{nameof(gameObject)} can't be initialized");
|
throw new Exception($"{nameof(gameObject)} can't be initialized");
|
||||||
|
|
||||||
|
foreach (ITransform child in gameObject.Transform.Children)
|
||||||
|
Register(child.GameObject);
|
||||||
|
|
||||||
_gameObjects.Add(gameObject);
|
_gameObjects.Add(gameObject);
|
||||||
_hierarchyObjects.Add(gameObject);
|
_hierarchyObjects.Add(gameObject);
|
||||||
|
|
||||||
|
|
|
@ -46,6 +46,9 @@ public class GameObject : BaseEntity, IGameObject
|
||||||
|
|
||||||
NotAssignedException.Check(this, _transform);
|
NotAssignedException.Check(this, _transform);
|
||||||
NotAssignedException.Check(this, _behaviourController);
|
NotAssignedException.Check(this, _behaviourController);
|
||||||
|
|
||||||
|
if (!_behaviourController.Initialize())
|
||||||
|
throw new System.Exception($"Failed to Initialize {BehaviourController.GetType().Name} on {Transform.GameObject.Name}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update()
|
public void Update()
|
||||||
|
@ -60,8 +63,8 @@ public class GameObject : BaseEntity, IGameObject
|
||||||
{
|
{
|
||||||
base.FinalizeInternal();
|
base.FinalizeInternal();
|
||||||
|
|
||||||
foreach (IBehaviour behaviour in _behaviourController.GetBehaviours<IBehaviour>())
|
if (!_behaviourController.Finalize())
|
||||||
behaviour.Finalize();
|
throw new System.Exception($"Failed to Finalize {BehaviourController.GetType().Name} on {Transform.GameObject.Name}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Assign(ITransform transform)
|
public bool Assign(ITransform transform)
|
||||||
|
|
Loading…
Reference in New Issue