From dd33ec59ade184edfab97882f4d4fdacf8aac0c2 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 7 Apr 2024 21:09:36 +0200 Subject: [PATCH] bullet hits tank, tank dies --- TanksServer/DrawSteps/TankDrawer.cs | 2 +- TanksServer/Models/Bullet.cs | 2 +- TanksServer/Models/Player.cs | 4 ++++ TanksServer/Models/Tank.cs | 15 +++++++++++++-- TanksServer/Services/TankManager.cs | 12 +++++++++--- .../TickSteps/CollideBulletsWithTanks.cs | 19 +++++++++++++++++-- 6 files changed, 45 insertions(+), 9 deletions(-) diff --git a/TanksServer/DrawSteps/TankDrawer.cs b/TanksServer/DrawSteps/TankDrawer.cs index b4ee9b2..f76ac60 100644 --- a/TanksServer/DrawSteps/TankDrawer.cs +++ b/TanksServer/DrawSteps/TankDrawer.cs @@ -34,7 +34,7 @@ internal sealed class TankDrawer : IDrawStep foreach (var tank in _tanks) { var pos = tank.Position.ToPixelPosition(); - var rotationVariant = (int)Math.Floor(tank.Rotation); + var rotationVariant = (int)Math.Round(tank.Rotation) % 16; for (var dy = 0; dy < MapService.TileSize; dy++) { var rowStartIndex = (pos.Y + dy) * MapService.PixelsPerRow; diff --git a/TanksServer/Models/Bullet.cs b/TanksServer/Models/Bullet.cs index ebb7ce8..00f6125 100644 --- a/TanksServer/Models/Bullet.cs +++ b/TanksServer/Models/Bullet.cs @@ -2,7 +2,7 @@ namespace TanksServer.Models; internal sealed class Bullet(Player tankOwner, FloatPosition position, double rotation) { - public Player TankOwner { get; } = tankOwner; + public Player Owner { get; } = tankOwner; public FloatPosition Position { get; set; } = position; diff --git a/TanksServer/Models/Player.cs b/TanksServer/Models/Player.cs index 10a7d59..3af9c24 100644 --- a/TanksServer/Models/Player.cs +++ b/TanksServer/Models/Player.cs @@ -7,6 +7,10 @@ internal sealed class Player(string name) public Guid Id { get; } = Guid.NewGuid(); public PlayerControls Controls { get; } = new(); + + public int Kills { get; set; } + + public int Deaths { get; set; } } internal sealed class PlayerControls diff --git a/TanksServer/Models/Tank.cs b/TanksServer/Models/Tank.cs index 9c2a8a4..5b63cf9 100644 --- a/TanksServer/Models/Tank.cs +++ b/TanksServer/Models/Tank.cs @@ -1,3 +1,5 @@ +using TanksServer.Services; + namespace TanksServer.Models; internal sealed class Tank(Player player, FloatPosition spawnPosition) @@ -16,8 +18,17 @@ internal sealed class Tank(Player player, FloatPosition spawnPosition) } public FloatPosition Position { get; set; } = spawnPosition; - + public DateTime NextShotAfter { get; set; } - + public bool Moved { get; set; } + + public (FloatPosition TopLeft, FloatPosition BottomRight) GetBounds() + { + const int halfTile = MapService.TileSize / 2; + return ( + new FloatPosition(Position.X - halfTile, Position.Y - halfTile), + new FloatPosition(Position.X + halfTile, Position.Y + halfTile) + ); + } } diff --git a/TanksServer/Services/TankManager.cs b/TanksServer/Services/TankManager.cs index b4ae7b4..100ebb4 100644 --- a/TanksServer/Services/TankManager.cs +++ b/TanksServer/Services/TankManager.cs @@ -6,14 +6,20 @@ namespace TanksServer.Services; internal sealed class TankManager(ILogger logger) : IEnumerable { - private readonly ConcurrentBag _tanks = new(); + private readonly ConcurrentDictionary _tanks = new(); public void Add(Tank tank) { logger.LogInformation("Tank added for player {}", tank.Owner.Id); - _tanks.Add(tank); + _tanks.TryAdd(tank, 0); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - public IEnumerator GetEnumerator() => _tanks.GetEnumerator(); + public IEnumerator GetEnumerator() => _tanks.Keys.GetEnumerator(); + + public void Remove(Tank tank) + { + logger.LogInformation("Tank removed for player {}", tank.Owner.Id); + _tanks.Remove(tank, out _); + } } diff --git a/TanksServer/TickSteps/CollideBulletsWithTanks.cs b/TanksServer/TickSteps/CollideBulletsWithTanks.cs index bd93f94..f477cea 100644 --- a/TanksServer/TickSteps/CollideBulletsWithTanks.cs +++ b/TanksServer/TickSteps/CollideBulletsWithTanks.cs @@ -3,7 +3,7 @@ using TanksServer.Services; namespace TanksServer.TickSteps; -internal sealed class CollideBulletsWithTanks(BulletManager bullets) : ITickStep +internal sealed class CollideBulletsWithTanks(BulletManager bullets, TankManager tanks) : ITickStep { public Task TickAsync() { @@ -13,6 +13,21 @@ internal sealed class CollideBulletsWithTanks(BulletManager bullets) : ITickStep private bool BulletHitsTank(Bullet bullet) { - return false; // TODO + foreach (var tank in tanks) + { + var (topLeft, bottomRight) = tank.GetBounds(); + if (bullet.Position.X <= topLeft.X || bullet.Position.X >= bottomRight.X || + bullet.Position.Y <= topLeft.Y || bullet.Position.Y >= bottomRight.Y) + continue; + + if (bullet.Owner != tank.Owner) + bullet.Owner.Kills++; + tank.Owner.Deaths++; + + tanks.Remove(tank); + return true; + } + + return false; } }