allocate predicate once instead of per tick
This commit is contained in:
parent
fe851ffc17
commit
079b096c16
|
@ -6,16 +6,18 @@ internal sealed class CollectPowerUp(
|
||||||
MapEntityManager entityManager
|
MapEntityManager entityManager
|
||||||
) : ITickStep
|
) : ITickStep
|
||||||
{
|
{
|
||||||
|
private readonly Predicate<PowerUp> _collectPredicate = b => TryCollect(b, entityManager.Tanks);
|
||||||
|
|
||||||
public ValueTask TickAsync(TimeSpan delta)
|
public ValueTask TickAsync(TimeSpan delta)
|
||||||
{
|
{
|
||||||
entityManager.RemoveWhere(TryCollect);
|
entityManager.RemoveWhere(_collectPredicate);
|
||||||
return ValueTask.CompletedTask;
|
return ValueTask.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool TryCollect(PowerUp obj)
|
private static bool TryCollect(PowerUp powerUp, IEnumerable<Tank> tanks)
|
||||||
{
|
{
|
||||||
var position = obj.Position;
|
var position = powerUp.Position;
|
||||||
foreach (var tank in entityManager.Tanks)
|
foreach (var tank in tanks)
|
||||||
{
|
{
|
||||||
var (topLeft, bottomRight) = tank.Bounds;
|
var (topLeft, bottomRight) = tank.Bounds;
|
||||||
if (position.X < topLeft.X || position.X > bottomRight.X ||
|
if (position.X < topLeft.X || position.X > bottomRight.X ||
|
||||||
|
@ -24,15 +26,25 @@ internal sealed class CollectPowerUp(
|
||||||
|
|
||||||
// now the tank overlaps the power up by at least 0.5 tiles
|
// now the tank overlaps the power up by at least 0.5 tiles
|
||||||
|
|
||||||
switch (obj.Type)
|
ApplyPowerUpEffect(powerUp, tank);
|
||||||
|
tank.Owner.Scores.PowerUpsCollected++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ApplyPowerUpEffect(PowerUp powerUp, Tank tank)
|
||||||
|
{
|
||||||
|
switch (powerUp.Type)
|
||||||
{
|
{
|
||||||
case PowerUpType.MagazineType:
|
case PowerUpType.MagazineType:
|
||||||
if (obj.MagazineType == null)
|
if (powerUp.MagazineType == null)
|
||||||
throw new UnreachableException();
|
throw new UnreachableException();
|
||||||
|
|
||||||
tank.Magazine = tank.Magazine with
|
tank.Magazine = tank.Magazine with
|
||||||
{
|
{
|
||||||
Type = tank.Magazine.Type | obj.MagazineType.Value,
|
Type = tank.Magazine.Type | powerUp.MagazineType.Value,
|
||||||
UsedBullets = 0
|
UsedBullets = 0
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -49,11 +61,5 @@ internal sealed class CollectPowerUp(
|
||||||
default:
|
default:
|
||||||
throw new UnreachableException();
|
throw new UnreachableException();
|
||||||
}
|
}
|
||||||
|
|
||||||
tank.Owner.Scores.PowerUpsCollected++;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,20 +2,31 @@ using TanksServer.Graphics;
|
||||||
|
|
||||||
namespace TanksServer.GameLogic;
|
namespace TanksServer.GameLogic;
|
||||||
|
|
||||||
internal sealed class CollideBullets(
|
internal sealed class CollideBullets : ITickStep
|
||||||
MapEntityManager entityManager,
|
|
||||||
MapService map,
|
|
||||||
IOptions<GameRules> options,
|
|
||||||
TankSpawnQueue tankSpawnQueue
|
|
||||||
) : ITickStep
|
|
||||||
{
|
{
|
||||||
private readonly Sprite _explosiveSprite = Sprite.FromImageFile("assets/explosion.png");
|
private readonly Sprite _explosiveSprite = Sprite.FromImageFile("assets/explosion.png");
|
||||||
|
private readonly Predicate<Bullet> _removeBulletsPredicate;
|
||||||
|
private readonly MapEntityManager _entityManager;
|
||||||
|
private readonly MapService _map;
|
||||||
|
private readonly bool _destructibleWalls;
|
||||||
|
private readonly TankSpawnQueue _tankSpawnQueue;
|
||||||
|
|
||||||
|
public CollideBullets(MapEntityManager entityManager,
|
||||||
|
MapService map,
|
||||||
|
IOptions<GameRules> options,
|
||||||
|
TankSpawnQueue tankSpawnQueue)
|
||||||
|
{
|
||||||
|
_entityManager = entityManager;
|
||||||
|
_map = map;
|
||||||
|
_tankSpawnQueue = tankSpawnQueue;
|
||||||
|
|
||||||
|
_destructibleWalls = options.Value.DestructibleWalls;
|
||||||
|
_removeBulletsPredicate = b => BulletHitsTank(b) || BulletHitsWall(b) || BulletTimesOut(b);
|
||||||
|
}
|
||||||
|
|
||||||
public ValueTask TickAsync(TimeSpan _)
|
public ValueTask TickAsync(TimeSpan _)
|
||||||
{
|
{
|
||||||
entityManager.RemoveWhere(BulletHitsTank);
|
_entityManager.RemoveWhere(_removeBulletsPredicate);
|
||||||
entityManager.RemoveWhere(BulletHitsWall);
|
|
||||||
entityManager.RemoveWhere(BulletTimesOut);
|
|
||||||
return ValueTask.CompletedTask;
|
return ValueTask.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +42,7 @@ internal sealed class CollideBullets(
|
||||||
private bool BulletHitsWall(Bullet bullet)
|
private bool BulletHitsWall(Bullet bullet)
|
||||||
{
|
{
|
||||||
var pixel = bullet.Position.ToPixelPosition();
|
var pixel = bullet.Position.ToPixelPosition();
|
||||||
if (!map.Current.IsWall(pixel))
|
if (!_map.Current.IsWall(pixel))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
ExplodeAt(pixel, bullet.IsExplosive, bullet.Owner);
|
ExplodeAt(pixel, bullet.IsExplosive, bullet.Owner);
|
||||||
|
@ -50,7 +61,7 @@ internal sealed class CollideBullets(
|
||||||
|
|
||||||
private Tank? GetTankAt(FloatPosition position, Player owner, bool canHitOwnTank)
|
private Tank? GetTankAt(FloatPosition position, Player owner, bool canHitOwnTank)
|
||||||
{
|
{
|
||||||
foreach (var tank in entityManager.Tanks)
|
foreach (var tank in _entityManager.Tanks)
|
||||||
{
|
{
|
||||||
var hitsOwnTank = owner == tank.Owner;
|
var hitsOwnTank = owner == tank.Owner;
|
||||||
if (hitsOwnTank && !canHitOwnTank)
|
if (hitsOwnTank && !canHitOwnTank)
|
||||||
|
@ -89,7 +100,7 @@ internal sealed class CollideBullets(
|
||||||
|
|
||||||
void Core(PixelPosition position)
|
void Core(PixelPosition position)
|
||||||
{
|
{
|
||||||
if (options.Value.DestructibleWalls && map.Current.TryDestroyWallAt(position))
|
if (_destructibleWalls && _map.Current.TryDestroyWallAt(position))
|
||||||
owner.Scores.WallsDestroyed++;
|
owner.Scores.WallsDestroyed++;
|
||||||
|
|
||||||
var tank = GetTankAt(position.ToFloatPosition(), owner, true);
|
var tank = GetTankAt(position.ToFloatPosition(), owner, true);
|
||||||
|
@ -100,8 +111,8 @@ internal sealed class CollideBullets(
|
||||||
owner.Scores.Kills++;
|
owner.Scores.Kills++;
|
||||||
tank.Owner.Scores.Deaths++;
|
tank.Owner.Scores.Deaths++;
|
||||||
|
|
||||||
entityManager.Remove(tank);
|
_entityManager.Remove(tank);
|
||||||
tankSpawnQueue.EnqueueForDelayedSpawn(tank.Owner);
|
_tankSpawnQueue.EnqueueForDelayedSpawn(tank.Owner);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue