2024-04-17 19:34:19 +02:00
|
|
|
namespace TanksServer.GameLogic;
|
|
|
|
|
|
|
|
internal sealed class CollectPowerUp(
|
|
|
|
MapEntityManager entityManager
|
|
|
|
) : ITickStep
|
|
|
|
{
|
2024-04-28 15:34:32 +02:00
|
|
|
public ValueTask TickAsync(TimeSpan delta)
|
2024-04-17 19:34:19 +02:00
|
|
|
{
|
|
|
|
entityManager.RemoveWhere(TryCollect);
|
2024-04-28 15:34:32 +02:00
|
|
|
return ValueTask.CompletedTask;
|
2024-04-17 19:34:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool TryCollect(PowerUp obj)
|
|
|
|
{
|
|
|
|
var position = obj.Position;
|
|
|
|
foreach (var tank in entityManager.Tanks)
|
|
|
|
{
|
|
|
|
var (topLeft, bottomRight) = tank.Bounds;
|
|
|
|
if (position.X < topLeft.X || position.X > bottomRight.X ||
|
|
|
|
position.Y < topLeft.Y || position.Y > bottomRight.Y)
|
|
|
|
continue;
|
|
|
|
|
2024-04-29 13:54:29 +02:00
|
|
|
// now the tank overlaps the power up by at least 0.5 tiles
|
2024-04-29 17:17:44 +02:00
|
|
|
|
2024-04-29 16:39:37 +02:00
|
|
|
tank.Magazine = tank.Magazine with
|
|
|
|
{
|
|
|
|
UsedBullets = 0,
|
2024-04-29 17:17:44 +02:00
|
|
|
Type = tank.Magazine.Type | MagazineType.Explosive
|
2024-04-29 16:39:37 +02:00
|
|
|
};
|
2024-04-29 17:17:44 +02:00
|
|
|
|
|
|
|
if (tank.ReloadingUntil >= DateTime.Now)
|
|
|
|
tank.ReloadingUntil = DateTime.Now;
|
|
|
|
|
2024-04-29 13:54:29 +02:00
|
|
|
tank.Owner.Scores.PowerUpsCollected++;
|
2024-04-17 19:34:19 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|