separate folders per functionality
This commit is contained in:
parent
7f00160780
commit
0ca6a91a7e
33 changed files with 60 additions and 113 deletions
15
TanksServer/GameLogic/BulletManager.cs
Normal file
15
TanksServer/GameLogic/BulletManager.cs
Normal 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);
|
||||
}
|
||||
}
|
15
TanksServer/GameLogic/CollideBulletsWithMap.cs
Normal file
15
TanksServer/GameLogic/CollideBulletsWithMap.cs
Normal 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());
|
||||
}
|
||||
}
|
34
TanksServer/GameLogic/CollideBulletsWithTanks.cs
Normal file
34
TanksServer/GameLogic/CollideBulletsWithTanks.cs
Normal 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;
|
||||
}
|
||||
}
|
60
TanksServer/GameLogic/GameTickWorker.cs
Normal file
60
TanksServer/GameLogic/GameTickWorker.cs
Normal 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();
|
||||
}
|
||||
}
|
6
TanksServer/GameLogic/ITickStep.cs
Normal file
6
TanksServer/GameLogic/ITickStep.cs
Normal file
|
@ -0,0 +1,6 @@
|
|||
namespace TanksServer.GameLogic;
|
||||
|
||||
public interface ITickStep
|
||||
{
|
||||
Task TickAsync();
|
||||
}
|
42
TanksServer/GameLogic/MapService.cs
Normal file
42
TanksServer/GameLogic/MapService.cs
Normal 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] == '#';
|
||||
}
|
||||
}
|
21
TanksServer/GameLogic/MoveBullets.cs
Normal file
21
TanksServer/GameLogic/MoveBullets.cs
Normal 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
|
||||
);
|
||||
}
|
||||
}
|
62
TanksServer/GameLogic/MoveTanks.cs
Normal file
62
TanksServer/GameLogic/MoveTanks.cs
Normal 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;
|
||||
}
|
||||
}
|
21
TanksServer/GameLogic/RotateTanks.cs
Normal file
21
TanksServer/GameLogic/RotateTanks.cs
Normal 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;
|
||||
}
|
||||
}
|
36
TanksServer/GameLogic/ShootFromTanks.cs
Normal file
36
TanksServer/GameLogic/ShootFromTanks.cs
Normal 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));
|
||||
}
|
||||
}
|
41
TanksServer/GameLogic/SpawnNewTanks.cs
Normal file
41
TanksServer/GameLogic/SpawnNewTanks.cs
Normal 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
|
||||
);
|
||||
}
|
||||
}
|
6
TanksServer/GameLogic/SpawnQueueProvider.cs
Normal file
6
TanksServer/GameLogic/SpawnQueueProvider.cs
Normal file
|
@ -0,0 +1,6 @@
|
|||
namespace TanksServer.GameLogic;
|
||||
|
||||
internal sealed class SpawnQueueProvider
|
||||
{
|
||||
public ConcurrentQueue<Player> Queue { get; } = new();
|
||||
}
|
23
TanksServer/GameLogic/TankManager.cs
Normal file
23
TanksServer/GameLogic/TankManager.cs
Normal 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 _);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue