diff --git a/Engine.Core/Abstract/BaseEntity.cs b/Engine.Core/Abstract/BaseEntity.cs new file mode 100644 index 0000000..b1c30a1 --- /dev/null +++ b/Engine.Core/Abstract/BaseEntity.cs @@ -0,0 +1,105 @@ +using System; + +namespace Syntriax.Engine.Core.Abstract; + +public abstract class BaseEntity : IEntity +{ + public Action? OnIdChanged { get; set; } = null; + + public Action? OnUnassigned { get; set; } = null; + public Action? OnStateEnableAssigned { get; set; } = null; + + public Action? OnInitialized { get; set; } = null; + public Action? 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; +} diff --git a/Engine.Core/Abstract/IEntity.cs b/Engine.Core/Abstract/IEntity.cs index e9532fe..11a824d 100644 --- a/Engine.Core/Abstract/IEntity.cs +++ b/Engine.Core/Abstract/IEntity.cs @@ -1,3 +1,5 @@ +using System; + namespace Syntriax.Engine.Core.Abstract; /// @@ -5,4 +7,14 @@ namespace Syntriax.Engine.Core.Abstract; /// public interface IEntity : IInitialize, IAssignableStateEnable { + /// + /// Event triggered when the of the changes. + /// The string action parameter is the previous of the . + /// + Action? OnIdChanged { get; set; } + + /// + /// The ID of the . + /// + string Id { get; set; } } diff --git a/Engine.Core/Behaviour.cs b/Engine.Core/Behaviour.cs index 4b9241b..12376a8 100644 --- a/Engine.Core/Behaviour.cs +++ b/Engine.Core/Behaviour.cs @@ -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? OnUnassigned { get; set; } = null; - public Action? OnStateEnableAssigned { get; set; } = null; public Action? OnBehaviourControllerAssigned { get; set; } = null; - public Action? OnInitialized { get; set; } = null; - public Action? OnFinalized { get; set; } = null; public Action? 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); } } diff --git a/Engine.Core/GameManager.cs b/Engine.Core/GameManager.cs index 49b4359..cd0ad07 100644 --- a/Engine.Core/GameManager.cs +++ b/Engine.Core/GameManager.cs @@ -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? OnGameObjectRegistered { get; set; } = null; public Action? OnGameObjectUnRegistered { get; set; } = null; - public Action? OnInitialized { get; set; } = null; - public Action? OnFinalized { get; set; } = null; - public Action? OnUnassigned { get; set; } = null; - public Action? OnStateEnableAssigned { get; set; } = null; - private readonly List _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 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) diff --git a/Engine.Core/GameObject.cs b/Engine.Core/GameObject.cs index 0e60147..8efb2fb 100644 --- a/Engine.Core/GameObject.cs +++ b/Engine.Core/GameObject.cs @@ -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? OnStateEnableAssigned { get; set; } = null; 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; - public Action? OnInitialized { get; set; } = null; - public Action? OnFinalized { get; set; } = null; - public Action? 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(), - 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()) + 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; }