servicepoint-tanks/TanksServer/TickSteps/CollideBulletsWithTanks.cs

34 lines
897 B
C#
Raw Normal View History

2024-04-07 20:16:22 +02:00
using TanksServer.Models;
using TanksServer.Services;
2024-04-07 19:52:16 +02:00
namespace TanksServer.TickSteps;
2024-04-07 21:09:36 +02:00
internal sealed class CollideBulletsWithTanks(BulletManager bullets, TankManager tanks) : ITickStep
2024-04-07 19:52:16 +02:00
{
public Task TickAsync()
{
bullets.RemoveWhere(BulletHitsTank);
return Task.CompletedTask;
}
private bool BulletHitsTank(Bullet bullet)
{
2024-04-07 21:09:36 +02:00
foreach (var tank in tanks)
{
var (topLeft, bottomRight) = tank.GetBounds();
if (bullet.Position.X <= topLeft.X || bullet.Position.X >= bottomRight.X ||
bullet.Position.Y <= topLeft.Y || bullet.Position.Y >= bottomRight.Y)
continue;
if (bullet.Owner != tank.Owner)
bullet.Owner.Kills++;
tank.Owner.Deaths++;
tanks.Remove(tank);
return true;
}
return false;
2024-04-07 19:52:16 +02:00
}
}