feat: GameObjects are now Connected to a Single IGameManager

This commit is contained in:
Syntriax 2024-01-30 12:26:17 +03:00
parent 4000e761a7
commit 1438b19e35
4 changed files with 44 additions and 1 deletions

View File

@ -0,0 +1,26 @@
using System;
namespace Syntriax.Engine.Core.Abstract;
/// <summary>
/// Indicates the object is an <see cref="IAssignable"/> with an assignable <see cref="ITransform"/> field.
/// </summary>
public interface IAssignableGameManager : IAssignable
{
/// <summary>
/// Callback triggered when the <see cref="IGameManager"/> value has has been assigned a new value.
/// </summary>
Action<IAssignableGameManager>? OnGameManagerAssigned { get; set; }
/// <inheritdoc cref="IGameManager" />
IGameManager GameManager { get; }
/// <summary>
/// Assign a value to the <see cref="IGameManager"/> field of this object
/// </summary>
/// <param name="gameManager">New <see cref="IGameManager"/> to assign.</param>
/// <returns>
/// <see cref="true"/>, if the value given assigned successfully assigned, <see cref="false"/> if not.
/// </returns>
bool Assign(IGameManager gameManager);
}

View File

@ -2,7 +2,7 @@ using System;
namespace Syntriax.Engine.Core.Abstract; namespace Syntriax.Engine.Core.Abstract;
public interface IGameObject : IEntity, IAssignableTransform, IAssignableBehaviourController, INameable, IInitialize public interface IGameObject : IEntity, IAssignableGameManager, IAssignableTransform, IAssignableBehaviourController, INameable, IInitialize
{ {
Action<IGameObject>? OnUpdated { get; set; } Action<IGameObject>? OnUpdated { get; set; }

View File

@ -143,6 +143,8 @@ public class GameManager : IGameManager
private void Register(IGameObject gameObject) private void Register(IGameObject gameObject)
{ {
gameObject.Assign(this);
gameObject.OnFinalized += OnGameObjectFinalize; gameObject.OnFinalized += OnGameObjectFinalize;
_gameObjects.Add(gameObject); _gameObjects.Add(gameObject);

View File

@ -12,6 +12,7 @@ public class GameObject : IGameObject
public Action<IAssignableTransform>? OnTransformAssigned { get; set; } = null; public Action<IAssignableTransform>? OnTransformAssigned { get; set; } = null;
public Action<IAssignable>? OnUnassigned { get; set; } = null; public Action<IAssignable>? OnUnassigned { get; set; } = null;
public Action<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; } = null; public Action<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; } = null;
public Action<IAssignableGameManager>? OnGameManagerAssigned { get; set; } = null;
public Action<IEntity>? OnNameChanged { get; set; } = null; public Action<IEntity>? OnNameChanged { get; set; } = null;
@ -24,6 +25,7 @@ public class GameObject : IGameObject
private ITransform _transform = null!; private ITransform _transform = null!;
private IBehaviourController _behaviourController = null!; private IBehaviourController _behaviourController = null!;
private IStateEnable _stateEnable = null!; private IStateEnable _stateEnable = null!;
private IGameManager _gameManager = null!;
private string _name = nameof(GameObject); private string _name = nameof(GameObject);
private bool _initialized = false; private bool _initialized = false;
@ -31,6 +33,7 @@ public class GameObject : IGameObject
public ITransform Transform => _transform; public ITransform Transform => _transform;
public IBehaviourController BehaviourController => _behaviourController; public IBehaviourController BehaviourController => _behaviourController;
public IStateEnable StateEnable => _stateEnable; public IStateEnable StateEnable => _stateEnable;
public IGameManager GameManager => _gameManager;
public bool Initialized public bool Initialized
{ {
@ -68,6 +71,7 @@ public class GameObject : IGameObject
NotAssignedException.Check(this, _transform); NotAssignedException.Check(this, _transform);
NotAssignedException.Check(this, _behaviourController); NotAssignedException.Check(this, _behaviourController);
NotAssignedException.Check(this, _stateEnable); NotAssignedException.Check(this, _stateEnable);
NotAssignedException.Check(this, _gameManager);
Initialized = true; Initialized = true;
return true; return true;
@ -126,6 +130,16 @@ public class GameObject : IGameObject
return true; return true;
} }
public bool Assign(IGameManager gameManager)
{
if (Initialized)
return false;
_gameManager = gameManager;
OnGameManagerAssigned?.Invoke(this);
return true;
}
public bool Unassign() public bool Unassign()
{ {
if (Initialized) if (Initialized)
@ -134,6 +148,7 @@ public class GameObject : IGameObject
_stateEnable = null!; _stateEnable = null!;
_transform = null!; _transform = null!;
_behaviourController = null!; _behaviourController = null!;
_gameManager = null!;
OnUnassigned?.Invoke(this); OnUnassigned?.Invoke(this);
return true; return true;