diff --git a/tanks-backend/TanksServer/Graphics/DrawPowerUpsStep.cs b/tanks-backend/TanksServer/Graphics/DrawPowerUpsStep.cs index dc898c7..c67a5e6 100644 --- a/tanks-backend/TanksServer/Graphics/DrawPowerUpsStep.cs +++ b/tanks-backend/TanksServer/Graphics/DrawPowerUpsStep.cs @@ -1,37 +1,14 @@ -using System.Diagnostics; -using SixLabors.ImageSharp; -using SixLabors.ImageSharp.PixelFormats; using TanksServer.GameLogic; namespace TanksServer.Graphics; -internal sealed class DrawPowerUpsStep : IDrawStep +internal sealed class DrawPowerUpsStep(MapEntityManager entityManager) : IDrawStep { - private readonly MapEntityManager _entityManager; - private readonly bool?[,] _explosiveSprite; - - public DrawPowerUpsStep(MapEntityManager entityManager) - { - _entityManager = entityManager; - - using var tankImage = Image.Load("assets/powerup_explosive.png"); - Debug.Assert(tankImage.Width == tankImage.Height && tankImage.Width == MapService.TileSize); - _explosiveSprite = 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++) - { - var pixelValue = tankImage[x, y]; - _explosiveSprite[x, y] = pixelValue.A == 0 - ? null - : pixelValue == whitePixel; - } - } + private readonly Sprite _explosiveSprite = Sprite.FromImageFile("assets/powerup_explosive.png"); public void Draw(GamePixelGrid pixels) { - foreach (var powerUp in _entityManager.PowerUps) + foreach (var powerUp in entityManager.PowerUps) { var position = powerUp.Bounds.TopLeft; diff --git a/tanks-backend/TanksServer/Graphics/DrawTanksStep.cs b/tanks-backend/TanksServer/Graphics/DrawTanksStep.cs index 98cd14f..9bd365e 100644 --- a/tanks-backend/TanksServer/Graphics/DrawTanksStep.cs +++ b/tanks-backend/TanksServer/Graphics/DrawTanksStep.cs @@ -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("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; - } } diff --git a/tanks-backend/TanksServer/Graphics/Sprite.cs b/tanks-backend/TanksServer/Graphics/Sprite.cs new file mode 100644 index 0000000..e8bef11 --- /dev/null +++ b/tanks-backend/TanksServer/Graphics/Sprite.cs @@ -0,0 +1,27 @@ +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; + +namespace TanksServer.Graphics; + +internal sealed class Sprite(bool?[,] data) +{ + public static Sprite FromImageFile(string filePath) + { + using var image = Image.Load(filePath); + var data = new bool?[image.Width, image.Height]; + + var whitePixel = new Rgba32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue); + for (var y = 0; y < image.Height; y++) + for (var x = 0; x < image.Width; x++) + { + var pixelValue = image[x, y]; + data[x, y] = pixelValue.A == 0 + ? null + : pixelValue == whitePixel; + } + + return new Sprite(data); + } + + public bool? this[int x, int y] => data[x, y]; +} diff --git a/tanks-backend/TanksServer/Graphics/SpriteSheet.cs b/tanks-backend/TanksServer/Graphics/SpriteSheet.cs new file mode 100644 index 0000000..514cfb1 --- /dev/null +++ b/tanks-backend/TanksServer/Graphics/SpriteSheet.cs @@ -0,0 +1,48 @@ +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; + +namespace TanksServer.Graphics; + +internal sealed class SpriteSheet(Sprite[,] sprites) +{ + public static SpriteSheet FromImageFile(string filePath, int spriteWidth, int spriteHeight) + { + using var image = Image.Load(filePath); + + var spritesPerRow = image.Width / spriteWidth; + if (image.Width % spriteWidth != 0) + throw new InvalidOperationException("invalid sprite dimensions"); + + var spritesPerColumn = image.Height / spriteHeight; + if (image.Height % spriteHeight != 0) + throw new InvalidOperationException("invalid sprite dimensions"); + + var sprites = new Sprite[spritesPerRow, spritesPerColumn]; + + var whitePixel = new Rgba32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue); + for (var spriteY = 0; spriteY < spritesPerColumn; spriteY++) + for (var spriteX = 0; spriteX < spritesPerRow; spriteX++) + { + var data = new bool?[spriteWidth, spriteHeight]; + for (var dy = 0; dy < spriteHeight; dy++) + for (var dx = 0; dx < spriteWidth; dx++) + { + var x = spriteX * spriteWidth + dx; + var y = spriteY * spriteHeight + dy; + + var pixelValue = image[x, y]; + data[dx, dy] = pixelValue.A == 0 + ? null + : pixelValue == whitePixel; + } + + sprites[spriteX, spriteY] = new Sprite(data); + } + + return new SpriteSheet(sprites); + } + + public Sprite this[int x, int y] => sprites[x, y]; + + public Sprite this[int index] => sprites[index % sprites.GetLength(1), index / sprites.GetLength(1)]; +} diff --git a/tanks-backend/TanksServer/assets/tank.png b/tanks-backend/TanksServer/assets/tank.png index 9210ac1..f168e54 100644 Binary files a/tanks-backend/TanksServer/assets/tank.png and b/tanks-backend/TanksServer/assets/tank.png differ