From 1438b19e35752097606974c6c5a45417756f12cb Mon Sep 17 00:00:00 2001 From: Syntriax Date: Tue, 30 Jan 2024 12:26:17 +0300 Subject: [PATCH] feat: GameObjects are now Connected to a Single IGameManager --- .../Assignable/IAssignableGameManager.cs | 26 +++++++++++++++++++ Engine.Core/Abstract/IGameObject.cs | 2 +- Engine.Core/GameManager.cs | 2 ++ Engine.Core/GameObject.cs | 15 +++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 Engine.Core/Abstract/Assignable/IAssignableGameManager.cs diff --git a/Engine.Core/Abstract/Assignable/IAssignableGameManager.cs b/Engine.Core/Abstract/Assignable/IAssignableGameManager.cs new file mode 100644 index 0000000..a2e772e --- /dev/null +++ b/Engine.Core/Abstract/Assignable/IAssignableGameManager.cs @@ -0,0 +1,26 @@ +using System; + +namespace Syntriax.Engine.Core.Abstract; + +/// +/// Indicates the object is an with an assignable field. +/// +public interface IAssignableGameManager : IAssignable +{ + /// + /// Callback triggered when the value has has been assigned a new value. + /// + Action? OnGameManagerAssigned { get; set; } + + /// + IGameManager GameManager { get; } + + /// + /// Assign a value to the field of this object + /// + /// New to assign. + /// + /// , if the value given assigned successfully assigned, if not. + /// + bool Assign(IGameManager gameManager); +} diff --git a/Engine.Core/Abstract/IGameObject.cs b/Engine.Core/Abstract/IGameObject.cs index c9d6a49..d625e88 100644 --- a/Engine.Core/Abstract/IGameObject.cs +++ b/Engine.Core/Abstract/IGameObject.cs @@ -2,7 +2,7 @@ using System; namespace Syntriax.Engine.Core.Abstract; -public interface IGameObject : IEntity, IAssignableTransform, IAssignableBehaviourController, INameable, IInitialize +public interface IGameObject : IEntity, IAssignableGameManager, IAssignableTransform, IAssignableBehaviourController, INameable, IInitialize { Action? OnUpdated { get; set; } diff --git a/Engine.Core/GameManager.cs b/Engine.Core/GameManager.cs index b9dd0c4..8ea531b 100644 --- a/Engine.Core/GameManager.cs +++ b/Engine.Core/GameManager.cs @@ -143,6 +143,8 @@ public class GameManager : IGameManager private void Register(IGameObject gameObject) { + gameObject.Assign(this); + gameObject.OnFinalized += OnGameObjectFinalize; _gameObjects.Add(gameObject); diff --git a/Engine.Core/GameObject.cs b/Engine.Core/GameObject.cs index 0e50c25..0e60147 100644 --- a/Engine.Core/GameObject.cs +++ b/Engine.Core/GameObject.cs @@ -12,6 +12,7 @@ public class GameObject : IGameObject public Action? OnTransformAssigned { get; set; } = null; public Action? OnUnassigned { get; set; } = null; public Action? OnBehaviourControllerAssigned { get; set; } = null; + public Action? OnGameManagerAssigned { get; set; } = null; public Action? OnNameChanged { get; set; } = null; @@ -24,6 +25,7 @@ public class GameObject : IGameObject private ITransform _transform = null!; private IBehaviourController _behaviourController = null!; private IStateEnable _stateEnable = null!; + private IGameManager _gameManager = null!; private string _name = nameof(GameObject); private bool _initialized = false; @@ -31,6 +33,7 @@ public class GameObject : IGameObject public ITransform Transform => _transform; public IBehaviourController BehaviourController => _behaviourController; public IStateEnable StateEnable => _stateEnable; + public IGameManager GameManager => _gameManager; public bool Initialized { @@ -68,6 +71,7 @@ public class GameObject : IGameObject NotAssignedException.Check(this, _transform); NotAssignedException.Check(this, _behaviourController); NotAssignedException.Check(this, _stateEnable); + NotAssignedException.Check(this, _gameManager); Initialized = true; return true; @@ -126,6 +130,16 @@ public class GameObject : IGameObject return true; } + public bool Assign(IGameManager gameManager) + { + if (Initialized) + return false; + + _gameManager = gameManager; + OnGameManagerAssigned?.Invoke(this); + return true; + } + public bool Unassign() { if (Initialized) @@ -134,6 +148,7 @@ public class GameObject : IGameObject _stateEnable = null!; _transform = null!; _behaviourController = null!; + _gameManager = null!; OnUnassigned?.Invoke(this); return true;