position is now center, socket improvements

This commit is contained in:
Vinzenz Schroeter 2024-04-13 14:07:14 +02:00
commit d4d0abd013
5 changed files with 129 additions and 101 deletions

View file

@ -1,16 +0,0 @@
using DisplayCommands;
using TanksServer.GameLogic;
namespace TanksServer.Graphics;
internal sealed class BulletDrawer(BulletManager bullets) : IDrawStep
{
public void Draw(PixelGrid buffer)
{
foreach (var bullet in bullets.GetAll())
{
var pos = bullet.Position.ToPixelPosition();
buffer[pos.X, pos.Y] = true;
}
}
}

View file

@ -0,0 +1,13 @@
using DisplayCommands;
using TanksServer.GameLogic;
namespace TanksServer.Graphics;
internal sealed class DrawBulletsStep(BulletManager bullets) : IDrawStep
{
public void Draw(PixelGrid buffer)
{
foreach (var position in bullets.GetAll().Select(b => b.Position.ToPixelPosition()))
buffer[position.X, position.Y] = true;
}
}

View file

@ -0,0 +1,25 @@
using DisplayCommands;
using TanksServer.GameLogic;
namespace TanksServer.Graphics;
internal sealed class DrawMapStep(MapService map) : IDrawStep
{
public void Draw(PixelGrid buffer)
{
for (ushort tileY = 0; tileY < MapService.TilesPerColumn; tileY++)
for (ushort tileX = 0; tileX < MapService.TilesPerRow; tileX++)
{
var tile = new TilePosition(tileX, tileY);
if (!map.IsCurrentlyWall(tile))
continue;
for (byte pixelInTileY = 0; pixelInTileY < MapService.TileSize; pixelInTileY++)
for (byte pixelInTileX = 0; pixelInTileX < MapService.TileSize; pixelInTileX++)
{
var position = tile.GetPixelRelative(pixelInTileX, pixelInTileY);
buffer[position.X, position.Y] = pixelInTileX % 2 == pixelInTileY % 2;
}
}
}
}

View file

@ -0,0 +1,57 @@
using DisplayCommands;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using TanksServer.GameLogic;
namespace TanksServer.Graphics;
internal sealed class DrawTanksStep : IDrawStep
{
private readonly TankManager _tanks;
private readonly bool[] _tankSprite;
private readonly int _tankSpriteWidth;
public DrawTanksStep(TankManager tanks)
{
_tanks = tanks;
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;
}
public void Draw(PixelGrid buffer)
{
foreach (var tank in _tanks)
{
var tankPosition = tank.Bounds.TopLeft;
var orientation = (int)Math.Round(tank.Rotation * 16d) % 16;
for (byte dy = 0; dy < MapService.TileSize; dy++)
for (byte dx = 0; dx < MapService.TileSize; dx++)
{
if (!TankSpriteAt(dx, dy, orientation))
continue;
var position = tankPosition.GetPixelRelative(dx, dy);
buffer[position.X, position.Y] = true;
}
}
}
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];
}
}

View file

@ -0,0 +1,21 @@
using DisplayCommands;
using TanksServer.GameLogic;
namespace TanksServer.Graphics;
internal sealed class GeneratePixelsTickStep(
IEnumerable<IDrawStep> drawSteps,
LastFinishedFrameProvider lastFrameProvider
) : ITickStep
{
private readonly List<IDrawStep> _drawSteps = drawSteps.ToList();
public Task TickAsync()
{
var drawGrid = new PixelGrid(MapService.PixelsPerRow, MapService.PixelsPerColumn);
foreach (var step in _drawSteps)
step.Draw(drawGrid);
lastFrameProvider.LastFrame = drawGrid;
return Task.CompletedTask;
}
}