sprite helper classes
This commit is contained in:
parent
83ee185c50
commit
a486f73046
|
@ -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<Rgba32>("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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
27
tanks-backend/TanksServer/Graphics/Sprite.cs
Normal file
27
tanks-backend/TanksServer/Graphics/Sprite.cs
Normal file
|
@ -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<Rgba32>(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];
|
||||
}
|
48
tanks-backend/TanksServer/Graphics/SpriteSheet.cs
Normal file
48
tanks-backend/TanksServer/Graphics/SpriteSheet.cs
Normal file
|
@ -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<Rgba32>(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)];
|
||||
}
|
Binary file not shown.
Before Width: | Height: | Size: 355 B After Width: | Height: | Size: 493 B |
Loading…
Reference in a new issue