separate tick steps
This commit is contained in:
parent
898a9cedc1
commit
a9aaf899a2
|
@ -1,5 +1,4 @@
|
|||
using TanksServer.Helpers;
|
||||
using TanksServer.Services;
|
||||
|
||||
namespace TanksServer.DrawSteps;
|
||||
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
using TanksServer.Helpers;
|
||||
using TanksServer.Models;
|
||||
using TanksServer.Services;
|
||||
|
||||
namespace TanksServer.DrawSteps;
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using TanksServer.Helpers;
|
||||
using TanksServer.Services;
|
||||
|
||||
namespace TanksServer.DrawSteps;
|
||||
|
||||
|
|
|
@ -3,3 +3,6 @@ global using System.Collections.Generic;
|
|||
global using System.Linq;
|
||||
global using System.Threading;
|
||||
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 TanksServer.Models;
|
||||
|
||||
namespace TanksServer.Helpers;
|
||||
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using TanksServer.Models;
|
||||
using TanksServer.Services;
|
||||
|
||||
namespace TanksServer.Helpers;
|
||||
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
using TanksServer.Services;
|
||||
|
||||
namespace TanksServer.Models;
|
||||
|
||||
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 DateTime NextShotAfter { get; set; }
|
||||
|
||||
public bool Moved { get; set; }
|
||||
}
|
||||
|
|
|
@ -6,7 +6,8 @@ using Microsoft.Extensions.DependencyInjection;
|
|||
using Microsoft.Extensions.FileProviders;
|
||||
using TanksServer.DrawSteps;
|
||||
using TanksServer.Helpers;
|
||||
using TanksServer.Services;
|
||||
using TanksServer.Servers;
|
||||
using TanksServer.TickSteps;
|
||||
|
||||
namespace TanksServer;
|
||||
|
||||
|
@ -73,31 +74,29 @@ internal static class Program
|
|||
options.SerializerOptions.TypeInfoResolverChain.Insert(0, new AppSerializerContext());
|
||||
});
|
||||
|
||||
builder.Services.AddSingleton<ServicePointDisplay>();
|
||||
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.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.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.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, TankDrawer>();
|
||||
|
|
|
@ -5,15 +5,13 @@ using System.Threading.Channels;
|
|||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TanksServer.Helpers;
|
||||
using TanksServer.Services;
|
||||
|
||||
namespace TanksServer;
|
||||
namespace TanksServer.Servers;
|
||||
|
||||
internal sealed class ClientScreenServer(
|
||||
ILogger<ClientScreenServer> logger,
|
||||
ILoggerFactory loggerFactory,
|
||||
PixelDrawer drawer
|
||||
) : IHostedLifecycleService, ITickStep
|
||||
ILoggerFactory loggerFactory
|
||||
) : IHostedLifecycleService
|
||||
{
|
||||
private readonly ConcurrentDictionary<ClientScreenServerConnection, byte> _connections = new();
|
||||
private bool _closing;
|
||||
|
@ -41,12 +39,6 @@ internal sealed class ClientScreenServer(
|
|||
return Task.WhenAll(_connections.Keys.Select(c => c.CloseAsync()));
|
||||
}
|
||||
|
||||
public Task TickAsync()
|
||||
{
|
||||
logger.LogTrace("Sending buffer to {} clients", _connections.Count);
|
||||
return Task.WhenAll(_connections.Keys.Select(c => c.SendAsync(drawer.LastFrame)));
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
public Task StartedAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
|
@ -54,8 +46,10 @@ internal sealed class ClientScreenServer(
|
|||
public Task StoppedAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
|
||||
private void Remove(ClientScreenServerConnection connection) => _connections.TryRemove(connection, out _);
|
||||
|
||||
public IEnumerable<ClientScreenServerConnection> GetConnections() => _connections.Keys;
|
||||
|
||||
private sealed class ClientScreenServerConnection: IDisposable
|
||||
internal sealed class ClientScreenServerConnection: IDisposable
|
||||
{
|
||||
private readonly ByteChannelWebSocket _channel;
|
||||
private readonly SemaphoreSlim _wantedFrames = new(1);
|
|
@ -2,9 +2,8 @@ using System.Net.WebSockets;
|
|||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TanksServer.Helpers;
|
||||
using TanksServer.Models;
|
||||
|
||||
namespace TanksServer;
|
||||
namespace TanksServer.Servers;
|
||||
|
||||
internal sealed class ControlsServer(ILogger<ControlsServer> logger, ILoggerFactory loggerFactory)
|
||||
: IHostedLifecycleService
|
|
@ -1,12 +1,11 @@
|
|||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TanksServer.Models;
|
||||
using TanksServer.Services;
|
||||
using TanksServer.TickSteps;
|
||||
|
||||
namespace TanksServer;
|
||||
namespace TanksServer.Servers;
|
||||
|
||||
internal sealed class PlayerServer(ILogger<PlayerServer> logger, SpawnQueue spawnQueue)
|
||||
internal sealed class PlayerServer(ILogger<PlayerServer> logger, SpawnNewTanks spawnNewTanks)
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, Player> _players = new();
|
||||
|
||||
|
@ -34,7 +33,7 @@ internal sealed class PlayerServer(ILogger<PlayerServer> logger, SpawnQueue spaw
|
|||
private Player AddAndSpawn(string name)
|
||||
{
|
||||
var player = new Player(name);
|
||||
spawnQueue.SpawnTankForPlayer(player);
|
||||
spawnNewTanks.SpawnTankForPlayer(player);
|
||||
return player;
|
||||
}
|
||||
}
|
|
@ -1,10 +1,6 @@
|
|||
using System.Collections;
|
||||
using TanksServer.Helpers;
|
||||
using TanksServer.Models;
|
||||
|
||||
namespace TanksServer.Services;
|
||||
|
||||
internal sealed class BulletManager(MapService map) : ITickStep
|
||||
internal sealed class BulletManager
|
||||
{
|
||||
private readonly HashSet<Bullet> _bullets = new();
|
||||
|
||||
|
@ -12,32 +8,8 @@ internal sealed class BulletManager(MapService map) : ITickStep
|
|||
|
||||
public IEnumerable<Bullet> GetAll() => _bullets;
|
||||
|
||||
public Task TickAsync()
|
||||
public void RemoveWhere(Predicate<Bullet> predicate)
|
||||
{
|
||||
HashSet<Bullet> bulletsToRemove = new();
|
||||
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());
|
||||
_bullets.RemoveWhere(predicate);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using Microsoft.Extensions.Hosting;
|
||||
using TanksServer.TickSteps;
|
||||
|
||||
namespace TanksServer.Services;
|
||||
|
||||
|
@ -36,8 +37,3 @@ internal sealed class GameTickService(IEnumerable<ITickStep> steps) : IHostedSer
|
|||
_run?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public interface ITickStep
|
||||
{
|
||||
Task TickAsync();
|
||||
}
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
using TanksServer.Models;
|
||||
|
||||
namespace TanksServer.Services;
|
||||
|
||||
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.Helpers;
|
||||
using TanksServer.Models;
|
||||
using TanksServer.TickSteps;
|
||||
|
||||
namespace TanksServer.Services;
|
||||
|
||||
|
|
|
@ -1,20 +1,12 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using TanksServer.Models;
|
||||
|
||||
namespace TanksServer.Services;
|
||||
|
||||
internal sealed class TankManager(
|
||||
ILogger<TankManager> logger,
|
||||
IOptions<TanksConfiguration> options,
|
||||
MapService map,
|
||||
BulletManager bullets
|
||||
) : ITickStep, IEnumerable<Tank>
|
||||
internal sealed class TankManager(ILogger<TankManager> logger) : IEnumerable<Tank>
|
||||
{
|
||||
private readonly ConcurrentBag<Tank> _tanks = new();
|
||||
private readonly TanksConfiguration _config = options.Value;
|
||||
|
||||
public void Add(Tank tank)
|
||||
{
|
||||
|
@ -22,86 +14,6 @@ internal sealed class TankManager(
|
|||
_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();
|
||||
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
|
||||
}
|
||||
}
|
6
TanksServer/TickSteps/ITickStep.cs
Normal file
6
TanksServer/TickSteps/ITickStep.cs
Normal file
|
@ -0,0 +1,6 @@
|
|||
namespace TanksServer.TickSteps;
|
||||
|
||||
public interface ITickStep
|
||||
{
|
||||
Task TickAsync();
|
||||
}
|
21
TanksServer/TickSteps/MoveBullets.cs
Normal file
21
TanksServer/TickSteps/MoveBullets.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
namespace TanksServer.TickSteps;
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
60
TanksServer/TickSteps/MoveTanks.cs
Normal file
60
TanksServer/TickSteps/MoveTanks.cs
Normal file
|
@ -0,0 +1,60 @@
|
|||
namespace TanksServer.TickSteps;
|
||||
|
||||
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/TickSteps/RotateTanks.cs
Normal file
21
TanksServer/TickSteps/RotateTanks.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
namespace TanksServer.TickSteps;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
13
TanksServer/TickSteps/SendToClientScreen.cs
Normal file
13
TanksServer/TickSteps/SendToClientScreen.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using TanksServer.Servers;
|
||||
|
||||
namespace TanksServer.TickSteps;
|
||||
|
||||
internal sealed class SendToClientScreen(
|
||||
ClientScreenServer clientScreenServer, PixelDrawer drawer
|
||||
) : ITickStep
|
||||
{
|
||||
public Task TickAsync()
|
||||
{
|
||||
return Task.WhenAll(clientScreenServer.GetConnections().Select(c => c.SendAsync(drawer.LastFrame)));
|
||||
}
|
||||
}
|
|
@ -1,9 +1,8 @@
|
|||
using System.Net.Sockets;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace TanksServer.Services;
|
||||
namespace TanksServer.TickSteps;
|
||||
|
||||
internal sealed class ServicePointDisplay(
|
||||
internal sealed class SendToServicePointDisplay(
|
||||
IOptions<ServicePointDisplayConfiguration> options,
|
||||
PixelDrawer drawer
|
||||
) : ITickStep, IDisposable
|
||||
|
@ -20,9 +19,3 @@ internal sealed class ServicePointDisplay(
|
|||
_udpClient.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class ServicePointDisplayConfiguration
|
||||
{
|
||||
public string Hostname { get; set; } = string.Empty;
|
||||
public int Port { get; set; }
|
||||
}
|
34
TanksServer/TickSteps/ShootFromTanks.cs
Normal file
34
TanksServer/TickSteps/ShootFromTanks.cs
Normal file
|
@ -0,0 +1,34 @@
|
|||
namespace TanksServer.TickSteps;
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
|
@ -1,9 +1,8 @@
|
|||
using System.Collections.Concurrent;
|
||||
using TanksServer.Models;
|
||||
|
||||
namespace TanksServer.Services;
|
||||
namespace TanksServer.TickSteps;
|
||||
|
||||
internal sealed class SpawnQueue(TankManager tanks, MapService map) : ITickStep
|
||||
internal sealed class SpawnNewTanks(TankManager tanks, MapService map) : ITickStep
|
||||
{
|
||||
private readonly ConcurrentQueue<Player> _playersToSpawn = new();
|
||||
|
Loading…
Reference in a new issue