servicepoint-tanks/TanksServer/TickSteps/CollideBulletsWithTanks.cs

38 lines
985 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:16:41 +02:00
internal sealed class CollideBulletsWithTanks(
BulletManager bullets, TankManager tanks, SpawnQueueProvider spawnQueueProvider
) : 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();
2024-04-07 21:16:41 +02:00
if (bullet.Position.X < topLeft.X || bullet.Position.X > bottomRight.X ||
bullet.Position.Y < topLeft.Y || bullet.Position.Y > bottomRight.Y)
2024-04-07 21:09:36 +02:00
continue;
if (bullet.Owner != tank.Owner)
bullet.Owner.Kills++;
tank.Owner.Deaths++;
2024-04-07 21:16:41 +02:00
2024-04-07 21:09:36 +02:00
tanks.Remove(tank);
2024-04-07 21:16:41 +02:00
spawnQueueProvider.Queue.Enqueue(tank.Owner);
2024-04-07 21:09:36 +02:00
return true;
}
return false;
2024-04-07 19:52:16 +02:00
}
}