separate tick steps
This commit is contained in:
parent
898a9cedc1
commit
a9aaf899a2
28 changed files with 239 additions and 194 deletions
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)));
|
||||
}
|
||||
}
|
21
TanksServer/TickSteps/SendToServicePointDisplay.cs
Normal file
21
TanksServer/TickSteps/SendToServicePointDisplay.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
using System.Net.Sockets;
|
||||
|
||||
namespace TanksServer.TickSteps;
|
||||
|
||||
internal sealed class SendToServicePointDisplay(
|
||||
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();
|
||||
}
|
||||
}
|
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));
|
||||
}
|
||||
}
|
50
TanksServer/TickSteps/SpawnNewTanks.cs
Normal file
50
TanksServer/TickSteps/SpawnNewTanks.cs
Normal file
|
@ -0,0 +1,50 @@
|
|||
using System.Collections.Concurrent;
|
||||
|
||||
namespace TanksServer.TickSteps;
|
||||
|
||||
internal sealed class SpawnNewTanks(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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue