BREAKING CHANGE: Added IHierarchObject with Hierarchy Enter & Exit

This commit is contained in:
Syntriax 2024-10-22 22:02:38 +03:00
parent cf8a5de580
commit aa7db94957
6 changed files with 159 additions and 20 deletions

View File

@ -0,0 +1,62 @@
namespace Syntriax.Engine.Core.Abstract;
/// <summary>
/// Represents an <see cref="IEntity"/> that can enter and exit a hierarchy within the <see cref="IGameManager"/> system.
/// This interface allows for tracking the object's presence in the hierarchy and provides events
/// for notifying when the see enters or exits the hierarchy.
/// </summary>
public interface IHierarchyObject : IEntity
{
/// <summary>
/// Event triggered when the <see cref="IEntity"/> enters the hierarchy.
/// </summary>
event OnEnteredHierarchyDelegate? OnEnteredHierarchy;
/// <summary>
/// Event triggered when the <see cref="IEntity"/> exits the hierarchy.
/// </summary>
event OnExitedHierarchyDelegate? OnExitedHierarchy;
/// <summary>
/// Gets the <see cref="IGameManager"/> associated with this <see cref="IEntity"/> , if any.
/// </summary>
IGameManager? GameManager { get; }
/// <summary>
/// Indicates whether the <see cref="IEntity"/> is currently in the hierarchy.
/// </summary>
bool IsInHierarchy { get; }
/// <summary>
/// Internal method to handle entering the hierarchy.
/// This should be called by the system to properly manage hierarchy states.
/// </summary>
/// <param name="gameManager">The game manager that is managing this hierarchy.</param>
/// <returns>
/// <see cref="true"/> if the <see cref="IEntity"/> successfully entered the hierarchy;
/// <see cref="false"/> if it failed to do so.
/// </returns>
internal bool EnterHierarchy(IGameManager gameManager);
/// <summary>
/// Internal method to handle exiting the hierarchy.
/// This should be called by the system to properly manage hierarchy states.
/// </summary>
/// <returns>
/// <see cref="true"/> if the <see cref="IEntity"/> successfully exited the hierarchy;
/// <see cref="false"/> if it failed to do so.
/// </returns>
internal bool ExitHierarchy();
/// <summary>
/// Delegate type for the event triggered when the <see cref="IEntity"/> enters the hierarchy.
/// </summary>
/// <param name="sender">The <see cref="IEntity"/> that entered the hierarchy.</param>
delegate void OnEnteredHierarchyDelegate(IHierarchyObject sender);
/// <summary>
/// Delegate type for the event triggered when the <see cref="IEntity"/> exits the hierarchy.
/// </summary>
/// <param name="sender">The <see cref="IEntity"/> that exited the hierarchy.</param>
delegate void OnExitedHierarchyDelegate(IHierarchyObject sender);
}

View File

@ -5,7 +5,7 @@ namespace Syntriax.Engine.Core.Abstract;
/// <summary>
/// Represents a game world responsible for managing <see cref="IGameObject"/>s.
/// </summary>
public interface IGameManager : IEntity, IEnumerable<IGameObject>
public interface IGameManager : IEntity, IEnumerable<IGameObject>, IEnumerable<IHierarchyObject>
{
/// <summary>
/// Event triggered when a <see cref="IGameObject"/> is registered to the <see cref="IGameManager"/>.
@ -17,6 +17,16 @@ public interface IGameManager : IEntity, IEnumerable<IGameObject>
/// </summary>
event OnGameObjectUnRegisteredDelegate? OnGameObjectUnRegistered;
/// <summary>
/// Event triggered when a <see cref="IHierarchyObject"/> is registered to the <see cref="IGameManager"/>.
/// </summary>
event OnHierarchyObjectRegisteredDelegate? OnHierarchyObjectRegistered;
/// <summary>
/// Event triggered when a <see cref="IHierarchyObject"/> is unregistered from the <see cref="IGameManager"/>.
/// </summary>
event OnHierarchyObjectUnRegisteredDelegate? OnHierarchyObjectUnRegistered;
/// <summary>
/// Gets a read-only list of <see cref="IGameObject"/>s managed by the <see cref="IGameManager"/>.
/// </summary>
@ -56,4 +66,6 @@ public interface IGameManager : IEntity, IEnumerable<IGameObject>
delegate void OnGameObjectRegisteredDelegate(IGameManager sender, IGameObject gameObjectRegistered);
delegate void OnGameObjectUnRegisteredDelegate(IGameManager sender, IGameObject gameObjectUnregistered);
delegate void OnHierarchyObjectRegisteredDelegate(IGameManager sender, IHierarchyObject hierarchyObjectRegistered);
delegate void OnHierarchyObjectUnRegisteredDelegate(IGameManager sender, IHierarchyObject hierarchyObjectUnregistered);
}

View File

@ -3,7 +3,7 @@ namespace Syntriax.Engine.Core.Abstract;
/// <summary>
/// Represents a game object with various properties and functionalities.
/// </summary>
public interface IGameObject : IEntity, IAssignableGameManager, IAssignableTransform, IAssignableBehaviourController, INameable, IInitialize
public interface IGameObject : IEntity, IHierarchyObject, IAssignableTransform, IAssignableBehaviourController, INameable, IInitialize
{
/// <summary>
/// Event triggered when the <see cref="Update"/> method is called.

View File

@ -27,6 +27,8 @@ public abstract class Behaviour : BehaviourBase
BehaviourController.OnPreUpdate += PreUpdate;
BehaviourController.OnPreDraw += PreDraw;
BehaviourController.OnUpdate += Update;
BehaviourController.GameObject.OnEnteredHierarchy += EnteredHierarchy;
BehaviourController.GameObject.OnExitedHierarchy += ExitedHierarchy;
OnInitialize();
}
@ -36,6 +38,8 @@ public abstract class Behaviour : BehaviourBase
BehaviourController.OnPreUpdate -= PreUpdate;
BehaviourController.OnPreDraw -= PreDraw;
BehaviourController.OnUpdate -= Update;
BehaviourController.GameObject.OnEnteredHierarchy -= EnteredHierarchy;
BehaviourController.GameObject.OnExitedHierarchy -= ExitedHierarchy;
OnFinalize();
}
@ -84,4 +88,10 @@ public abstract class Behaviour : BehaviourBase
OnPreDraw();
}
protected virtual void OnEnteredHierarchy() { }
private void EnteredHierarchy(IHierarchyObject sender) => OnEnteredHierarchy();
protected virtual void OnExitedHierarchy() { }
private void ExitedHierarchy(IHierarchyObject sender) => OnExitedHierarchy();
}

View File

@ -13,9 +13,11 @@ public class GameManager : BaseEntity, IGameManager
{
public event IGameManager.OnGameObjectRegisteredDelegate? OnGameObjectRegistered = null;
public event IGameManager.OnGameObjectUnRegisteredDelegate? OnGameObjectUnRegistered = null;
public event IGameManager.OnHierarchyObjectRegisteredDelegate? OnHierarchyObjectRegistered = null;
public event IGameManager.OnHierarchyObjectUnRegisteredDelegate? OnHierarchyObjectUnRegistered = null;
private readonly List<IGameObject> _gameObjects = new(Constants.GAME_OBJECTS_SIZE_INITIAL);
private readonly List<IHierarchyObject> _hierarchyObjects = new(Constants.GAME_OBJECTS_SIZE_INITIAL);
private GameObjectFactory _gameObjectFactory = null!;
@ -101,21 +103,55 @@ public class GameManager : BaseEntity, IGameManager
/////////////////////////////////////////////////////////////////
private void Register(IHierarchyObject hierarchyObject)
{
if (hierarchyObject is IGameObject gameObject)
Register(gameObject);
else
{
_hierarchyObjects.Add(hierarchyObject);
OnHierarchyObjectRegistered?.Invoke(this, hierarchyObject);
}
}
private void Register(IGameObject gameObject)
{
gameObject.Assign(this);
gameObject.OnFinalized += OnGameObjectFinalize;
gameObject.OnExitedHierarchy += OnGameObjectExitedHierarchy;
_gameObjects.Add(gameObject);
_hierarchyObjects.Add(gameObject);
if (!gameObject.EnterHierarchy(this))
throw new Exception($"{nameof(gameObject)} can't enter the hierarchy");
OnHierarchyObjectRegistered?.Invoke(this, gameObject);
OnGameObjectRegistered?.Invoke(this, gameObject);
}
private void Unregister(IHierarchyObject hierarchyObject)
{
if (hierarchyObject is IGameObject gameObject)
Unregister(gameObject);
else
{
_hierarchyObjects.Remove(hierarchyObject);
OnHierarchyObjectUnRegistered?.Invoke(this, hierarchyObject);
}
}
private void Unregister(IGameObject gameObject)
{
gameObject.OnFinalized -= OnGameObjectFinalize;
gameObject.OnExitedHierarchy -= OnGameObjectExitedHierarchy;
_gameObjects.Remove(gameObject);
_hierarchyObjects.Remove(gameObject);
if (!gameObject.ExitHierarchy())
throw new Exception($"{nameof(gameObject)} can't exit the hierarchy");
OnHierarchyObjectUnRegistered?.Invoke(this, gameObject);
OnGameObjectUnRegistered?.Invoke(this, gameObject);
}
@ -125,8 +161,15 @@ public class GameManager : BaseEntity, IGameManager
Unregister(gameObject);
}
private void OnGameObjectExitedHierarchy(IHierarchyObject sender)
{
if (sender is IGameObject gameObject)
Unregister(gameObject);
}
/////////////////////////////////////////////////////////////////
public IEnumerator<IGameObject> GetEnumerator() => _gameObjects.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _gameObjects.GetEnumerator();
IEnumerator<IGameObject> IEnumerable<IGameObject>.GetEnumerator() => _gameObjects.GetEnumerator();
IEnumerator<IHierarchyObject> IEnumerable<IHierarchyObject>.GetEnumerator() => _hierarchyObjects.GetEnumerator();
}

View File

@ -6,25 +6,27 @@ namespace Syntriax.Engine.Core;
[System.Diagnostics.DebuggerDisplay("Name: {Name}, Initialized: {Initialized}")]
public class GameObject : BaseEntity, IGameObject
{
public event IHierarchyObject.OnEnteredHierarchyDelegate? OnEnteredHierarchy = null;
public event IHierarchyObject.OnExitedHierarchyDelegate? OnExitedHierarchy = null;
public event IAssignableTransform.OnTransformAssignedDelegate? OnTransformAssigned = null;
public event IAssignableGameManager.OnGameManagerAssignedDelegate? OnGameManagerAssigned = null;
public event IAssignableBehaviourController.OnBehaviourControllerAssignedDelegate? OnBehaviourControllerAssigned = null;
public event INameable.OnNameChangedDelegate? OnNameChanged = null;
public event IGameObject.OnUpdatedDelegate? OnUpdated = null;
private ITransform _transform = null!;
private IBehaviourController _behaviourController = null!;
private IStateEnable _stateEnable = null!;
private IGameManager _gameManager = null!;
private IGameManager? _gameManager = null;
private string _name = nameof(GameObject);
public ITransform Transform => _transform;
public IBehaviourController BehaviourController => _behaviourController;
public IGameManager GameManager => _gameManager;
public IGameManager? GameManager => _gameManager;
public bool IsInHierarchy => GameManager is not null;
public string Name
{
@ -85,16 +87,6 @@ public class GameObject : BaseEntity, IGameObject
return true;
}
public bool Assign(IGameManager gameManager)
{
if (IsInitialized)
return false;
_gameManager = gameManager;
OnGameManagerAssigned?.Invoke(this);
return true;
}
protected override void UnassignInternal()
{
base.UnassignInternal();
@ -114,4 +106,24 @@ public class GameObject : BaseEntity, IGameObject
}
private void OnBehaviourRemoved(IBehaviourController _, IBehaviour behaviour) { if (IsInitialized) behaviour.Initialize(); }
private void OnBehaviourAdded(IBehaviourController _, IBehaviour behaviour) { if (IsInitialized) behaviour.Finalize(); }
public bool EnterHierarchy(IGameManager gameManager)
{
if (IsInHierarchy)
return false;
_gameManager = gameManager;
OnEnteredHierarchy?.Invoke(this);
return true;
}
public bool ExitHierarchy()
{
if (!IsInHierarchy)
return false;
_gameManager = null;
OnExitedHierarchy?.Invoke(this);
return true;
}
}