Engine-Pong/Shared/Behaviours/ShapeBatcher.cs

36 lines
1.3 KiB
C#

using System.Collections.Generic;
using Apos.Shapes;
using Syntriax.Engine.Core;
using Syntriax.Engine.Integration.MonoGame;
namespace Pong.Behaviours;
public class ShapeBatcher : BehaviourBase, IFirstFrameUpdate, IDraw
{
private static Comparer<IBehaviour> SortByPriority() => Comparer<IBehaviour>.Create((x, y) => y.Priority.CompareTo(x.Priority));
private ShapeBatch shapeBatch = null!;
private MonoGameCamera2DBehaviour camera2D = null!;
private readonly ActiveBehaviourCollectorSorted<IDrawableShape> drawableShapes = new() { SortBy = SortByPriority() };
public void FirstActiveFrame()
{
MonoGameWindowContainer windowContainer = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameWindowContainer>();
camera2D = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameCamera2DBehaviour>();
shapeBatch = new(windowContainer.Window.GraphicsDevice, windowContainer.Window.Content);
drawableShapes.Unassign();
drawableShapes.Assign(Universe);
}
public void Draw()
{
shapeBatch.Begin(camera2D.MatrixTransform);
for (int i = drawableShapes.Count - 1; i >= 0; i--)
drawableShapes[i].Draw(shapeBatch);
shapeBatch.End();
}
}