refactor: IHierarchy Integration with Overall Code Base
This commit is contained in:
parent
62e50aefc1
commit
43f1749b04
@ -5,7 +5,7 @@ namespace Syntriax.Engine.Core.Abstract;
|
||||
/// 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
|
||||
public interface IHierarchyObject : IEntity, INameable
|
||||
{
|
||||
/// <summary>
|
||||
/// Event triggered when the <see cref="IEntity"/> enters the hierarchy.
|
||||
|
@ -48,10 +48,10 @@ public interface IGameManager : IEntity, IEnumerable<IGameObject>
|
||||
IReadOnlyList<IHierarchyObject> HierarchyObjects { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Registers a <see cref="IGameObject"/> to the <see cref="IGameManager"/>.
|
||||
/// Registers an <see cref="IHierarchyObject"/> to the <see cref="IGameManager"/>.
|
||||
/// </summary>
|
||||
/// <param name="gameObject">The <see cref="IGameObject"/> to register.</param>
|
||||
void RegisterGameObject(IGameObject gameObject);
|
||||
/// <param name="hierarchyObject">The <see cref="IHierarchyObject"/> to register.</param>
|
||||
void Register(IHierarchyObject hierarchyObject);
|
||||
|
||||
/// <summary>
|
||||
/// Instantiates a <see cref="IGameObject"/> of type T with the given arguments and registers it to the <see cref="IGameManager"/>.
|
||||
@ -62,11 +62,10 @@ public interface IGameManager : IEntity, IEnumerable<IGameObject>
|
||||
T InstantiateGameObject<T>(params object?[]? args) where T : class, IGameObject;
|
||||
|
||||
/// <summary>
|
||||
/// Removes a <see cref="IGameObject"/> from the <see cref="IGameManager"/>.
|
||||
/// Removes an <see cref="IHierarchyObject"/> from the <see cref="IGameManager"/>.
|
||||
/// </summary>
|
||||
/// <param name="gameObject">The <see cref="IGameObject"/> to remove.</param>
|
||||
/// <returns>The removed <see cref="IGameObject"/>.</returns>
|
||||
IGameObject RemoveGameObject(IGameObject gameObject);
|
||||
/// <param name="hierarchyObject">The <see cref="IHierarchyObject"/> to remove.</param>
|
||||
void Remove(IHierarchyObject hierarchyObject);
|
||||
|
||||
/// <summary>
|
||||
/// Updates the <see cref="IGameManager"/> with the given engine time data.
|
||||
|
32
Engine.Core/Extensions/HierarchyObjectExtensions.cs
Normal file
32
Engine.Core/Extensions/HierarchyObjectExtensions.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
|
||||
namespace Syntriax.Engine.Core;
|
||||
|
||||
public static class HierarchyObjectExtensions
|
||||
{
|
||||
public static T? FindObject<T>(this IEnumerable<IHierarchyObject> hierarchyObjects) where T : class
|
||||
{
|
||||
foreach (IHierarchyObject hierarchyObject in hierarchyObjects)
|
||||
if (hierarchyObject is T @object)
|
||||
return @object;
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public static bool TryFindObject<T>(this IEnumerable<IHierarchyObject> hierarchyObjects, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
|
||||
{
|
||||
behaviour = FindObject<T>(hierarchyObjects);
|
||||
return behaviour is not null;
|
||||
}
|
||||
|
||||
public static void FindObjects<T>(this IEnumerable<IHierarchyObject> hierarchyObjects, List<T> behaviours) where T : class
|
||||
{
|
||||
behaviours.Clear();
|
||||
foreach (IHierarchyObject hierarchyObject in hierarchyObjects)
|
||||
if (hierarchyObject is T @object)
|
||||
behaviours.Add(@object);
|
||||
}
|
||||
}
|
@ -52,12 +52,19 @@ public class GameManager : BaseEntity, IGameManager
|
||||
}
|
||||
}
|
||||
|
||||
public void RegisterGameObject(IGameObject gameObject)
|
||||
public void Register(IHierarchyObject hierarchyObject)
|
||||
{
|
||||
if (_gameObjects.Contains(gameObject))
|
||||
throw new Exception($"{nameof(IGameObject)} named {gameObject.Name} is already registered to the {nameof(GameManager)}.");
|
||||
if (_hierarchyObjects.Contains(hierarchyObject))
|
||||
throw new Exception($"{nameof(IHierarchyObject)} named {hierarchyObject.Name} is already registered to the {nameof(GameManager)}.");
|
||||
|
||||
Register(gameObject);
|
||||
if (hierarchyObject is IGameObject gameObject)
|
||||
Register(gameObject);
|
||||
else
|
||||
{
|
||||
_hierarchyObjects.Add(hierarchyObject);
|
||||
hierarchyObject.EnterHierarchy(this);
|
||||
OnHierarchyObjectRegistered?.Invoke(this, hierarchyObject);
|
||||
}
|
||||
}
|
||||
|
||||
public T InstantiateGameObject<T>(params object?[]? args) where T : class, IGameObject
|
||||
@ -67,13 +74,19 @@ public class GameManager : BaseEntity, IGameManager
|
||||
return gameObject;
|
||||
}
|
||||
|
||||
public IGameObject RemoveGameObject(IGameObject gameObject)
|
||||
public void Remove(IHierarchyObject hierarchyObject)
|
||||
{
|
||||
if (!_gameObjects.Contains(gameObject))
|
||||
throw new Exception($"{nameof(IGameObject)} named {gameObject.Name} is not registered to the {nameof(GameManager)}.");
|
||||
if (!_hierarchyObjects.Contains(hierarchyObject))
|
||||
throw new Exception($"{nameof(IHierarchyObject)} named {hierarchyObject.Name} is not registered to the {nameof(GameManager)}.");
|
||||
|
||||
Unregister(gameObject);
|
||||
return gameObject;
|
||||
if (hierarchyObject is IGameObject gameObject)
|
||||
Unregister(gameObject);
|
||||
else
|
||||
{
|
||||
_hierarchyObjects.Remove(hierarchyObject);
|
||||
hierarchyObject.ExitHierarchy();
|
||||
OnHierarchyObjectUnRegistered?.Invoke(this, hierarchyObject);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void InitializeInternal()
|
||||
@ -111,19 +124,11 @@ 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)
|
||||
{
|
||||
if (_gameObjects.Contains(gameObject))
|
||||
throw new Exception($"{nameof(IGameObject)} named {gameObject.Name} is already registered to the {nameof(GameManager)}.");
|
||||
|
||||
gameObject.OnFinalized += OnGameObjectFinalize;
|
||||
gameObject.OnExitedHierarchy += OnGameObjectExitedHierarchy;
|
||||
|
||||
@ -137,19 +142,11 @@ public class GameManager : BaseEntity, IGameManager
|
||||
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)
|
||||
{
|
||||
if (!_gameObjects.Contains(gameObject))
|
||||
throw new Exception($"{nameof(IGameObject)} named {gameObject.Name} is not registered to the {nameof(GameManager)}.");
|
||||
|
||||
gameObject.OnFinalized -= OnGameObjectFinalize;
|
||||
gameObject.OnExitedHierarchy -= OnGameObjectExitedHierarchy;
|
||||
|
||||
|
@ -107,7 +107,7 @@ 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)
|
||||
bool IHierarchyObject.EnterHierarchy(IGameManager gameManager)
|
||||
{
|
||||
if (IsInHierarchy)
|
||||
return false;
|
||||
@ -117,7 +117,7 @@ public class GameObject : BaseEntity, IGameObject
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool ExitHierarchy()
|
||||
bool IHierarchyObject.ExitHierarchy()
|
||||
{
|
||||
if (!IsInHierarchy)
|
||||
return false;
|
||||
|
Loading…
x
Reference in New Issue
Block a user