refactor: IHierarchyObject Nullable IGameManager to Non-Nullable

Since in a typical use case of these classes they are already in the
hierarchy and it bloats the behaviour code with lots of null checks
This commit is contained in:
Syntriax 2024-11-17 21:57:00 +03:00
parent cc4068fa2e
commit 11f4e8a14a
4 changed files with 9 additions and 8 deletions

View File

@ -20,7 +20,7 @@ public interface IHierarchyObject : IEntity, INameable
/// <summary> /// <summary>
/// Gets the <see cref="IGameManager"/> associated with this <see cref="IEntity"/> , if any. /// Gets the <see cref="IGameManager"/> associated with this <see cref="IEntity"/> , if any.
/// </summary> /// </summary>
IGameManager? GameManager { get; } IGameManager GameManager { get; }
/// <summary> /// <summary>
/// Indicates whether the <see cref="IEntity"/> is currently in the hierarchy. /// Indicates whether the <see cref="IEntity"/> is currently in the hierarchy.

View File

@ -6,6 +6,7 @@ public abstract class Behaviour : BehaviourBase
{ {
private bool isInitializedThisFrame = false; private bool isInitializedThisFrame = false;
protected IGameManager GameManager => BehaviourController.GameObject.GameManager;
protected IGameObject GameObject => BehaviourController.GameObject; protected IGameObject GameObject => BehaviourController.GameObject;
protected ITransform Transform => BehaviourController.GameObject.Transform; protected ITransform Transform => BehaviourController.GameObject.Transform;
@ -33,7 +34,7 @@ public abstract class Behaviour : BehaviourBase
OnInitialize(); OnInitialize();
if (GameObject.IsInHierarchy) if (GameObject.IsInHierarchy)
EnteredHierarchy(GameObject, GameObject.GameManager ?? throw new System.Exception("Unexpected Error")); EnteredHierarchy(GameObject, GameManager);
} }
protected virtual void OnFinalize() { } protected virtual void OnFinalize() { }
@ -48,7 +49,7 @@ public abstract class Behaviour : BehaviourBase
OnFinalize(); OnFinalize();
if (GameObject.IsInHierarchy) if (GameObject.IsInHierarchy)
ExitedHierarchy(GameObject, GameObject.GameManager ?? throw new System.Exception("Unexpected Error")); ExitedHierarchy(GameObject, GameManager);
} }
protected virtual void OnPreUpdatePreActiveCheck() { } protected virtual void OnPreUpdatePreActiveCheck() { }

View File

@ -18,13 +18,13 @@ public class GameObject : BaseEntity, IGameObject
private ITransform _transform = null!; private ITransform _transform = null!;
private IBehaviourController _behaviourController = null!; private IBehaviourController _behaviourController = null!;
private IGameManager? _gameManager = null; private IGameManager _gameManager = null!;
private string _name = nameof(GameObject); private string _name = nameof(GameObject);
public ITransform Transform => _transform; public ITransform Transform => _transform;
public IBehaviourController BehaviourController => _behaviourController; public IBehaviourController BehaviourController => _behaviourController;
public IGameManager? GameManager => _gameManager; public IGameManager GameManager => _gameManager;
public bool IsInHierarchy => GameManager is not null; public bool IsInHierarchy => GameManager is not null;
public string Name public string Name
@ -144,7 +144,7 @@ public class GameObject : BaseEntity, IGameObject
if (!IsInHierarchy || _gameManager is not IGameManager gameManager) if (!IsInHierarchy || _gameManager is not IGameManager gameManager)
return false; return false;
_gameManager = null; _gameManager = null!;
OnExitedHierarchy?.Invoke(this, gameManager); OnExitedHierarchy?.Invoke(this, gameManager);
return true; return true;
} }

View File

@ -17,9 +17,9 @@ public abstract class HierarchyObjectBase : IHierarchyObject
private string _name = nameof(HierarchyObjectBase); private string _name = nameof(HierarchyObjectBase);
private bool _initialized = false; private bool _initialized = false;
private IStateEnable _stateEnable = null!; private IStateEnable _stateEnable = null!;
private IGameManager? _gameManager = null; private IGameManager _gameManager = null!;
public IGameManager? GameManager => _gameManager; public IGameManager GameManager => _gameManager;
public bool IsInHierarchy => _gameManager is not null; public bool IsInHierarchy => _gameManager is not null;