feat: IEntity.Id & BaseEntity

This commit is contained in:
Syntriax 2024-02-02 12:11:51 +03:00
parent 5d897f2f56
commit dbb263ebed
5 changed files with 143 additions and 168 deletions

View File

@ -0,0 +1,105 @@
using System;
namespace Syntriax.Engine.Core.Abstract;
public abstract class BaseEntity : IEntity
{
public Action<IEntity, string>? OnIdChanged { get; set; } = null;
public Action<IAssignable>? OnUnassigned { get; set; } = null;
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
public Action<IInitialize>? OnInitialized { get; set; } = null;
public Action<IInitialize>? OnFinalized { get; set; } = null;
private IStateEnable _stateEnable = null!;
private bool _initialized = false;
private string _id = string.Empty;
public virtual IStateEnable StateEnable => _stateEnable;
public virtual bool IsActive => StateEnable.Enabled;
public string Id
{
get => _id;
set
{
if (value == _id)
return;
string previousId = _id;
_id = value;
OnIdChanged?.Invoke(this, previousId);
}
}
public bool Initialized
{
get => _initialized;
private set
{
if (value == _initialized)
return;
_initialized = value;
if (value)
OnInitialized?.Invoke(this);
else
OnFinalized?.Invoke(this);
}
}
public bool Assign(IStateEnable stateEnable)
{
if (Initialized)
return false;
_stateEnable = stateEnable;
_stateEnable.Assign(this);
OnStateEnableAssigned?.Invoke(this);
return true;
}
protected virtual void UnassignInternal() { }
public bool Unassign()
{
if (Initialized)
return false;
UnassignInternal();
OnUnassigned?.Invoke(this);
return true;
}
protected virtual void InitializeInternal() { }
public bool Initialize()
{
if (Initialized)
return false;
InitializeInternal();
Initialized = true;
return true;
}
protected virtual void FinalizeInternal() { }
public bool Finalize()
{
if (!Initialized)
return false;
FinalizeInternal();
Initialized = false;
return true;
}
protected BaseEntity() => _id = Guid.NewGuid().ToString("D");
protected BaseEntity(string id) => _id = id;
}

View File

@ -1,3 +1,5 @@
using System;
namespace Syntriax.Engine.Core.Abstract;
/// <summary>
@ -5,4 +7,14 @@ namespace Syntriax.Engine.Core.Abstract;
/// </summary>
public interface IEntity : IInitialize, IAssignableStateEnable
{
/// <summary>
/// Event triggered when the <see cref="Id"/> of the <see cref="IEntity"/> changes.
/// The string action parameter is the previous <see cref="Id"/> of the <see cref="IEntity"/>.
/// </summary>
Action<IEntity, string>? OnIdChanged { get; set; }
/// <summary>
/// The ID of the <see cref="IEntity"/>.
/// </summary>
string Id { get; set; }
}

View File

@ -6,43 +6,20 @@ using Syntriax.Engine.Core.Exceptions;
namespace Syntriax.Engine.Core;
[System.Diagnostics.DebuggerDisplay("{GetType().Name, nq}, Priority: {Priority}, Initialized: {Initialized}")]
public abstract class Behaviour : IBehaviour
public abstract class Behaviour : BaseEntity, IBehaviour
{
public Action<IAssignable>? OnUnassigned { get; set; } = null;
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
public Action<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; } = null;
public Action<IInitialize>? OnInitialized { get; set; } = null;
public Action<IInitialize>? OnFinalized { get; set; } = null;
public Action<IBehaviour>? OnPriorityChanged { get; set; } = null;
private IBehaviourController _behaviourController = null!;
private IStateEnable _stateEnable = null!;
private bool _initialized = false;
private int _priority = 0;
public IStateEnable StateEnable => _stateEnable;
public IBehaviourController BehaviourController => _behaviourController;
public bool IsActive => StateEnable.Enabled && BehaviourController.GameObject.StateEnable.Enabled;
public bool Initialized
{
get => _initialized;
private set
{
if (value == _initialized)
return;
_initialized = value;
if (value)
OnInitialized?.Invoke(this);
else
OnFinalized?.Invoke(this);
}
}
public override bool IsActive => base.IsActive && BehaviourController.GameObject.StateEnable.Enabled;
public int Priority
{
@ -57,17 +34,6 @@ public abstract class Behaviour : IBehaviour
}
}
public bool Assign(IStateEnable stateEnable)
{
if (Initialized)
return false;
_stateEnable = stateEnable;
_stateEnable.Assign(this);
OnStateEnableAssigned?.Invoke(this);
return true;
}
public bool Assign(IBehaviourController behaviourController)
{
if (Initialized)
@ -78,36 +44,16 @@ public abstract class Behaviour : IBehaviour
return true;
}
public bool Unassign()
protected override void UnassignInternal()
{
if (Initialized)
return false;
_stateEnable = null!;
base.UnassignInternal();
_behaviourController = null!;
OnUnassigned?.Invoke(this);
return true;
}
public bool Initialize()
protected override void InitializeInternal()
{
if (Initialized)
return false;
base.InitializeInternal();
NotAssignedException.Check(this, _behaviourController);
NotAssignedException.Check(this, _stateEnable);
Initialized = true;
return true;
}
public bool Finalize()
{
if (!Initialized)
return false;
Initialized = false;
return true;
NotAssignedException.Check(this, StateEnable);
}
}

View File

@ -9,22 +9,15 @@ using Syntriax.Engine.Core.Factory;
namespace Syntriax.Engine.Core;
[System.Diagnostics.DebuggerDisplay("GameObject Count: {_gameObjects.Count}")]
public class GameManager : IGameManager
public class GameManager : BaseEntity, IGameManager
{
public Action<IGameManager, IGameObject>? OnGameObjectRegistered { get; set; } = null;
public Action<IGameManager, IGameObject>? OnGameObjectUnRegistered { get; set; } = null;
public Action<IInitialize>? OnInitialized { get; set; } = null;
public Action<IInitialize>? OnFinalized { get; set; } = null;
public Action<IAssignable>? OnUnassigned { get; set; } = null;
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
private readonly List<IGameObject> _gameObjects = new(Constants.GAME_OBJECTS_SIZE_INITIAL);
private IStateEnable _stateEnable = null!;
private GameObjectFactory _gameObjectFactory = null!;
private bool _initialized = false;
private GameObjectFactory GameObjectFactory
{
@ -36,21 +29,20 @@ public class GameManager : IGameManager
}
}
public bool Initialized => _initialized;
public IReadOnlyList<IGameObject> GameObjects => _gameObjects;
public IStateEnable StateEnable
public override IStateEnable StateEnable
{
get
{
if (_stateEnable is null)
if (base.StateEnable is null)
{
Assign(new StateEnableFactory().Instantiate(this));
if (_stateEnable is null)
throw NotAssignedException.From(this, _stateEnable);
if (base.StateEnable is null)
throw NotAssignedException.From(this, base.StateEnable);
}
return _stateEnable;
return base.StateEnable;
}
}
@ -78,52 +70,20 @@ public class GameManager : IGameManager
return gameObject;
}
public bool Initialize()
protected override void InitializeInternal()
{
if (Initialized)
return false;
base.InitializeInternal();
NotAssignedException.Check(this, StateEnable);
foreach (var gameObject in GameObjects)
gameObject.Initialize();
_initialized = true;
OnInitialized?.Invoke(this);
return true;
}
public bool Finalize()
protected override void FinalizeInternal()
{
if (!Initialized)
return false;
base.FinalizeInternal();
for (int i = GameObjects.Count; i >= 0; i--)
GameObjects[i].Finalize();
OnFinalized?.Invoke(this);
_initialized = false;
return true;
}
public bool Assign(IStateEnable stateEnable)
{
if (Initialized)
return false;
_stateEnable = stateEnable;
OnStateEnableAssigned?.Invoke(this);
return true;
}
public bool Unassign()
{
if (Initialized)
return false;
_stateEnable = null!;
OnUnassigned?.Invoke(this);
return true;
}
public void Update(EngineTime time)

View File

@ -6,19 +6,14 @@ using Syntriax.Engine.Core.Exceptions;
namespace Syntriax.Engine.Core;
[System.Diagnostics.DebuggerDisplay("Name: {Name}, Initialized: {Initialized}")]
public class GameObject : IGameObject
public class GameObject : BaseEntity, IGameObject
{
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
public Action<IAssignableTransform>? OnTransformAssigned { get; set; } = null;
public Action<IAssignable>? OnUnassigned { 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<IInitialize>? OnInitialized { get; set; } = null;
public Action<IInitialize>? OnFinalized { get; set; } = null;
public Action<IGameObject>? OnUpdated { get; set; } = null;
@ -28,29 +23,11 @@ public class GameObject : IGameObject
private IGameManager _gameManager = null!;
private string _name = nameof(GameObject);
private bool _initialized = false;
public ITransform Transform => _transform;
public IBehaviourController BehaviourController => _behaviourController;
public IStateEnable StateEnable => _stateEnable;
public IGameManager GameManager => _gameManager;
public bool Initialized
{
get => _initialized;
private set
{
if (value == _initialized)
return;
_initialized = value;
if (value)
OnInitialized?.Invoke(this);
else
OnFinalized?.Invoke(this);
}
}
public string Name
{
get => _name;
@ -63,18 +40,14 @@ public class GameObject : IGameObject
}
}
public bool Initialize()
protected override void InitializeInternal()
{
if (Initialized)
return false;
base.InitializeInternal();
NotAssignedException.Check(this, _transform);
NotAssignedException.Check(this, _behaviourController);
NotAssignedException.Check(this, _stateEnable);
NotAssignedException.Check(this, _gameManager);
Initialized = true;
return true;
}
public void Update()
@ -85,29 +58,12 @@ public class GameObject : IGameObject
OnUpdated?.Invoke(this);
}
public bool Finalize()
protected override void FinalizeInternal()
{
if (!Initialized)
return false;
base.FinalizeInternal();
System.Threading.Tasks.Parallel.ForEach(
_behaviourController.GetBehaviours<IBehaviour>(),
behaviour => behaviour.Finalize()
);
Initialized = false;
return true;
}
public bool Assign(IStateEnable stateEnable)
{
if (Initialized)
return false;
_stateEnable = stateEnable;
OnStateEnableAssigned?.Invoke(this);
return true;
foreach (IBehaviour behaviour in _behaviourController.GetBehaviours<IBehaviour>())
behaviour.Finalize();
}
public bool Assign(ITransform transform)
@ -140,18 +96,14 @@ public class GameObject : IGameObject
return true;
}
public bool Unassign()
protected override void UnassignInternal()
{
if (Initialized)
return false;
base.UnassignInternal();
_stateEnable = null!;
_transform = null!;
_behaviourController = null!;
_gameManager = null!;
OnUnassigned?.Invoke(this);
return true;
}
public GameObject() { OnBehaviourControllerAssigned += ConnectBehaviourController; }