From 11f4e8a14ab34ce8af181720ba8642d76da85b20 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sun, 17 Nov 2024 21:57:00 +0300 Subject: [PATCH] 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 --- Engine.Core/Abstract/Assignable/IHierarchyObject.cs | 2 +- Engine.Core/Behaviour.cs | 5 +++-- Engine.Core/GameObject.cs | 6 +++--- Engine.Core/HierarchyObjectBase.cs | 4 ++-- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Engine.Core/Abstract/Assignable/IHierarchyObject.cs b/Engine.Core/Abstract/Assignable/IHierarchyObject.cs index be67532..04308a0 100644 --- a/Engine.Core/Abstract/Assignable/IHierarchyObject.cs +++ b/Engine.Core/Abstract/Assignable/IHierarchyObject.cs @@ -20,7 +20,7 @@ public interface IHierarchyObject : IEntity, INameable /// /// Gets the associated with this , if any. /// - IGameManager? GameManager { get; } + IGameManager GameManager { get; } /// /// Indicates whether the is currently in the hierarchy. diff --git a/Engine.Core/Behaviour.cs b/Engine.Core/Behaviour.cs index a16cea0..13d1117 100644 --- a/Engine.Core/Behaviour.cs +++ b/Engine.Core/Behaviour.cs @@ -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() { } diff --git a/Engine.Core/GameObject.cs b/Engine.Core/GameObject.cs index 942ed15..8f2a9af 100644 --- a/Engine.Core/GameObject.cs +++ b/Engine.Core/GameObject.cs @@ -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; } diff --git a/Engine.Core/HierarchyObjectBase.cs b/Engine.Core/HierarchyObjectBase.cs index 9400f43..df5a253 100644 --- a/Engine.Core/HierarchyObjectBase.cs +++ b/Engine.Core/HierarchyObjectBase.cs @@ -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;