CodeLiturgy.Dashboard/BlueWest/Core/ComponentSystem/Component.cs

53 lines
1.4 KiB
C#

using System.Collections;
using BlueWest.Collections;
using BlueWest.Coroutines;
using BlueWest.Tools;
namespace BlueWest.Core.ComponentSystem
{
public class Component
{
protected readonly EventManager _eventManager;
public bool IsActive { get; private set; } = true;
private readonly FastDictionary<int, IEnumerator> _coroutines = new FastDictionary<int, IEnumerator>(10000);
private int _coroutineCount = 0;
public Component(EventManager eventManager)
{
_eventManager = eventManager;
}
public void StartCoroutine(IEnumerator routine)
{
_coroutines.Add(_coroutineCount, routine);
_coroutineCount++;
}
public void HandleCoroutines()
{
for (var i = 0; i < _coroutineCount; i++)
{
var yielded = _coroutines[i].Current is CustomYieldInstruction yielder && yielder.MoveNext();
if (yielded || _coroutines[i].MoveNext()) continue;
_coroutines.Remove(i);
_coroutineCount--;
i--;
}
}
public void EveryFrame(double delta)
{
if (!IsActive) return;
Update(delta);
HandleCoroutines();
}
public virtual void Update(double delta) { }
public virtual void Start() { }
}
}