separate tick steps
This commit is contained in:
parent
898a9cedc1
commit
a9aaf899a2
28 changed files with 239 additions and 194 deletions
|
@ -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,28 +0,0 @@
|
|||
using System.Net.Sockets;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace TanksServer.Services;
|
||||
|
||||
internal sealed class ServicePointDisplay(
|
||||
IOptions<ServicePointDisplayConfiguration> options,
|
||||
PixelDrawer drawer
|
||||
) : ITickStep, IDisposable
|
||||
{
|
||||
private readonly UdpClient _udpClient = new(options.Value.Hostname, options.Value.Port);
|
||||
|
||||
public Task TickAsync()
|
||||
{
|
||||
return _udpClient.SendAsync(drawer.LastFrame.Data).AsTask();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_udpClient.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class ServicePointDisplayConfiguration
|
||||
{
|
||||
public string Hostname { get; set; } = string.Empty;
|
||||
public int Port { get; set; }
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
using System.Collections.Concurrent;
|
||||
using TanksServer.Models;
|
||||
|
||||
namespace TanksServer.Services;
|
||||
|
||||
internal sealed class SpawnQueue(TankManager tanks, MapService map) : ITickStep
|
||||
{
|
||||
private readonly ConcurrentQueue<Player> _playersToSpawn = new();
|
||||
|
||||
public Task TickAsync()
|
||||
{
|
||||
while (_playersToSpawn.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
|
||||
);
|
||||
}
|
||||
|
||||
public void SpawnTankForPlayer(Player player)
|
||||
{
|
||||
_playersToSpawn.Enqueue(player);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue