2024-04-07 19:05:50 +02:00
|
|
|
using SixLabors.ImageSharp;
|
|
|
|
using SixLabors.ImageSharp.PixelFormats;
|
2024-04-10 19:25:45 +02:00
|
|
|
using TanksServer.GameLogic;
|
2024-04-07 19:05:50 +02:00
|
|
|
|
2024-04-10 19:25:45 +02:00
|
|
|
namespace TanksServer.Graphics;
|
2024-04-07 19:05:50 +02:00
|
|
|
|
2024-04-13 14:07:14 +02:00
|
|
|
internal sealed class DrawTanksStep : IDrawStep
|
2024-04-07 19:05:50 +02:00
|
|
|
{
|
2024-04-17 19:34:19 +02:00
|
|
|
private readonly MapEntityManager _entityManager;
|
2024-04-07 19:05:50 +02:00
|
|
|
private readonly bool[] _tankSprite;
|
|
|
|
private readonly int _tankSpriteWidth;
|
|
|
|
|
2024-04-17 19:34:19 +02:00
|
|
|
public DrawTanksStep(MapEntityManager entityManager)
|
2024-04-07 19:05:50 +02:00
|
|
|
{
|
2024-04-17 19:34:19 +02:00
|
|
|
_entityManager = entityManager;
|
2024-04-07 19:05:50 +02:00
|
|
|
|
|
|
|
using var tankImage = Image.Load<Rgba32>("assets/tank.png");
|
|
|
|
_tankSprite = new bool[tankImage.Height * tankImage.Width];
|
|
|
|
|
|
|
|
var whitePixel = new Rgba32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);
|
|
|
|
var i = 0;
|
|
|
|
for (var y = 0; y < tankImage.Height; y++)
|
|
|
|
for (var x = 0; x < tankImage.Width; x++, i++)
|
|
|
|
_tankSprite[i] = tankImage[x, y] == whitePixel;
|
|
|
|
|
|
|
|
_tankSpriteWidth = tankImage.Width;
|
|
|
|
}
|
|
|
|
|
2024-04-15 20:34:23 +02:00
|
|
|
public void Draw(GamePixelGrid pixels)
|
2024-04-07 19:05:50 +02:00
|
|
|
{
|
2024-04-17 19:34:19 +02:00
|
|
|
foreach (var tank in _entityManager.Tanks)
|
2024-04-07 19:05:50 +02:00
|
|
|
{
|
2024-04-13 14:07:14 +02:00
|
|
|
var tankPosition = tank.Bounds.TopLeft;
|
2024-04-12 16:05:24 +02:00
|
|
|
|
2024-04-12 18:32:10 +02:00
|
|
|
for (byte dy = 0; dy < MapService.TileSize; dy++)
|
|
|
|
for (byte dx = 0; dx < MapService.TileSize; dx++)
|
2024-04-07 19:05:50 +02:00
|
|
|
{
|
2024-04-13 18:35:36 +02:00
|
|
|
if (!TankSpriteAt(dx, dy, tank.Orientation))
|
2024-04-10 22:39:33 +02:00
|
|
|
continue;
|
2024-04-07 19:05:50 +02:00
|
|
|
|
2024-04-13 16:27:45 +02:00
|
|
|
var (x, y) = tankPosition.GetPixelRelative(dx, dy);
|
2024-04-15 20:34:23 +02:00
|
|
|
pixels[x, y].EntityType = GamePixelEntityType.Tank;
|
|
|
|
pixels[x, y].BelongsTo = tank.Owner;
|
2024-04-07 19:05:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
var index = (y + dy) * _tankSpriteWidth + x + dx;
|
|
|
|
|
|
|
|
return _tankSprite[index];
|
|
|
|
}
|
2024-04-13 14:07:14 +02:00
|
|
|
}
|