sprite helper classes

This commit is contained in:
Vinzenz Schroeter 2024-04-22 20:58:12 +02:00
parent 83ee185c50
commit a486f73046
5 changed files with 82 additions and 52 deletions

View file

@ -1,12 +1,11 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using TanksServer.GameLogic;
namespace TanksServer.Graphics;
internal sealed class DrawTanksStep(MapEntityManager entityManager) : IDrawStep
{
private readonly bool[,] _tankSprite = LoadTankSprite();
private readonly SpriteSheet _tankSprites =
SpriteSheet.FromImageFile("assets/tank.png", MapService.TileSize, MapService.TileSize);
public void Draw(GamePixelGrid pixels)
{
@ -17,7 +16,8 @@ internal sealed class DrawTanksStep(MapEntityManager entityManager) : IDrawStep
for (byte dy = 0; dy < MapService.TileSize; dy++)
for (byte dx = 0; dx < MapService.TileSize; dx++)
{
if (!TankSpriteAt(dx, dy, tank.Orientation))
var pixel = _tankSprites[tank.Orientation][dx, dy];
if (!pixel.HasValue || !pixel.Value)
continue;
var (x, y) = tankPosition.GetPixelRelative(dx, dy);
@ -26,26 +26,4 @@ internal sealed class DrawTanksStep(MapEntityManager entityManager) : IDrawStep
}
}
}
private bool TankSpriteAt(int dx, int dy, int tankRotation)
{
var x = tankRotation % 4 * (MapService.TileSize + 1);
var y = (int)Math.Floor(tankRotation / 4d) * (MapService.TileSize + 1);
return _tankSprite[x + dx, y + dy];
}
private static bool[,] LoadTankSprite()
{
using var tankImage = Image.Load<Rgba32>("assets/tank.png");
var tankSprite = new bool[tankImage.Width, tankImage.Height];
var whitePixel = new Rgba32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);
for (var y = 0; y < tankImage.Height; y++)
for (var x = 0; x < tankImage.Width; x++)
tankSprite[x, y] = tankImage[x, y] == whitePixel;
return tankSprite;
}
}