2024-04-17 19:34:19 +02:00
|
|
|
namespace TanksServer.GameLogic;
|
|
|
|
|
2024-05-08 00:29:33 +02:00
|
|
|
internal sealed class CollectPowerUp : ITickStep
|
2024-04-17 19:34:19 +02:00
|
|
|
{
|
2024-05-08 00:29:33 +02:00
|
|
|
private readonly Predicate<PowerUp> _collectPredicate;
|
|
|
|
private readonly GameRules _rules;
|
|
|
|
private readonly MapEntityManager _entityManager;
|
|
|
|
|
|
|
|
public CollectPowerUp(MapEntityManager entityManager,
|
|
|
|
IOptions<GameRules> options)
|
|
|
|
{
|
|
|
|
_entityManager = entityManager;
|
|
|
|
_rules = options.Value;
|
|
|
|
_collectPredicate = b => TryCollect(b, entityManager.Tanks);
|
|
|
|
}
|
2024-05-05 13:01:37 +02:00
|
|
|
|
2024-04-28 15:34:32 +02:00
|
|
|
public ValueTask TickAsync(TimeSpan delta)
|
2024-04-17 19:34:19 +02:00
|
|
|
{
|
2024-05-08 00:29:33 +02:00
|
|
|
_entityManager.RemoveWhere(_collectPredicate);
|
2024-04-28 15:34:32 +02:00
|
|
|
return ValueTask.CompletedTask;
|
2024-04-17 19:34:19 +02:00
|
|
|
}
|
|
|
|
|
2024-05-08 00:29:33 +02:00
|
|
|
private bool TryCollect(PowerUp powerUp, IEnumerable<Tank> tanks)
|
2024-04-17 19:34:19 +02:00
|
|
|
{
|
2024-05-05 13:01:37 +02:00
|
|
|
var position = powerUp.Position;
|
|
|
|
foreach (var tank in tanks)
|
2024-04-17 19:34:19 +02:00
|
|
|
{
|
|
|
|
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-05-05 13:01:37 +02:00
|
|
|
ApplyPowerUpEffect(powerUp, tank);
|
2024-04-29 13:54:29 +02:00
|
|
|
tank.Owner.Scores.PowerUpsCollected++;
|
2024-04-17 19:34:19 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2024-05-05 13:01:37 +02:00
|
|
|
|
2024-05-08 00:29:33 +02:00
|
|
|
private void ApplyPowerUpEffect(PowerUp powerUp, Tank tank)
|
2024-05-05 13:01:37 +02:00
|
|
|
{
|
|
|
|
switch (powerUp.Type)
|
|
|
|
{
|
2024-05-08 00:29:33 +02:00
|
|
|
case PowerUpType.MagazineSize:
|
2024-05-08 01:01:11 +02:00
|
|
|
tank.MaxBullets = int.Clamp(tank.MaxBullets + 1, 1, 32);
|
2024-05-08 00:29:33 +02:00
|
|
|
break;
|
2024-05-05 13:01:37 +02:00
|
|
|
|
2024-05-08 00:29:33 +02:00
|
|
|
case PowerUpType.BulletAcceleration:
|
|
|
|
tank.BulletStats = tank.BulletStats with
|
2024-05-05 13:01:37 +02:00
|
|
|
{
|
2024-05-08 01:01:11 +02:00
|
|
|
Acceleration = tank.BulletStats.Acceleration + _rules.BulletAccelerationUpgradeStrength
|
2024-05-05 13:01:37 +02:00
|
|
|
};
|
2024-05-08 00:29:33 +02:00
|
|
|
break;
|
2024-05-05 13:01:37 +02:00
|
|
|
|
2024-05-08 00:29:33 +02:00
|
|
|
case PowerUpType.ExplosiveBullets:
|
|
|
|
tank.BulletStats = tank.BulletStats with { Explosive = true };
|
|
|
|
break;
|
2024-05-05 13:01:37 +02:00
|
|
|
|
2024-05-08 00:29:33 +02:00
|
|
|
case PowerUpType.SmartBullets:
|
|
|
|
tank.BulletStats = tank.BulletStats with { Smart = true };
|
2024-05-05 13:01:37 +02:00
|
|
|
break;
|
2024-05-08 00:29:33 +02:00
|
|
|
|
|
|
|
case PowerUpType.BulletSpeed:
|
|
|
|
tank.BulletStats = tank.BulletStats with
|
2024-05-05 13:01:37 +02:00
|
|
|
{
|
2024-05-08 01:01:11 +02:00
|
|
|
Speed = tank.BulletStats.Speed + _rules.BulletSpeedUpgradeStrength
|
2024-05-05 13:01:37 +02:00
|
|
|
};
|
|
|
|
break;
|
2024-05-08 00:29:33 +02:00
|
|
|
|
2024-05-05 13:01:37 +02:00
|
|
|
default:
|
2024-05-08 00:29:33 +02:00
|
|
|
throw new NotImplementedException($"unknown type {powerUp.Type}");
|
2024-05-05 13:01:37 +02:00
|
|
|
}
|
|
|
|
}
|
2024-04-17 19:34:19 +02:00
|
|
|
}
|