separate folders per functionality

This commit is contained in:
Vinzenz Schroeter 2024-04-10 19:25:45 +02:00
commit 0ca6a91a7e
33 changed files with 60 additions and 113 deletions

View file

@ -0,0 +1,15 @@
namespace TanksServer.GameLogic;
internal sealed class BulletManager
{
private readonly HashSet<Bullet> _bullets = new();
public void Spawn(Bullet bullet) => _bullets.Add(bullet);
public IEnumerable<Bullet> GetAll() => _bullets;
public void RemoveWhere(Predicate<Bullet> predicate)
{
_bullets.RemoveWhere(predicate);
}
}

View file

@ -0,0 +1,15 @@
namespace TanksServer.GameLogic;
internal sealed class CollideBulletsWithMap(BulletManager bullets, MapService map) : ITickStep
{
public Task TickAsync()
{
bullets.RemoveWhere(BulletHitsWall);
return Task.CompletedTask;
}
private bool BulletHitsWall(Bullet bullet)
{
return map.IsCurrentlyWall(bullet.Position.ToPixelPosition().ToTilePosition());
}
}

View file

@ -0,0 +1,34 @@
namespace TanksServer.GameLogic;
internal sealed class CollideBulletsWithTanks(
BulletManager bullets, TankManager tanks, SpawnQueueProvider spawnQueueProvider
) : ITickStep
{
public Task TickAsync()
{
bullets.RemoveWhere(BulletHitsTank);
return Task.CompletedTask;
}
private bool BulletHitsTank(Bullet bullet)
{
foreach (var tank in tanks)
{
var (topLeft, bottomRight) = tank.GetBounds();
if (bullet.Position.X < topLeft.X || bullet.Position.X > bottomRight.X ||
bullet.Position.Y < topLeft.Y || bullet.Position.Y > bottomRight.Y)
continue;
if (bullet.Owner != tank.Owner)
bullet.Owner.Kills++;
tank.Owner.Deaths++;
tanks.Remove(tank);
spawnQueueProvider.Queue.Enqueue(tank.Owner);
return true;
}
return false;
}
}

View file

@ -0,0 +1,60 @@
using System.Diagnostics;
using Microsoft.Extensions.Hosting;
namespace TanksServer.GameLogic;
internal sealed class GameTickWorker(
IEnumerable<ITickStep> steps,
IHostApplicationLifetime lifetime,
ILogger<GameTickWorker> logger
) : IHostedService, IDisposable
{
private const int TicksPerSecond = 25;
private static readonly TimeSpan TickPacing = TimeSpan.FromMilliseconds((int)(1000 / TicksPerSecond));
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()
{
try
{
var sw = new Stopwatch();
while (!_cancellation.IsCancellationRequested)
{
logger.LogTrace("since last frame: {}", sw.Elapsed);
sw.Restart();
foreach (var step in _steps)
await step.TickAsync();
var wantedDelay = TickPacing - sw.Elapsed;
if (wantedDelay.Ticks > 0)
await Task.Delay(wantedDelay);
}
}
catch (Exception ex)
{
logger.LogError(ex, "game tick service crashed");
lifetime.StopApplication();
}
}
public async Task StopAsync(CancellationToken cancellationToken)
{
await _cancellation.CancelAsync();
if (_run != null) await _run;
}
public void Dispose()
{
_cancellation.Dispose();
_run?.Dispose();
}
}

View file

@ -0,0 +1,6 @@
namespace TanksServer.GameLogic;
public interface ITickStep
{
Task TickAsync();
}

View file

@ -0,0 +1,42 @@
namespace TanksServer.GameLogic;
internal sealed 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(TilePosition position)
{
return this[position.X, position.Y] == '#';
}
}

View file

@ -0,0 +1,21 @@
namespace TanksServer.GameLogic;
internal sealed class MoveBullets(BulletManager bullets) : ITickStep
{
public Task TickAsync()
{
foreach (var bullet in bullets.GetAll())
MoveBullet(bullet);
return Task.CompletedTask;
}
private static void MoveBullet(Bullet bullet)
{
var angle = bullet.Rotation / 16 * 2 * Math.PI;
bullet.Position = new FloatPosition(
X: bullet.Position.X + Math.Sin(angle) * 3,
Y: bullet.Position.Y - Math.Cos(angle) * 3
);
}
}

View file

@ -0,0 +1,62 @@
namespace TanksServer.GameLogic;
internal sealed class MoveTanks(
TankManager tanks,
IOptions<TanksConfiguration> options,
MapService map
) : ITickStep
{
private readonly TanksConfiguration _config = options.Value;
public Task TickAsync()
{
foreach (var tank in tanks)
tank.Moved = TryMoveTank(tank);
return Task.CompletedTask;
}
private bool TryMoveTank(Tank tank)
{
var player = tank.Owner;
double speed;
switch (player.Controls)
{
case { Forward: false, Backward: false }:
case { Forward: true, Backward: true }:
return false;
case { Forward: true }:
speed = +_config.MoveSpeed;
break;
case { Backward: true }:
speed = -_config.MoveSpeed;
break;
default:
return false;
}
var angle = tank.Rotation / 16d * 2d * Math.PI;
var newX = tank.Position.X + Math.Sin(angle) * speed;
var newY = tank.Position.Y - Math.Cos(angle) * speed;
return TryMoveTankTo(tank, new FloatPosition(newX, newY))
|| TryMoveTankTo(tank, tank.Position with { X = newX })
|| TryMoveTankTo(tank, tank.Position with { Y = newY });
}
private bool TryMoveTankTo(Tank tank, FloatPosition newPosition)
{
var x0 = (int)Math.Floor(newPosition.X / MapService.TileSize);
var x1 = (int)Math.Ceiling(newPosition.X / MapService.TileSize);
var y0 = (int)Math.Floor(newPosition.Y / MapService.TileSize);
var y1 = (int)Math.Ceiling(newPosition.Y / MapService.TileSize);
TilePosition[] positions = { new(x0, y0), new(x0, y1), new(x1, y0), new(x1, y1) };
if (positions.Any(map.IsCurrentlyWall))
return false;
tank.Position = newPosition;
return true;
}
}

View file

@ -0,0 +1,21 @@
namespace TanksServer.GameLogic;
internal sealed class RotateTanks(TankManager tanks, IOptions<TanksConfiguration> options) : ITickStep
{
private readonly TanksConfiguration _config = options.Value;
public Task TickAsync()
{
foreach (var tank in tanks)
{
var player = tank.Owner;
if (player.Controls.TurnLeft)
tank.Rotation -= _config.TurnSpeed;
if (player.Controls.TurnRight)
tank.Rotation += _config.TurnSpeed;
}
return Task.CompletedTask;
}
}

View file

@ -0,0 +1,36 @@
namespace TanksServer.GameLogic;
internal sealed class ShootFromTanks(
TankManager tanks,
IOptions<TanksConfiguration> options,
BulletManager bulletManager
) : ITickStep
{
private readonly TanksConfiguration _config = options.Value;
public Task TickAsync()
{
foreach (var tank in tanks.Where(t => !t.Moved))
Shoot(tank);
return Task.CompletedTask;
}
private void Shoot(Tank tank)
{
if (!tank.Owner.Controls.Shoot)
return;
if (tank.NextShotAfter >= DateTime.Now)
return;
tank.NextShotAfter = DateTime.Now.AddMilliseconds(_config.ShootDelayMs);
var angle = tank.Rotation / 16 * 2 * Math.PI;
var position = new FloatPosition(
X: tank.Position.X + MapService.TileSize / 2d + Math.Sin(angle) * _config.BulletSpeed,
Y: tank.Position.Y + MapService.TileSize / 2d - Math.Cos(angle) * _config.BulletSpeed
);
bulletManager.Spawn(new Bullet(tank.Owner, position, tank.Rotation));
}
}

View file

@ -0,0 +1,41 @@
namespace TanksServer.GameLogic;
internal sealed class SpawnNewTanks(TankManager tanks, MapService map, SpawnQueueProvider queueProvider) : ITickStep
{
public Task TickAsync()
{
while (queueProvider.Queue.TryDequeue(out var player))
{
var tank = new Tank(player, ChooseSpawnPosition())
{
Rotation = Random.Shared.Next(0, 16)
};
tanks.Add(tank);
}
return Task.CompletedTask;
}
private FloatPosition ChooseSpawnPosition()
{
List<TilePosition> candidates = new();
for (var x = 0; x < MapService.TilesPerRow; x++)
for (var y = 0; y < MapService.TilesPerColumn; y++)
{
var tile = new TilePosition(x, y);
if (map.IsCurrentlyWall(tile))
continue;
// TODO: check tanks and bullets
candidates.Add(tile);
}
var chosenTile = candidates[Random.Shared.Next(candidates.Count)];
return new FloatPosition(
chosenTile.X * MapService.TileSize,
chosenTile.Y * MapService.TileSize
);
}
}

View file

@ -0,0 +1,6 @@
namespace TanksServer.GameLogic;
internal sealed class SpawnQueueProvider
{
public ConcurrentQueue<Player> Queue { get; } = new();
}

View file

@ -0,0 +1,23 @@
using System.Collections;
namespace TanksServer.GameLogic;
internal sealed class TankManager(ILogger<TankManager> logger) : IEnumerable<Tank>
{
private readonly ConcurrentDictionary<Tank, byte> _tanks = new();
public void Add(Tank tank)
{
logger.LogInformation("Tank added for player {}", tank.Owner.Id);
_tanks.TryAdd(tank, 0);
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public IEnumerator<Tank> GetEnumerator() => _tanks.Keys.GetEnumerator();
public void Remove(Tank tank)
{
logger.LogInformation("Tank removed for player {}", tank.Owner.Id);
_tanks.Remove(tank, out _);
}
}

View file

@ -4,4 +4,6 @@ global using System.Collections.Generic;
global using System.Linq;
global using System.Threading;
global using System.Threading.Tasks;
global using Microsoft.Extensions.Logging;
global using Microsoft.Extensions.Options;
global using TanksServer.Models;

View 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;
}
}

View 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;
}
}

View file

@ -0,0 +1,8 @@
using TanksServer.ServicePointDisplay;
namespace TanksServer.Graphics;
internal interface IDrawStep
{
void Draw(PixelDisplayBufferView buffer);
}

View 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;
}
}

View 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;
}
}
}
}

View 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];
}
}

Some files were not shown because too many files have changed in this diff Show more