implement smart bullet

This commit is contained in:
Vinzenz Schroeter 2024-04-29 21:52:50 +02:00
parent 21f7d1d5f4
commit 0e01ff0fb9
9 changed files with 54 additions and 21 deletions

View file

@ -47,7 +47,7 @@ internal sealed class CollectPowerUp(
};
break;
default:
throw new NotImplementedException();
throw new UnreachableException();
}
tank.Owner.Scores.PowerUpsCollected++;

View file

@ -25,4 +25,6 @@ internal sealed class GameRules
public byte MagazineSize { get; set; } = 5;
public int ReloadDelayMs { get; set; } = 3000;
public double SmartBulletInertia { get; set; } = 1;
}

View file

@ -27,7 +27,8 @@ internal sealed class MapEntityManager(
IsExplosive = type.HasFlag(MagazineType.Explosive),
Timeout = DateTime.Now + _bulletTimeout,
OwnerCollisionAfter = DateTime.Now + TimeSpan.FromSeconds(1),
Speed = speed
Speed = speed,
IsSmart = type.HasFlag(MagazineType.Smart)
});
}

View file

@ -1,7 +1,12 @@
namespace TanksServer.GameLogic;
internal sealed class MoveBullets(MapEntityManager entityManager) : ITickStep
internal sealed class MoveBullets(
MapEntityManager entityManager,
IOptions<GameRules> options
) : ITickStep
{
private readonly double _smartBulletInertia = options.Value.SmartBulletInertia;
public ValueTask TickAsync(TimeSpan delta)
{
foreach (var bullet in entityManager.Bullets)
@ -10,8 +15,15 @@ internal sealed class MoveBullets(MapEntityManager entityManager) : ITickStep
return ValueTask.CompletedTask;
}
private static void MoveBullet(Bullet bullet, TimeSpan delta)
private void MoveBullet(Bullet bullet, TimeSpan delta)
{
if (bullet.IsSmart && TryGetSmartRotation(bullet.Position, bullet.Owner, out var wantedRotation))
{
var inertiaFactor = _smartBulletInertia * delta.TotalSeconds;
var difference = wantedRotation - bullet.Rotation;
bullet.Rotation += difference * inertiaFactor;
}
var speed = bullet.Speed * delta.TotalSeconds;
var angle = bullet.Rotation * 2 * Math.PI;
bullet.Position = new FloatPosition(
@ -19,4 +31,24 @@ internal sealed class MoveBullets(MapEntityManager entityManager) : ITickStep
bullet.Position.Y - Math.Cos(angle) * speed
);
}
private bool TryGetSmartRotation(FloatPosition position, Player bulletOwner, out double rotation)
{
var nearestEnemy = entityManager.Tanks
.Where(t => t.Owner != bulletOwner)
.MinBy(t => position.Distance(t.Position));
if (nearestEnemy == null)
{
rotation = double.NaN;
return false;
}
var rotationRadians = Math.Atan2(
y: nearestEnemy.Position.Y - position.Y,
x: nearestEnemy.Position.X - position.X
) + (Math.PI / 2);
rotation = rotationRadians / (2 * Math.PI);
return true;
}
}

View file

@ -18,18 +18,17 @@ internal sealed class SpawnPowerUp(
return ValueTask.CompletedTask;
var type = Random.Shared.Next(10) < 3
var type = Random.Shared.Next(4) == 0
? PowerUpType.MagazineSizeUpgrade
: PowerUpType.MagazineTypeUpgrade;
MagazineType? magazineType = type switch
{
PowerUpType.MagazineTypeUpgrade => Random.Shared.Next(0, 4) switch
PowerUpType.MagazineTypeUpgrade => Random.Shared.Next(0, 3) switch
{
0 => MagazineType.Fast,
1 => MagazineType.Explosive,
2 => MagazineType.Smart,
3 => MagazineType.Mine,
_ => throw new UnreachableException()
},
_ => null