2024-04-10 19:25:45 +02:00
|
|
|
namespace TanksServer.GameLogic;
|
2024-04-07 19:52:16 +02:00
|
|
|
|
|
|
|
internal sealed class ShootFromTanks(
|
2024-04-19 13:37:28 +02:00
|
|
|
IOptions<GameRules> options,
|
2024-04-17 19:34:19 +02:00
|
|
|
MapEntityManager entityManager
|
2024-04-07 19:52:16 +02:00
|
|
|
) : ITickStep
|
|
|
|
{
|
2024-04-19 13:37:28 +02:00
|
|
|
private readonly GameRules _config = options.Value;
|
2024-04-07 19:52:16 +02:00
|
|
|
|
2024-04-28 15:34:32 +02:00
|
|
|
public ValueTask TickAsync(TimeSpan _)
|
2024-04-07 19:52:16 +02:00
|
|
|
{
|
2024-04-22 19:44:28 +02:00
|
|
|
foreach (var tank in entityManager.Tanks.Where(t => !t.Moving))
|
2024-04-07 19:52:16 +02:00
|
|
|
Shoot(tank);
|
|
|
|
|
2024-04-28 15:34:32 +02:00
|
|
|
return ValueTask.CompletedTask;
|
2024-04-07 19:52:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void Shoot(Tank tank)
|
|
|
|
{
|
|
|
|
if (!tank.Owner.Controls.Shoot)
|
|
|
|
return;
|
2024-04-29 17:17:44 +02:00
|
|
|
|
|
|
|
var now = DateTime.Now;
|
|
|
|
if (tank.NextShotAfter >= now)
|
|
|
|
return;
|
|
|
|
if (tank.ReloadingUntil >= now)
|
2024-04-29 16:39:37 +02:00
|
|
|
return;
|
2024-04-29 17:17:44 +02:00
|
|
|
|
|
|
|
if (tank.Magazine.Empty)
|
|
|
|
{
|
|
|
|
tank.ReloadingUntil = now.AddMilliseconds(_config.ReloadDelayMs);
|
|
|
|
tank.Magazine = tank.Magazine with
|
|
|
|
{
|
|
|
|
UsedBullets = 0,
|
|
|
|
Type = MagazineType.Basic
|
|
|
|
};
|
2024-04-07 19:52:16 +02:00
|
|
|
return;
|
2024-04-29 17:17:44 +02:00
|
|
|
}
|
2024-04-07 19:52:16 +02:00
|
|
|
|
2024-04-29 17:17:44 +02:00
|
|
|
tank.NextShotAfter = now.AddMilliseconds(_config.ShootDelayMs);
|
2024-04-29 16:39:37 +02:00
|
|
|
tank.Magazine = tank.Magazine with
|
|
|
|
{
|
|
|
|
UsedBullets = (byte)(tank.Magazine.UsedBullets + 1)
|
|
|
|
};
|
2024-04-07 19:52:16 +02:00
|
|
|
|
2024-04-22 21:26:46 +02:00
|
|
|
tank.Owner.Scores.ShotsFired++;
|
2024-04-29 16:59:37 +02:00
|
|
|
entityManager.SpawnBullet(tank.Owner, tank.Position, tank.Orientation / 16d, tank.Magazine.Type);
|
2024-04-07 19:52:16 +02:00
|
|
|
}
|
2024-04-13 14:07:14 +02:00
|
|
|
}
|