feat: Basic CoroutineManager
This commit is contained in:
parent
43f1749b04
commit
2b6b366f4a
|
@ -0,0 +1,33 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
|
||||
namespace Syntriax.Engine.Core;
|
||||
|
||||
public class CoroutineManager : HierarchyObjectBase
|
||||
{
|
||||
public List<IEnumerator> enumerators = [];
|
||||
|
||||
public void CreateCoroutine(IEnumerator enumerator)
|
||||
{
|
||||
enumerators.Add(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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue