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:
parent
cc4068fa2e
commit
c0c48c7d51
|
@ -20,7 +20,7 @@ public interface IHierarchyObject : IEntity, INameable
|
|||
/// <summary>
|
||||
/// Gets the <see cref="IGameManager"/> associated with this <see cref="IEntity"/> , if any.
|
||||
/// </summary>
|
||||
IGameManager? GameManager { get; }
|
||||
IGameManager GameManager { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the <see cref="IEntity"/> is currently in the hierarchy.
|
||||
|
|
|
@ -6,6 +6,7 @@ public abstract class Behaviour : BehaviourBase
|
|||
{
|
||||
private bool isInitializedThisFrame = false;
|
||||
|
||||
protected IGameManager GameManager => BehaviourController.GameObject.GameManager;
|
||||
protected IGameObject GameObject => BehaviourController.GameObject;
|
||||
protected ITransform Transform => BehaviourController.GameObject.Transform;
|
||||
|
||||
|
@ -33,7 +34,7 @@ public abstract class Behaviour : BehaviourBase
|
|||
OnInitialize();
|
||||
|
||||
if (GameObject.IsInHierarchy)
|
||||
EnteredHierarchy(GameObject, GameObject.GameManager ?? throw new System.Exception("Unexpected Error"));
|
||||
EnteredHierarchy(GameObject, GameManager);
|
||||
}
|
||||
|
||||
protected virtual void OnFinalize() { }
|
||||
|
@ -48,7 +49,7 @@ public abstract class Behaviour : BehaviourBase
|
|||
OnFinalize();
|
||||
|
||||
if (GameObject.IsInHierarchy)
|
||||
ExitedHierarchy(GameObject, GameObject.GameManager ?? throw new System.Exception("Unexpected Error"));
|
||||
ExitedHierarchy(GameObject, GameManager);
|
||||
}
|
||||
|
||||
protected virtual void OnPreUpdatePreActiveCheck() { }
|
||||
|
|
|
@ -18,13 +18,13 @@ public class GameObject : BaseEntity, IGameObject
|
|||
|
||||
private ITransform _transform = null!;
|
||||
private IBehaviourController _behaviourController = 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
|
||||
|
@ -144,7 +144,7 @@ public class GameObject : BaseEntity, IGameObject
|
|||
if (!IsInHierarchy || _gameManager is not IGameManager gameManager)
|
||||
return false;
|
||||
|
||||
_gameManager = null;
|
||||
_gameManager = null!;
|
||||
OnExitedHierarchy?.Invoke(this, gameManager);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -17,9 +17,9 @@ public abstract class HierarchyObjectBase : IHierarchyObject
|
|||
private string _name = nameof(HierarchyObjectBase);
|
||||
private bool _initialized = false;
|
||||
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;
|
||||
|
||||
|
@ -134,7 +134,7 @@ public abstract class HierarchyObjectBase : IHierarchyObject
|
|||
if (!IsInHierarchy || _gameManager is not IGameManager gameManager)
|
||||
return false;
|
||||
|
||||
_gameManager = null;
|
||||
_gameManager = null!;
|
||||
OnExitingHierarchy(gameManager);
|
||||
OnExitedHierarchy?.Invoke(this, gameManager);
|
||||
return true;
|
||||
|
|
Loading…
Reference in New Issue