move a bunch of stuff into subfolders

This commit is contained in:
Vinzenz Schroeter 2024-04-07 11:19:14 +02:00
parent 8f281d65b2
commit 151cad4cee
13 changed files with 44 additions and 27 deletions

View file

@ -0,0 +1,37 @@
using Microsoft.Extensions.Hosting;
namespace TanksServer.Services;
internal sealed class GameTickService(IEnumerable<ITickStep> steps) : IHostedService
{
private readonly CancellationTokenSource _cancellation = new();
private readonly List<ITickStep> _steps = steps.ToList();
private Task? _run;
public Task StartAsync(CancellationToken cancellationToken)
{
_run = RunAsync();
return Task.CompletedTask;
}
private async Task RunAsync()
{
while (!_cancellation.IsCancellationRequested)
{
foreach (var step in _steps)
await step.TickAsync();
await Task.Delay(1000);
}
}
public async Task StopAsync(CancellationToken cancellationToken)
{
await _cancellation.CancelAsync();
if (_run != null) await _run;
}
}
public interface ITickStep
{
Task TickAsync();
}

View file

@ -0,0 +1,56 @@
namespace TanksServer.Services;
internal class MapDrawer(MapService map):ITickStep
{
private const uint GameFieldPixelCount = MapService.PixelsPerRow * MapService.PixelsPerColumn;
private void DrawInto(DisplayPixelBuffer buf)
{
for (var tileY = 0; tileY < MapService.TilesPerColumn; tileY++)
for (var tileX = 0; tileX < MapService.TilesPerRow; tileX++)
{
if (!map.IsCurrentlyWall(tileX, tileY))
continue;
var absoluteTilePixelY = tileY * MapService.TileSize;
for (var pixelInTileY = 0; pixelInTileY < MapService.TileSize; pixelInTileY++)
{
var absoluteRowStartPixelIndex = (absoluteTilePixelY + pixelInTileY) * MapService.PixelsPerRow
+ tileX * MapService.TileSize;
for (var pixelInTileX = 0; pixelInTileX < MapService.TileSize; pixelInTileX++)
buf.Pixels[absoluteRowStartPixelIndex + pixelInTileX] = pixelInTileX % 2 == pixelInTileY % 2;
}
}
}
private DisplayPixelBuffer CreateGameFieldPixelBuffer()
{
var data = new byte[10 + GameFieldPixelCount / 8];
var result = new DisplayPixelBuffer(data)
{
Magic1 = 0,
Magic2 = 19,
X = 0,
Y = 0,
WidthInTiles = MapService.TilesPerRow,
HeightInPixels = MapService.PixelsPerColumn
};
return result;
}
private DisplayPixelBuffer? _lastFrame;
public DisplayPixelBuffer LastFrame
{
get => _lastFrame ?? throw new InvalidOperationException("first frame not yet drawn");
private set => _lastFrame = value;
}
public Task TickAsync()
{
var buffer = CreateGameFieldPixelBuffer();
DrawInto(buffer);
LastFrame = buffer;
return Task.CompletedTask;
}
}

View file

@ -0,0 +1,42 @@
namespace TanksServer.Services;
internal class MapService
{
public const int TilesPerRow = 44;
public const int TilesPerColumn = 20;
public const int TileSize = 8;
public const int PixelsPerRow = TilesPerRow * TileSize;
public const int PixelsPerColumn = TilesPerColumn * TileSize;
private readonly string _map =
"""
############################################
#...................##.....................#
#...................##.....................#
#.....####......................####.......#
#..........................................#
#............###...........###.............#
#............#...............#.............#
#...##.......#...............#......##.....#
#....#..............................#......#
#....#..##......................##..#......#
#....#..##......................##..#......#
#....#..............................#......#
#...##.......#...............#......##.....#
#............#...............#.............#
#............###...........###.............#
#..........................................#
#.....####......................####.......#
#...................##.....................#
#...................##.....................#
############################################
"""
.ReplaceLineEndings(string.Empty);
private char this[int tileX, int tileY] => _map[tileX + tileY * TilesPerRow];
public bool IsCurrentlyWall(int tileX, int tileY)
{
return this[tileX, tileY] == '#';
}
}

View file

@ -0,0 +1,20 @@
using System.Net.Sockets;
using Microsoft.Extensions.Options;
namespace TanksServer.Services;
internal sealed class ServicePointDisplay(IOptions<ServicePointDisplayConfiguration> options)
{
private readonly UdpClient _udpClient = new(options.Value.Hostname, options.Value.Port);
public ValueTask<int> Send(DisplayPixelBuffer buffer)
{
return _udpClient.SendAsync(buffer.Data);
}
}
internal sealed class ServicePointDisplayConfiguration
{
public string Hostname { get; set; } = string.Empty;
public int Port { get; set; }
}