separate tick steps

This commit is contained in:
Vinzenz Schroeter 2024-04-07 19:52:16 +02:00
commit a9aaf899a2

View file

@ -1,10 +1,6 @@
using System.Collections;
using TanksServer.Helpers;
using TanksServer.Models;
namespace TanksServer.Services;
internal sealed class BulletManager(MapService map) : ITickStep
internal sealed class BulletManager
{
private readonly HashSet<Bullet> _bullets = new();
@ -12,32 +8,8 @@ internal sealed class BulletManager(MapService map) : ITickStep
public IEnumerable<Bullet> GetAll() => _bullets;
public Task TickAsync()
public void RemoveWhere(Predicate<Bullet> predicate)
{
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;
}
private static void MoveBullet(Bullet bullet)
{
var angle = bullet.Rotation / 16 * 2 * Math.PI;
bullet.Position = new FloatPosition(
X: bullet.Position.X + Math.Sin(angle) * 3,
Y: bullet.Position.Y - Math.Cos(angle) * 3
);
}
private bool BulletHitsWall(Bullet bullet)
{
return map.IsCurrentlyWall(bullet.Position.ToPixelPosition().ToTilePosition());
_bullets.RemoveWhere(predicate);
}
}