refactor: IHierarchy Integration with Overall Code Base
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user