From 6bc9043a8685b980a612ed59ca2301054add3773 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sat, 26 Oct 2024 13:46:22 +0300 Subject: [PATCH] feat: Basic CoroutineManager --- Engine.Core/CoroutineManager.cs | 39 ++++++++ Engine.Core/HierarchyObjectBase.cs | 144 +++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 Engine.Core/CoroutineManager.cs create mode 100644 Engine.Core/HierarchyObjectBase.cs diff --git a/Engine.Core/CoroutineManager.cs b/Engine.Core/CoroutineManager.cs new file mode 100644 index 0000000..d827222 --- /dev/null +++ b/Engine.Core/CoroutineManager.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; + +using Syntriax.Engine.Core.Abstract; + +namespace Syntriax.Engine.Core; + +public class CoroutineManager : HierarchyObjectBase +{ + public List enumerators = []; + + public IEnumerator CreateCoroutine(IEnumerator enumerator) + { + enumerators.Add(enumerator); + return enumerator; + } + + public void StopCoroutine(IEnumerator enumerator) + { + enumerators.Remove(enumerator); + } + + protected override void OnEnteringHierarchy(IGameManager gameManager) + { + gameManager.OnUpdate += OnUpdate; + } + + protected override void OnExitingHierarchy(IGameManager gameManager) + { + gameManager.OnUpdate -= OnUpdate; + } + + private void OnUpdate(IGameManager sender, EngineTime time) + { + for (int i = enumerators.Count - 1; i >= 0; i--) + if (!enumerators[i].MoveNext()) + enumerators.RemoveAt(i); + } +} diff --git a/Engine.Core/HierarchyObjectBase.cs b/Engine.Core/HierarchyObjectBase.cs new file mode 100644 index 0000000..ea855f2 --- /dev/null +++ b/Engine.Core/HierarchyObjectBase.cs @@ -0,0 +1,144 @@ +using Syntriax.Engine.Core.Abstract; + +namespace Syntriax.Engine.Core; + +public abstract class HierarchyObjectBase : IHierarchyObject +{ + public event IHierarchyObject.OnEnteredHierarchyDelegate? OnEnteredHierarchy = null; + public event IHierarchyObject.OnExitedHierarchyDelegate? OnExitedHierarchy = null; + public event IEntity.OnIdChangedDelegate? OnIdChanged = null; + public event IInitialize.OnInitializedDelegate? OnInitialized = null; + public event IInitialize.OnFinalizedDelegate? OnFinalized = null; + public event IAssignableStateEnable.OnStateEnableAssignedDelegate? OnStateEnableAssigned = null; + public event IAssignable.OnUnassignedDelegate? OnUnassigned = null; + public event INameable.OnNameChangedDelegate? OnNameChanged = null; + + private string _id = string.Empty; + private string _name = nameof(HierarchyObjectBase); + private bool _initialized = false; + private IStateEnable _stateEnable = null!; + private IGameManager? _gameManager = null; + + public IGameManager? GameManager => _gameManager; + + public bool IsInHierarchy => _gameManager is not null; + + public bool IsInitialized + { + get => _initialized; + private set + { + if (value == _initialized) + return; + + _initialized = value; + if (value) + OnInitialized?.Invoke(this); + else + OnFinalized?.Invoke(this); + } + } + + public virtual IStateEnable StateEnable => _stateEnable; + + public string Name + { + get => _name; + set + { + if (value == _name) return; + + string previousName = _name; + _name = value; + OnNameChanged?.Invoke(this, previousName); + } + } + + public string Id + { + get => _id; + set + { + if (value == _id) + return; + + string previousId = _id; + + _id = value; + OnIdChanged?.Invoke(this, previousId); + } + } + + public bool Assign(IStateEnable stateEnable) + { + if (IsInitialized) + return false; + + _stateEnable = stateEnable; + _stateEnable.Assign(this); + OnStateEnableAssigned?.Invoke(this); + return true; + } + + protected virtual void UnassignInternal() { } + public bool Unassign() + { + if (IsInitialized) + return false; + + UnassignInternal(); + + OnUnassigned?.Invoke(this); + return true; + } + + protected virtual void InitializeInternal() { } + public bool Initialize() + { + if (IsInitialized) + return false; + + InitializeInternal(); + + IsInitialized = true; + return true; + } + + protected virtual void FinalizeInternal() { } + public bool Finalize() + { + if (!IsInitialized) + return false; + + FinalizeInternal(); + + IsInitialized = false; + return true; + } + + protected virtual void OnEnteringHierarchy(IGameManager gameManager) { } + bool IHierarchyObject.EnterHierarchy(IGameManager gameManager) + { + if (IsInHierarchy) + return false; + + _gameManager = gameManager; + OnEnteringHierarchy(gameManager); + OnEnteredHierarchy?.Invoke(this); + return true; + } + + protected virtual void OnExitingHierarchy(IGameManager gameManager) { } + bool IHierarchyObject.ExitHierarchy() + { + if (!IsInHierarchy) + return false; + + IGameManager? previousGameManager = _gameManager; + _gameManager = null; + if (previousGameManager is not null) + OnExitingHierarchy(previousGameManager); + OnExitedHierarchy?.Invoke(this); + return true; + } +}