Files
Syntriax.Engine/Engine.Core/Systems/DrawManager.cs
Syntriax 988a6f67f2 BREAKING CHANGE: renamed original Behaviour class to BehaviourInternal, and replaced it with BehaviourBase
Original Behaviour was using old methods for detecting entering/exiting universe,
they are now all under the same hood and the original is kept for UniverseEntranceManager
because it needs to enter the universe without itself. The internal behaviour kept under
a subnamespace of "Core.Internal" for the purpose that it might come in handy for other use cases.
2025-10-22 16:50:19 +03:00

55 lines
1.9 KiB
C#

using System.Collections.Generic;
namespace Engine.Core;
public class DrawManager : Behaviour, IEnterUniverse, IExitUniverse
{
// We use Descending order because draw calls are running from last to first
private static Comparer<int> SortByDescendingPriority() => Comparer<int>.Create((x, y) => y.CompareTo(x));
private static System.Func<IBehaviour, int> GetPriority() => (b) => b.Priority;
private readonly ActiveBehaviourCollectorOrdered<int, IPreDraw> preDrawEntities = new(GetPriority(), SortByDescendingPriority());
private readonly ActiveBehaviourCollectorOrdered<int, IDraw> drawEntities = new(GetPriority(), SortByDescendingPriority());
private readonly ActiveBehaviourCollectorOrdered<int, IPostDraw> postDrawEntities = new(GetPriority(), SortByDescendingPriority());
private void OnPreDraw(IUniverse sender)
{
for (int i = preDrawEntities.Count - 1; i >= 0; i--)
preDrawEntities[i].PreDraw();
}
private void OnDraw(IUniverse sender)
{
for (int i = drawEntities.Count - 1; i >= 0; i--)
drawEntities[i].Draw();
}
private void OnPostDraw(IUniverse sender)
{
for (int i = postDrawEntities.Count - 1; i >= 0; i--)
postDrawEntities[i].PostDraw();
}
public void EnterUniverse(IUniverse universe)
{
preDrawEntities.Assign(universe);
drawEntities.Assign(universe);
postDrawEntities.Assign(universe);
universe.OnPreDraw.AddListener(OnPreDraw);
universe.OnDraw.AddListener(OnDraw);
universe.OnPostDraw.AddListener(OnPostDraw);
}
public void ExitUniverse(IUniverse universe)
{
preDrawEntities.Unassign();
drawEntities.Unassign();
postDrawEntities.Unassign();
universe.OnPreDraw.RemoveListener(OnPreDraw);
universe.OnDraw.RemoveListener(OnDraw);
universe.OnPostDraw.RemoveListener(OnPostDraw);
}
}