servicepoint-tanks/tanks-backend/TanksServer/GameLogic/ShootFromTanks.cs

43 lines
1.1 KiB
C#
Raw Permalink Normal View History

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)
return;
2024-04-29 17:17:44 +02:00
if (tank.UsedBullets >= tank.MaxBullets)
2024-04-29 17:17:44 +02:00
{
tank.ReloadingUntil = now.AddMilliseconds(_config.ReloadDelayMs);
tank.UsedBullets = 0;
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);
tank.UsedBullets++;
2024-04-07 19:52:16 +02:00
2024-04-22 21:26:46 +02:00
tank.Owner.Scores.ShotsFired++;
entityManager.SpawnBullet(tank.Owner, tank.Position, tank.Orientation / 16d, tank.BulletStats);
2024-04-07 19:52:16 +02:00
}
}