separate folders per functionality
This commit is contained in:
parent
7f00160780
commit
0ca6a91a7e
33 changed files with 60 additions and 113 deletions
13
TanksServer/Graphics/BulletDrawer.cs
Normal file
13
TanksServer/Graphics/BulletDrawer.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using TanksServer.GameLogic;
|
||||
using TanksServer.ServicePointDisplay;
|
||||
|
||||
namespace TanksServer.Graphics;
|
||||
|
||||
internal sealed class BulletDrawer(BulletManager bullets): IDrawStep
|
||||
{
|
||||
public void Draw(PixelDisplayBufferView buffer)
|
||||
{
|
||||
foreach (var bullet in bullets.GetAll())
|
||||
buffer.Pixels[bullet.Position.ToPixelPosition().ToPixelIndex()] = true;
|
||||
}
|
||||
}
|
20
TanksServer/Graphics/DrawStateToFrame.cs
Normal file
20
TanksServer/Graphics/DrawStateToFrame.cs
Normal file
|
@ -0,0 +1,20 @@
|
|||
using TanksServer.GameLogic;
|
||||
using TanksServer.ServicePointDisplay;
|
||||
|
||||
namespace TanksServer.Graphics;
|
||||
|
||||
internal sealed class DrawStateToFrame(
|
||||
IEnumerable<IDrawStep> drawSteps, LastFinishedFrameProvider lastFrameProvider
|
||||
) : ITickStep
|
||||
{
|
||||
private readonly List<IDrawStep> _drawSteps = drawSteps.ToList();
|
||||
|
||||
public Task TickAsync()
|
||||
{
|
||||
var buffer = PixelDisplayBufferView.New(0, 0, MapService.TilesPerRow, MapService.PixelsPerColumn);
|
||||
foreach (var step in _drawSteps)
|
||||
step.Draw(buffer);
|
||||
lastFrameProvider.LastFrame = buffer;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
8
TanksServer/Graphics/IDrawStep.cs
Normal file
8
TanksServer/Graphics/IDrawStep.cs
Normal file
|
@ -0,0 +1,8 @@
|
|||
using TanksServer.ServicePointDisplay;
|
||||
|
||||
namespace TanksServer.Graphics;
|
||||
|
||||
internal interface IDrawStep
|
||||
{
|
||||
void Draw(PixelDisplayBufferView buffer);
|
||||
}
|
14
TanksServer/Graphics/LastFinishedFrameProvider.cs
Normal file
14
TanksServer/Graphics/LastFinishedFrameProvider.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using TanksServer.ServicePointDisplay;
|
||||
|
||||
namespace TanksServer.Graphics;
|
||||
|
||||
internal sealed class LastFinishedFrameProvider
|
||||
{
|
||||
private PixelDisplayBufferView? _lastFrame;
|
||||
|
||||
public PixelDisplayBufferView LastFrame
|
||||
{
|
||||
get => _lastFrame ?? throw new InvalidOperationException("first frame not yet drawn");
|
||||
set => _lastFrame = value;
|
||||
}
|
||||
}
|
25
TanksServer/Graphics/MapDrawer.cs
Normal file
25
TanksServer/Graphics/MapDrawer.cs
Normal file
|
@ -0,0 +1,25 @@
|
|||
using TanksServer.GameLogic;
|
||||
using TanksServer.ServicePointDisplay;
|
||||
|
||||
namespace TanksServer.Graphics;
|
||||
|
||||
internal sealed class MapDrawer(MapService map) : IDrawStep
|
||||
{
|
||||
public void Draw(PixelDisplayBufferView buffer)
|
||||
{
|
||||
for (var tileY = 0; tileY < MapService.TilesPerColumn; tileY++)
|
||||
for (var 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 index = tile.GetPixelRelative(pixelInTileX, pixelInTileY).ToPixelIndex();
|
||||
buffer.Pixels[index] = pixelInTileX % 2 == pixelInTileY % 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
62
TanksServer/Graphics/TankDrawer.cs
Normal file
62
TanksServer/Graphics/TankDrawer.cs
Normal file
|
@ -0,0 +1,62 @@
|
|||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using TanksServer.GameLogic;
|
||||
using TanksServer.ServicePointDisplay;
|
||||
|
||||
namespace TanksServer.Graphics;
|
||||
|
||||
internal sealed class TankDrawer : IDrawStep
|
||||
{
|
||||
private readonly TankManager _tanks;
|
||||
private readonly bool[] _tankSprite;
|
||||
private readonly int _tankSpriteWidth;
|
||||
|
||||
public TankDrawer(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(PixelDisplayBufferView buffer)
|
||||
{
|
||||
foreach (var tank in _tanks)
|
||||
{
|
||||
var pos = tank.Position.ToPixelPosition();
|
||||
var rotationVariant = (int)Math.Round(tank.Rotation) % 16;
|
||||
for (var dy = 0; dy < MapService.TileSize; dy++)
|
||||
{
|
||||
var rowStartIndex = (pos.Y + dy) * MapService.PixelsPerRow;
|
||||
|
||||
for (var dx = 0; dx < MapService.TileSize; dx++)
|
||||
{
|
||||
if (!TankSpriteAt(dx, dy, rotationVariant))
|
||||
continue;
|
||||
|
||||
var i = rowStartIndex + pos.X + dx;
|
||||
buffer.Pixels[i] = 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];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue