From 0aac6f780b04675eaae64e31569ffd49d449ff6c Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 19 Apr 2024 13:32:41 +0200 Subject: [PATCH] add bullet timeout --- TanksServer/GameLogic/CollideBullets.cs | 11 +++++++++++ TanksServer/GameLogic/GameRules.cs | 2 ++ TanksServer/GameLogic/MapEntityManager.cs | 6 ++++-- TanksServer/Models/Bullet.cs | 4 +++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/TanksServer/GameLogic/CollideBullets.cs b/TanksServer/GameLogic/CollideBullets.cs index 59fc297..1504506 100644 --- a/TanksServer/GameLogic/CollideBullets.cs +++ b/TanksServer/GameLogic/CollideBullets.cs @@ -13,9 +13,20 @@ internal sealed class CollideBullets( { entityManager.RemoveBulletsWhere(BulletHitsTank); entityManager.RemoveBulletsWhere(TryHitAndDestroyWall); + entityManager.RemoveBulletsWhere(TimeoutBullet); return Task.CompletedTask; } + private bool TimeoutBullet(Bullet bullet) + { + if (bullet.Timeout > DateTime.Now) + return false; + + var radius = bullet.IsExplosive ? ExplosionRadius : 0; + ExplodeAt(bullet.Position.ToPixelPosition(), radius, bullet.Owner); + return true; + } + private bool TryHitAndDestroyWall(Bullet bullet) { var pixel = bullet.Position.ToPixelPosition(); diff --git a/TanksServer/GameLogic/GameRules.cs b/TanksServer/GameLogic/GameRules.cs index 287ea50..77de620 100644 --- a/TanksServer/GameLogic/GameRules.cs +++ b/TanksServer/GameLogic/GameRules.cs @@ -7,4 +7,6 @@ internal sealed class GameRules public double PowerUpSpawnChance { get; set; } public int MaxPowerUpCount { get; set; } = int.MaxValue; + + public int BulletTimeoutMs { get; set; } = int.MaxValue; } diff --git a/TanksServer/GameLogic/MapEntityManager.cs b/TanksServer/GameLogic/MapEntityManager.cs index bfb976a..5c2cbfc 100644 --- a/TanksServer/GameLogic/MapEntityManager.cs +++ b/TanksServer/GameLogic/MapEntityManager.cs @@ -2,12 +2,14 @@ namespace TanksServer.GameLogic; internal sealed class MapEntityManager( ILogger logger, - MapService map + MapService map, + IOptions options ) { private readonly HashSet _bullets = []; private readonly HashSet _tanks = []; private readonly HashSet _powerUps = []; + private readonly TimeSpan _bulletTimeout = TimeSpan.FromMilliseconds(options.Value.BulletTimeoutMs); public IEnumerable Bullets => _bullets; public IEnumerable Tanks => _tanks; @@ -19,7 +21,7 @@ internal sealed class MapEntityManager( .Concat(PowerUps); public void SpawnBullet(Player tankOwner, FloatPosition position, double rotation, bool isExplosive) - => _bullets.Add(new Bullet(tankOwner, position, rotation, isExplosive)); + => _bullets.Add(new Bullet(tankOwner, position, rotation, isExplosive, DateTime.Now + _bulletTimeout)); public void RemoveBulletsWhere(Predicate predicate) => _bullets.RemoveWhere(predicate); diff --git a/TanksServer/Models/Bullet.cs b/TanksServer/Models/Bullet.cs index 11910ab..2558c7b 100644 --- a/TanksServer/Models/Bullet.cs +++ b/TanksServer/Models/Bullet.cs @@ -1,6 +1,6 @@ namespace TanksServer.Models; -internal sealed class Bullet(Player tankOwner, FloatPosition position, double rotation, bool isExplosive) : IMapEntity +internal sealed class Bullet(Player tankOwner, FloatPosition position, double rotation, bool isExplosive, DateTime timeout) : IMapEntity { public Player Owner { get; } = tankOwner; @@ -10,5 +10,7 @@ internal sealed class Bullet(Player tankOwner, FloatPosition position, double ro public bool IsExplosive { get; } = isExplosive; + public DateTime Timeout { get; } = timeout; + public PixelBounds Bounds => new (Position.ToPixelPosition(), Position.ToPixelPosition()); }