separate tick steps
This commit is contained in:
parent
898a9cedc1
commit
a9aaf899a2
28 changed files with 238 additions and 193 deletions
|
|
@ -1,5 +1,4 @@
|
||||||
using TanksServer.Helpers;
|
using TanksServer.Helpers;
|
||||||
using TanksServer.Services;
|
|
||||||
|
|
||||||
namespace TanksServer.DrawSteps;
|
namespace TanksServer.DrawSteps;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
using TanksServer.Helpers;
|
using TanksServer.Helpers;
|
||||||
using TanksServer.Models;
|
|
||||||
using TanksServer.Services;
|
|
||||||
|
|
||||||
namespace TanksServer.DrawSteps;
|
namespace TanksServer.DrawSteps;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
using SixLabors.ImageSharp;
|
using SixLabors.ImageSharp;
|
||||||
using SixLabors.ImageSharp.PixelFormats;
|
using SixLabors.ImageSharp.PixelFormats;
|
||||||
using TanksServer.Helpers;
|
using TanksServer.Helpers;
|
||||||
using TanksServer.Services;
|
|
||||||
|
|
||||||
namespace TanksServer.DrawSteps;
|
namespace TanksServer.DrawSteps;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,3 +3,6 @@ global using System.Collections.Generic;
|
||||||
global using System.Linq;
|
global using System.Linq;
|
||||||
global using System.Threading;
|
global using System.Threading;
|
||||||
global using System.Threading.Tasks;
|
global using System.Threading.Tasks;
|
||||||
|
global using Microsoft.Extensions.Options;
|
||||||
|
global using TanksServer.Models;
|
||||||
|
global using TanksServer.Services;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using TanksServer.Models;
|
|
||||||
|
|
||||||
namespace TanksServer.Helpers;
|
namespace TanksServer.Helpers;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,4 @@
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using TanksServer.Models;
|
|
||||||
using TanksServer.Services;
|
|
||||||
|
|
||||||
namespace TanksServer.Helpers;
|
namespace TanksServer.Helpers;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
using TanksServer.Services;
|
|
||||||
|
|
||||||
namespace TanksServer.Models;
|
namespace TanksServer.Models;
|
||||||
|
|
||||||
internal readonly record struct FloatPosition(double X, double Y)
|
internal readonly record struct FloatPosition(double X, double Y)
|
||||||
|
|
|
||||||
7
TanksServer/Models/ServicePointDisplayConfiguration.cs
Normal file
7
TanksServer/Models/ServicePointDisplayConfiguration.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
namespace TanksServer.Services;
|
||||||
|
|
||||||
|
internal sealed class ServicePointDisplayConfiguration
|
||||||
|
{
|
||||||
|
public string Hostname { get; set; } = string.Empty;
|
||||||
|
public int Port { get; set; }
|
||||||
|
}
|
||||||
|
|
@ -18,4 +18,6 @@ internal sealed class Tank(Player player, FloatPosition spawnPosition)
|
||||||
public FloatPosition Position { get; set; } = spawnPosition;
|
public FloatPosition Position { get; set; } = spawnPosition;
|
||||||
|
|
||||||
public DateTime NextShotAfter { get; set; }
|
public DateTime NextShotAfter { get; set; }
|
||||||
|
|
||||||
|
public bool Moved { get; set; }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,8 @@ using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.FileProviders;
|
using Microsoft.Extensions.FileProviders;
|
||||||
using TanksServer.DrawSteps;
|
using TanksServer.DrawSteps;
|
||||||
using TanksServer.Helpers;
|
using TanksServer.Helpers;
|
||||||
using TanksServer.Services;
|
using TanksServer.Servers;
|
||||||
|
using TanksServer.TickSteps;
|
||||||
|
|
||||||
namespace TanksServer;
|
namespace TanksServer;
|
||||||
|
|
||||||
|
|
@ -73,31 +74,29 @@ internal static class Program
|
||||||
options.SerializerOptions.TypeInfoResolverChain.Insert(0, new AppSerializerContext());
|
options.SerializerOptions.TypeInfoResolverChain.Insert(0, new AppSerializerContext());
|
||||||
});
|
});
|
||||||
|
|
||||||
builder.Services.AddSingleton<ServicePointDisplay>();
|
|
||||||
builder.Services.AddSingleton<MapService>();
|
builder.Services.AddSingleton<MapService>();
|
||||||
|
builder.Services.AddSingleton<BulletManager>();
|
||||||
|
builder.Services.AddSingleton<TankManager>();
|
||||||
|
builder.Services.AddSingleton<SpawnNewTanks>();
|
||||||
|
builder.Services.AddSingleton<PixelDrawer>();
|
||||||
|
builder.Services.AddSingleton<ControlsServer>();
|
||||||
|
builder.Services.AddSingleton<PlayerServer>();
|
||||||
|
builder.Services.AddSingleton<ClientScreenServer>();
|
||||||
|
|
||||||
builder.Services.AddHostedService<GameTickService>();
|
builder.Services.AddHostedService<GameTickService>();
|
||||||
|
|
||||||
builder.Services.AddSingleton<BulletManager>();
|
|
||||||
builder.Services.AddSingleton<ITickStep>(sp => sp.GetRequiredService<BulletManager>());
|
|
||||||
|
|
||||||
builder.Services.AddSingleton<TankManager>();
|
|
||||||
builder.Services.AddSingleton<ITickStep>(sp => sp.GetRequiredService<TankManager>());
|
|
||||||
|
|
||||||
builder.Services.AddSingleton<ControlsServer>();
|
|
||||||
builder.Services.AddHostedService(sp => sp.GetRequiredService<ControlsServer>());
|
builder.Services.AddHostedService(sp => sp.GetRequiredService<ControlsServer>());
|
||||||
|
|
||||||
builder.Services.AddSingleton<SpawnQueue>();
|
|
||||||
builder.Services.AddSingleton<ITickStep>(sp => sp.GetRequiredService<SpawnQueue>());
|
|
||||||
|
|
||||||
builder.Services.AddSingleton<PixelDrawer>();
|
|
||||||
builder.Services.AddSingleton<ITickStep>(sp => sp.GetRequiredService<PixelDrawer>());
|
|
||||||
|
|
||||||
builder.Services.AddSingleton<ClientScreenServer>();
|
|
||||||
builder.Services.AddHostedService(sp => sp.GetRequiredService<ClientScreenServer>());
|
builder.Services.AddHostedService(sp => sp.GetRequiredService<ClientScreenServer>());
|
||||||
builder.Services.AddSingleton<ITickStep>(sp => sp.GetRequiredService<ClientScreenServer>());
|
|
||||||
|
|
||||||
builder.Services.AddSingleton<PlayerServer>();
|
builder.Services.AddSingleton<ITickStep, MoveBullets>();
|
||||||
|
builder.Services.AddSingleton<ITickStep, CollideBulletsWithTanks>();
|
||||||
|
builder.Services.AddSingleton<ITickStep, CollideBulletsWithMap>();
|
||||||
|
builder.Services.AddSingleton<ITickStep, RotateTanks>();
|
||||||
|
builder.Services.AddSingleton<ITickStep, MoveTanks>();
|
||||||
|
builder.Services.AddSingleton<ITickStep, ShootFromTanks>();
|
||||||
|
builder.Services.AddSingleton<ITickStep>(sp => sp.GetRequiredService<SpawnNewTanks>());
|
||||||
|
builder.Services.AddSingleton<ITickStep>(sp => sp.GetRequiredService<PixelDrawer>());
|
||||||
|
builder.Services.AddSingleton<ITickStep, SendToServicePointDisplay>();
|
||||||
|
builder.Services.AddSingleton<ITickStep, SendToClientScreen>();
|
||||||
|
|
||||||
builder.Services.AddSingleton<IDrawStep, MapDrawer>();
|
builder.Services.AddSingleton<IDrawStep, MapDrawer>();
|
||||||
builder.Services.AddSingleton<IDrawStep, TankDrawer>();
|
builder.Services.AddSingleton<IDrawStep, TankDrawer>();
|
||||||
|
|
|
||||||
111
TanksServer/Servers/ClientScreenServer.cs
Normal file
111
TanksServer/Servers/ClientScreenServer.cs
Normal file
|
|
@ -0,0 +1,111 @@
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Net.WebSockets;
|
||||||
|
using System.Threading.Channels;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using TanksServer.Helpers;
|
||||||
|
|
||||||
|
namespace TanksServer.Servers;
|
||||||
|
|
||||||
|
internal sealed class ClientScreenServer(
|
||||||
|
ILogger<ClientScreenServer> logger,
|
||||||
|
ILoggerFactory loggerFactory
|
||||||
|
) : IHostedLifecycleService
|
||||||
|
{
|
||||||
|
private readonly ConcurrentDictionary<ClientScreenServerConnection, byte> _connections = new();
|
||||||
|
private bool _closing;
|
||||||
|
|
||||||
|
public Task HandleClient(WebSocket socket)
|
||||||
|
{
|
||||||
|
if (_closing)
|
||||||
|
{
|
||||||
|
logger.LogWarning("ignoring request because connections are closing");
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.LogDebug("HandleClient");
|
||||||
|
var connection =
|
||||||
|
new ClientScreenServerConnection(socket, loggerFactory.CreateLogger<ClientScreenServerConnection>(), this);
|
||||||
|
var added = _connections.TryAdd(connection, 0);
|
||||||
|
Debug.Assert(added);
|
||||||
|
return connection.Done;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task StoppingAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
logger.LogInformation("closing connections");
|
||||||
|
_closing = true;
|
||||||
|
return Task.WhenAll(_connections.Keys.Select(c => c.CloseAsync()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||||
|
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||||
|
public Task StartedAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||||
|
public Task StartingAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||||
|
public Task StoppedAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||||
|
|
||||||
|
private void Remove(ClientScreenServerConnection connection) => _connections.TryRemove(connection, out _);
|
||||||
|
|
||||||
|
public IEnumerable<ClientScreenServerConnection> GetConnections() => _connections.Keys;
|
||||||
|
|
||||||
|
internal sealed class ClientScreenServerConnection: IDisposable
|
||||||
|
{
|
||||||
|
private readonly ByteChannelWebSocket _channel;
|
||||||
|
private readonly SemaphoreSlim _wantedFrames = new(1);
|
||||||
|
private readonly ClientScreenServer _server;
|
||||||
|
private readonly ILogger<ClientScreenServerConnection> _logger;
|
||||||
|
|
||||||
|
public ClientScreenServerConnection(WebSocket webSocket,
|
||||||
|
ILogger<ClientScreenServerConnection> logger,
|
||||||
|
ClientScreenServer server)
|
||||||
|
{
|
||||||
|
_server = server;
|
||||||
|
_logger = logger;
|
||||||
|
_channel = new(webSocket, logger, 0);
|
||||||
|
Done = ReceiveAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SendAsync(DisplayPixelBuffer buf)
|
||||||
|
{
|
||||||
|
if (!await _wantedFrames.WaitAsync(TimeSpan.Zero))
|
||||||
|
{
|
||||||
|
_logger.LogTrace("client does not want a frame yet");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogTrace("sending");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _channel.Writer.WriteAsync(buf.Data);
|
||||||
|
}
|
||||||
|
catch (ChannelClosedException)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("send failed, channel is closed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ReceiveAsync()
|
||||||
|
{
|
||||||
|
await foreach (var _ in _channel.Reader.ReadAllAsync())
|
||||||
|
_wantedFrames.Release();
|
||||||
|
|
||||||
|
_logger.LogTrace("done receiving");
|
||||||
|
_server.Remove(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task CloseAsync()
|
||||||
|
{
|
||||||
|
_logger.LogDebug("closing connection");
|
||||||
|
return _channel.CloseAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task Done { get; }
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_wantedFrames.Dispose();
|
||||||
|
Done.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
115
TanksServer/Servers/ControlsServer.cs
Normal file
115
TanksServer/Servers/ControlsServer.cs
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
using System.Net.WebSockets;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using TanksServer.Helpers;
|
||||||
|
|
||||||
|
namespace TanksServer.Servers;
|
||||||
|
|
||||||
|
internal sealed class ControlsServer(ILogger<ControlsServer> logger, ILoggerFactory loggerFactory)
|
||||||
|
: IHostedLifecycleService
|
||||||
|
{
|
||||||
|
private readonly List<ControlsServerConnection> _connections = new();
|
||||||
|
|
||||||
|
public Task HandleClient(WebSocket ws, Player player)
|
||||||
|
{
|
||||||
|
logger.LogDebug("control client connected {}", player.Id);
|
||||||
|
var clientLogger = loggerFactory.CreateLogger<ControlsServerConnection>();
|
||||||
|
var sock = new ControlsServerConnection(ws, clientLogger, this, player);
|
||||||
|
_connections.Add(sock);
|
||||||
|
return sock.Done;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task StoppingAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return Task.WhenAll(_connections.Select(c => c.CloseAsync()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||||
|
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||||
|
public Task StartedAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||||
|
public Task StartingAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||||
|
public Task StoppedAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||||
|
|
||||||
|
private void Remove(ControlsServerConnection connection)
|
||||||
|
{
|
||||||
|
_connections.Remove(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed class ControlsServerConnection
|
||||||
|
{
|
||||||
|
private readonly ByteChannelWebSocket _binaryWebSocket;
|
||||||
|
private readonly ILogger<ControlsServerConnection> _logger;
|
||||||
|
private readonly ControlsServer _server;
|
||||||
|
private readonly Player _player;
|
||||||
|
|
||||||
|
public ControlsServerConnection(WebSocket socket, ILogger<ControlsServerConnection> logger,
|
||||||
|
ControlsServer server, Player player)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_server = server;
|
||||||
|
_player = player;
|
||||||
|
_binaryWebSocket = new(socket, logger, 2);
|
||||||
|
Done = ReceiveAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task Done { get; }
|
||||||
|
|
||||||
|
private enum MessageType : byte
|
||||||
|
{
|
||||||
|
Enable = 0x01,
|
||||||
|
Disable = 0x02,
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum InputType : byte
|
||||||
|
{
|
||||||
|
Forward = 0x01,
|
||||||
|
Backward = 0x02,
|
||||||
|
Left = 0x03,
|
||||||
|
Right = 0x04,
|
||||||
|
Shoot = 0x05
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ReceiveAsync()
|
||||||
|
{
|
||||||
|
await foreach (var buffer in _binaryWebSocket.Reader.ReadAllAsync())
|
||||||
|
{
|
||||||
|
var type = (MessageType)buffer[0];
|
||||||
|
var control = (InputType)buffer[1];
|
||||||
|
|
||||||
|
_logger.LogTrace("player input {} {} {}", _player.Id, type, control);
|
||||||
|
|
||||||
|
var isEnable = type switch
|
||||||
|
{
|
||||||
|
MessageType.Enable => true,
|
||||||
|
MessageType.Disable => false,
|
||||||
|
_ => throw new ArgumentException("invalid message type")
|
||||||
|
};
|
||||||
|
|
||||||
|
switch (control)
|
||||||
|
{
|
||||||
|
case InputType.Forward:
|
||||||
|
_player.Controls.Forward = isEnable;
|
||||||
|
break;
|
||||||
|
case InputType.Backward:
|
||||||
|
_player.Controls.Backward = isEnable;
|
||||||
|
break;
|
||||||
|
case InputType.Left:
|
||||||
|
_player.Controls.TurnLeft = isEnable;
|
||||||
|
break;
|
||||||
|
case InputType.Right:
|
||||||
|
_player.Controls.TurnRight = isEnable;
|
||||||
|
break;
|
||||||
|
case InputType.Shoot:
|
||||||
|
_player.Controls.Shoot = isEnable;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new ArgumentException("invalid control type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_server.Remove(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task CloseAsync() => _binaryWebSocket.CloseAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
39
TanksServer/Servers/PlayerServer.cs
Normal file
39
TanksServer/Servers/PlayerServer.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using TanksServer.TickSteps;
|
||||||
|
|
||||||
|
namespace TanksServer.Servers;
|
||||||
|
|
||||||
|
internal sealed class PlayerServer(ILogger<PlayerServer> logger, SpawnNewTanks spawnNewTanks)
|
||||||
|
{
|
||||||
|
private readonly ConcurrentDictionary<string, Player> _players = new();
|
||||||
|
|
||||||
|
public Player GetOrAdd(string name)
|
||||||
|
{
|
||||||
|
var player = _players.GetOrAdd(name, AddAndSpawn);
|
||||||
|
logger.LogInformation("player {} (re)joined", player.Id);
|
||||||
|
return player;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGet(Guid? playerId, [MaybeNullWhen(false)] out Player foundPlayer)
|
||||||
|
{
|
||||||
|
foreach (var player in _players.Values)
|
||||||
|
{
|
||||||
|
if (player.Id != playerId)
|
||||||
|
continue;
|
||||||
|
foundPlayer = player;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
foundPlayer = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Player AddAndSpawn(string name)
|
||||||
|
{
|
||||||
|
var player = new Player(name);
|
||||||
|
spawnNewTanks.SpawnTankForPlayer(player);
|
||||||
|
return player;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,10 +1,6 @@
|
||||||
using System.Collections;
|
|
||||||
using TanksServer.Helpers;
|
|
||||||
using TanksServer.Models;
|
|
||||||
|
|
||||||
namespace TanksServer.Services;
|
namespace TanksServer.Services;
|
||||||
|
|
||||||
internal sealed class BulletManager(MapService map) : ITickStep
|
internal sealed class BulletManager
|
||||||
{
|
{
|
||||||
private readonly HashSet<Bullet> _bullets = new();
|
private readonly HashSet<Bullet> _bullets = new();
|
||||||
|
|
||||||
|
|
@ -12,32 +8,8 @@ internal sealed class BulletManager(MapService map) : ITickStep
|
||||||
|
|
||||||
public IEnumerable<Bullet> GetAll() => _bullets;
|
public IEnumerable<Bullet> GetAll() => _bullets;
|
||||||
|
|
||||||
public Task TickAsync()
|
public void RemoveWhere(Predicate<Bullet> predicate)
|
||||||
{
|
{
|
||||||
HashSet<Bullet> bulletsToRemove = new();
|
_bullets.RemoveWhere(predicate);
|
||||||
foreach (var bullet in _bullets)
|
|
||||||
{
|
|
||||||
MoveBullet(bullet);
|
|
||||||
|
|
||||||
if (BulletHitsWall(bullet))
|
|
||||||
bulletsToRemove.Add(bullet);
|
|
||||||
}
|
|
||||||
|
|
||||||
_bullets.RemoveWhere(b => bulletsToRemove.Contains(b));
|
|
||||||
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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool BulletHitsWall(Bullet bullet)
|
|
||||||
{
|
|
||||||
return map.IsCurrentlyWall(bullet.Position.ToPixelPosition().ToTilePosition());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using TanksServer.TickSteps;
|
||||||
|
|
||||||
namespace TanksServer.Services;
|
namespace TanksServer.Services;
|
||||||
|
|
||||||
|
|
@ -36,8 +37,3 @@ internal sealed class GameTickService(IEnumerable<ITickStep> steps) : IHostedSer
|
||||||
_run?.Dispose();
|
_run?.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface ITickStep
|
|
||||||
{
|
|
||||||
Task TickAsync();
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
using TanksServer.Models;
|
|
||||||
|
|
||||||
namespace TanksServer.Services;
|
namespace TanksServer.Services;
|
||||||
|
|
||||||
internal sealed class MapService
|
internal sealed class MapService
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,6 @@
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Net.Mime;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using SixLabors.ImageSharp;
|
|
||||||
using SixLabors.ImageSharp.PixelFormats;
|
|
||||||
using TanksServer.DrawSteps;
|
using TanksServer.DrawSteps;
|
||||||
using TanksServer.Helpers;
|
using TanksServer.Helpers;
|
||||||
using TanksServer.Models;
|
using TanksServer.TickSteps;
|
||||||
|
|
||||||
namespace TanksServer.Services;
|
namespace TanksServer.Services;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,12 @@
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using TanksServer.Models;
|
|
||||||
|
|
||||||
namespace TanksServer.Services;
|
namespace TanksServer.Services;
|
||||||
|
|
||||||
internal sealed class TankManager(
|
internal sealed class TankManager(ILogger<TankManager> logger) : IEnumerable<Tank>
|
||||||
ILogger<TankManager> logger,
|
|
||||||
IOptions<TanksConfiguration> options,
|
|
||||||
MapService map,
|
|
||||||
BulletManager bullets
|
|
||||||
) : ITickStep, IEnumerable<Tank>
|
|
||||||
{
|
{
|
||||||
private readonly ConcurrentBag<Tank> _tanks = new();
|
private readonly ConcurrentBag<Tank> _tanks = new();
|
||||||
private readonly TanksConfiguration _config = options.Value;
|
|
||||||
|
|
||||||
public void Add(Tank tank)
|
public void Add(Tank tank)
|
||||||
{
|
{
|
||||||
|
|
@ -22,86 +14,6 @@ internal sealed class TankManager(
|
||||||
_tanks.Add(tank);
|
_tanks.Add(tank);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task TickAsync()
|
|
||||||
{
|
|
||||||
foreach (var tank in _tanks)
|
|
||||||
{
|
|
||||||
if (TryMoveTank(tank))
|
|
||||||
continue;
|
|
||||||
Shoot(tank);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool TryMoveTank(Tank tank)
|
|
||||||
{
|
|
||||||
logger.LogTrace("moving tank for player {}", tank.Owner.Id);
|
|
||||||
var player = tank.Owner;
|
|
||||||
|
|
||||||
if (player.Controls.TurnLeft)
|
|
||||||
tank.Rotation -= _config.TurnSpeed;
|
|
||||||
if (player.Controls.TurnRight)
|
|
||||||
tank.Rotation += _config.TurnSpeed;
|
|
||||||
|
|
||||||
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 TryMove(tank, new FloatPosition(newX, newY))
|
|
||||||
|| TryMove(tank, tank.Position with { X = newX })
|
|
||||||
|| TryMove(tank, tank.Position with { Y = newY });
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool TryMove(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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
);
|
|
||||||
|
|
||||||
bullets.Spawn(new Bullet(tank.Owner, position, tank.Rotation));
|
|
||||||
}
|
|
||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||||
public IEnumerator<Tank> GetEnumerator() => _tanks.GetEnumerator();
|
public IEnumerator<Tank> GetEnumerator() => _tanks.GetEnumerator();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
17
TanksServer/TickSteps/CollideBulletsWithMap.cs
Normal file
17
TanksServer/TickSteps/CollideBulletsWithMap.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
using TanksServer.Helpers;
|
||||||
|
|
||||||
|
namespace TanksServer.TickSteps;
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
15
TanksServer/TickSteps/CollideBulletsWithTanks.cs
Normal file
15
TanksServer/TickSteps/CollideBulletsWithTanks.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
namespace TanksServer.TickSteps;
|
||||||
|
|
||||||
|
internal sealed class CollideBulletsWithTanks(BulletManager bullets) : ITickStep
|
||||||
|
{
|
||||||
|
public Task TickAsync()
|
||||||
|
{
|
||||||
|
bullets.RemoveWhere(BulletHitsTank);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool BulletHitsTank(Bullet bullet)
|
||||||
|
{
|
||||||
|
return false; // TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Reference in a new issue