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

36 lines
892 B
C#
Raw Normal View History

2024-04-16 20:24:29 +02:00
using System.Diagnostics;
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-16 19:40:08 +02:00
public Task 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);
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);
2024-04-21 20:20:30 +02:00
var explosive = tank.ExplosiveBullets > 0;
if (explosive)
2024-04-17 19:34:19 +02:00
tank.ExplosiveBullets--;
2024-04-21 20:20:30 +02:00
entityManager.SpawnBullet(tank.Owner, tank.Position, tank.Orientation / 16d, explosive);
2024-04-07 19:52:16 +02:00
}
}