bullets collide with walls

This commit is contained in:
Vinzenz Schroeter 2024-04-07 19:19:11 +02:00
parent b10ccf2da8
commit 898a9cedc1
6 changed files with 50 additions and 28 deletions

View file

@ -1,21 +1,29 @@
using System.Collections;
using TanksServer.Helpers;
using TanksServer.Models;
namespace TanksServer.Services;
internal sealed class BulletManager : ITickStep
internal sealed class BulletManager(MapService map) : ITickStep
{
private readonly HashSet<Bullet> _bullets = new();
public void Spawn(Bullet bullet) => _bullets.Add(bullet);
public IEnumerable<Bullet> GetAll() => _bullets;
public Task TickAsync()
{
HashSet<Bullet> bulletsToRemove = new();
foreach (var bullet in _bullets)
{
MoveBullet(bullet);
if (BulletHitsWall(bullet))
bulletsToRemove.Add(bullet);
}
_bullets.RemoveWhere(b => bulletsToRemove.Contains(b));
return Task.CompletedTask;
}
@ -28,5 +36,8 @@ internal sealed class BulletManager : ITickStep
);
}
public IEnumerable<Bullet> GetAll() => _bullets;
private bool BulletHitsWall(Bullet bullet)
{
return map.IsCurrentlyWall(bullet.Position.ToPixelPosition().ToTilePosition());
}
}