wip move to new uniffi language binding

This commit is contained in:
Vinzenz Schroeter 2024-11-12 18:27:04 +01:00
parent f7a5d8f823
commit 53cbdd8440
30 changed files with 211 additions and 187 deletions

View file

@ -1,4 +1,3 @@
using ServicePoint;
using TanksServer.GameLogic;
namespace TanksServer.Graphics;
@ -9,24 +8,24 @@ internal sealed class DrawMapStep(MapService map) : IDrawStep
private static void Draw(GamePixelGrid pixels, Map map)
{
for (ushort y = 0; y < MapService.PixelsPerColumn; y++)
for (ushort x = 0; x < MapService.PixelsPerRow; x++)
{
if (!map.IsWall(x, y))
continue;
for (ulong y = 0; y < MapService.PixelsPerColumn; y++)
for (ulong x = 0; x < MapService.PixelsPerRow; x++)
{
if (!map.IsWall(x, y))
continue;
pixels[x, y].EntityType = GamePixelEntityType.Wall;
}
pixels[x, y].EntityType = GamePixelEntityType.Wall;
}
}
public static void Draw(Bitmap pixels, Map map)
{
for (ushort y = 0; y < MapService.PixelsPerColumn; y++)
for (ushort x = 0; x < MapService.PixelsPerRow; x++)
{
if (!map.IsWall(x, y))
continue;
pixels[x, y] = true;
}
for (ulong y = 0; y < MapService.PixelsPerColumn; y++)
for (ulong x = 0; x < MapService.PixelsPerRow; x++)
{
if (!map.IsWall(x, y))
continue;
pixels.Set(x, y, true);
}
}
}

View file

@ -30,16 +30,16 @@ internal sealed class DrawPowerUpsStep(MapEntityManager entityManager) : IDrawSt
private static void DrawPowerUp(GamePixelGrid pixels, Sprite sprite, PixelPosition position)
{
for (byte dy = 0; dy < MapService.TileSize; dy++)
for (byte dx = 0; dx < MapService.TileSize; dx++)
{
var pixelState = sprite[dx, dy];
if (!pixelState.HasValue)
continue;
for (byte dx = 0; dx < MapService.TileSize; dx++)
{
var pixelState = sprite[dx, dy];
if (!pixelState.HasValue)
continue;
var (x, y) = position.GetPixelRelative(dx, dy);
pixels[x, y].EntityType = pixelState.Value
? GamePixelEntityType.PowerUp
: null;
}
var (x, y) = position.GetPixelRelative(dx, dy);
pixels[x, y].EntityType = pixelState.Value
? GamePixelEntityType.PowerUp
: null;
}
}
}

View file

@ -5,7 +5,7 @@ namespace TanksServer.Graphics;
internal sealed class DrawTanksStep(MapEntityManager entityManager) : IDrawStep
{
private readonly SpriteSheet _tankSprites =
SpriteSheet.FromImageFile("assets/tank.png", MapService.TileSize, MapService.TileSize);
SpriteSheet.FromImageFile("assets/tank.png", (int)MapService.TileSize, (int)MapService.TileSize);
public void Draw(GamePixelGrid pixels)
{
@ -14,16 +14,16 @@ internal sealed class DrawTanksStep(MapEntityManager entityManager) : IDrawStep
var tankPosition = tank.Bounds.TopLeft;
for (byte dy = 0; dy < MapService.TileSize; dy++)
for (byte dx = 0; dx < MapService.TileSize; dx++)
{
var pixel = _tankSprites[tank.Orientation][dx, dy];
if (!pixel.HasValue || !pixel.Value)
continue;
for (byte dx = 0; dx < MapService.TileSize; dx++)
{
var pixel = _tankSprites[tank.Orientation][dx, dy];
if (!pixel.HasValue || !pixel.Value)
continue;
var (x, y) = tankPosition.GetPixelRelative(dx, dy);
pixels[x, y].EntityType = GamePixelEntityType.Tank;
pixels[x, y].BelongsTo = tank.Owner;
}
var (x, y) = tankPosition.GetPixelRelative(dx, dy);
pixels[x, y].EntityType = GamePixelEntityType.Tank;
pixels[x, y].BelongsTo = tank.Owner;
}
}
}
}

View file

@ -5,27 +5,27 @@ namespace TanksServer.Graphics;
internal sealed class GamePixelGrid : IEnumerable<GamePixel>
{
public int Width { get; }
public int Height { get; }
public ulong Width { get; }
public ulong Height { get; }
private readonly GamePixel[,] _pixels;
public GamePixelGrid(int width, int height)
public GamePixelGrid(ulong width, ulong height)
{
Width = width;
Height = height;
_pixels = new GamePixel[width, height];
for (var y = 0; y < height; y++)
for (var x = 0; x < width; x++)
this[x, y] = new GamePixel();
for (ulong y = 0; y < height; y++)
for (ulong x = 0; x < width; x++)
this[x, y] = new GamePixel();
}
public GamePixel this[int x, int y]
public GamePixel this[ulong x, ulong y]
{
get
{
Debug.Assert(y * Width + x < _pixels.Length);
Debug.Assert(y * Width + x < (ulong)_pixels.Length);
return _pixels[x, y];
}
set => _pixels[x, y] = value;
@ -41,8 +41,8 @@ internal sealed class GamePixelGrid : IEnumerable<GamePixel>
public IEnumerator<GamePixel> GetEnumerator()
{
for (var y = 0; y < Height; y++)
for (var x = 0; x < Width; x++)
yield return this[x, y];
for (ulong y = 0; y < Height; y++)
for (ulong x = 0; x < Width; x++)
yield return this[x, y];
}
}

View file

@ -20,7 +20,7 @@ internal sealed class GeneratePixelsTickStep(
public async ValueTask TickAsync(TimeSpan _)
{
Draw(_gamePixelGrid, _observerPixelGrid);
if (_observerPixelGrid.Data.SequenceEqual(_lastObserverPixelGrid.Data))
if (_observerPixelGrid.Equals(_lastObserverPixelGrid))
return;
await _consumers.Select(c => c.OnFrameDoneAsync(_gamePixelGrid, _observerPixelGrid))
@ -37,12 +37,12 @@ internal sealed class GeneratePixelsTickStep(
step.Draw(gamePixelGrid);
observerPixelGrid.Fill(false);
for (var y = 0; y < MapService.PixelsPerColumn; y++)
for (var x = 0; x < MapService.PixelsPerRow; x++)
{
if (gamePixelGrid[x, y].EntityType.HasValue)
observerPixelGrid[(ushort)x, (ushort)y] = true;
}
for (ulong y = 0; y < MapService.PixelsPerColumn; y++)
for (ulong x = 0; x < MapService.PixelsPerRow; x++)
{
if (gamePixelGrid[x, y].EntityType.HasValue)
observerPixelGrid.Set(x, y, true);
}
}
public void Dispose()

View file

@ -12,13 +12,13 @@ internal sealed class Sprite(bool?[,] data)
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;
}
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);
}
@ -34,8 +34,8 @@ internal sealed class Sprite(bool?[,] data)
var result = new bool[Width, Height];
for (var y = 0; y < Height; y++)
for (var x = 0; x < Width; x++)
result[x, y] = this[x, y] ?? false;
for (var x = 0; x < Width; x++)
result[x, y] = this[x, y] ?? false;
return result;
}

View file

@ -21,24 +21,24 @@ internal sealed class SpriteSheet(Sprite[,] sprites)
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++)
for (var spriteX = 0; spriteX < spritesPerRow; spriteX++)
{
var x = spriteX * spriteWidth + dx;
var y = spriteY * spriteHeight + dy;
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;
var pixelValue = image[x, y];
data[dx, dy] = pixelValue.A == 0
? null
: pixelValue == whitePixel;
}
sprites[spriteX, spriteY] = new Sprite(data);
}
sprites[spriteX, spriteY] = new Sprite(data);
}
return new SpriteSheet(sprites);
}