34 lines
1.2 KiB
C#
34 lines
1.2 KiB
C#
using System.Collections.Generic;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using Syntriax.Engine.Core;
|
|
|
|
namespace Syntriax.Engine.Integration.MonoGame;
|
|
|
|
public class SpriteBatcher : BehaviourBase, IFirstFrameUpdate, IDraw
|
|
{
|
|
private static Comparer<IBehaviour> SortByPriority() => Comparer<IBehaviour>.Create((x, y) => y.Priority.CompareTo(x.Priority));
|
|
private SpriteBatch spriteBatch = null!;
|
|
private MonoGameCamera2DBehaviour camera2D = null!;
|
|
private readonly ActiveBehaviourCollectorSorted<IDrawableSprite> drawableSprites = new() { SortBy = SortByPriority() };
|
|
|
|
public void FirstActiveFrame()
|
|
{
|
|
MonoGameWindowContainer windowContainer = Universe.FindRequiredBehaviour<MonoGameWindowContainer>();
|
|
camera2D = Universe.FindRequiredBehaviour<MonoGameCamera2DBehaviour>();
|
|
|
|
spriteBatch = new(windowContainer.Window.GraphicsDevice);
|
|
drawableSprites.Unassign();
|
|
drawableSprites.Assign(Universe);
|
|
}
|
|
|
|
public void Draw()
|
|
{
|
|
spriteBatch.Begin(transformMatrix: camera2D.MatrixTransform);
|
|
for (int i = 0; i < drawableSprites.Count; i++)
|
|
drawableSprites[i].Draw(spriteBatch);
|
|
spriteBatch.End();
|
|
}
|
|
}
|