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

53 lines
1.4 KiB
C#
Raw Permalink Normal View History

2021-12-06 02:49:27 +03:00
using System.Collections;
2022-08-13 08:34:20 +03:00
using System.Collections.Generic;
2021-12-06 02:49:27 +03:00
using BlueWest.Coroutines;
using BlueWest.Tools;
namespace BlueWest.Core.ComponentSystem
{
public class Component
{
protected readonly EventManager _eventManager;
public bool IsActive { get; private set; } = true;
2022-08-13 08:34:20 +03:00
private readonly Dictionary<int, IEnumerator> _coroutines = new Dictionary<int, IEnumerator>(10000);
2021-12-06 02:49:27 +03:00
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() { }
}
}