52 lines
1.7 KiB
C#
52 lines
1.7 KiB
C#
|
|
namespace Syntriax.Engine.Core;
|
|
|
|
public class DrawManager : UniverseObject
|
|
{
|
|
private static System.Comparison<IBehaviour> SortByPriority() => (x, y) => y.Priority.CompareTo(x.Priority);
|
|
|
|
private readonly ActiveBehaviourCollectorSorted<IPreDraw> preDrawEntities = new() { SortBy = SortByPriority() };
|
|
private readonly ActiveBehaviourCollectorSorted<IDraw> drawEntities = new() { SortBy = SortByPriority() };
|
|
private readonly ActiveBehaviourCollectorSorted<IPostDraw> postDrawEntities = new() { SortBy = SortByPriority() };
|
|
|
|
private void OnPreDraw(IUniverse sender)
|
|
{
|
|
for (int i = preDrawEntities.Behaviours.Count - 1; i >= 0; i--)
|
|
preDrawEntities.Behaviours[i].PreDraw();
|
|
}
|
|
|
|
private void OnDraw(IUniverse sender)
|
|
{
|
|
for (int i = drawEntities.Behaviours.Count - 1; i >= 0; i--)
|
|
drawEntities.Behaviours[i].Draw();
|
|
}
|
|
|
|
private void OnPostDraw(IUniverse sender)
|
|
{
|
|
for (int i = postDrawEntities.Behaviours.Count - 1; i >= 0; i--)
|
|
postDrawEntities.Behaviours[i].PostDraw();
|
|
}
|
|
|
|
protected override void OnEnteringUniverse(IUniverse universe)
|
|
{
|
|
preDrawEntities.Assign(universe);
|
|
drawEntities.Assign(universe);
|
|
postDrawEntities.Assign(universe);
|
|
|
|
universe.OnPreDraw += OnPreDraw;
|
|
universe.OnDraw += OnDraw;
|
|
universe.OnPostDraw += OnPostDraw;
|
|
}
|
|
|
|
protected override void OnExitingUniverse(IUniverse universe)
|
|
{
|
|
preDrawEntities.Unassign();
|
|
drawEntities.Unassign();
|
|
postDrawEntities.Unassign();
|
|
|
|
universe.OnPreDraw -= OnPreDraw;
|
|
universe.OnDraw -= OnDraw;
|
|
universe.OnPostDraw -= OnPostDraw;
|
|
}
|
|
}
|