From a5a3ca30135071345df163195e766dbc424fff6b Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 29 Apr 2024 17:17:44 +0200 Subject: [PATCH] implement reloading --- .../TanksServer/GameLogic/CollectPowerUp.cs | 7 ++++++- .../TanksServer/GameLogic/GameRules.cs | 2 ++ .../TanksServer/GameLogic/ShootFromTanks.cs | 19 ++++++++++++++++--- .../Interactivity/PlayerInfoConnection.cs | 12 ++++++++---- tanks-backend/TanksServer/Models/Tank.cs | 2 ++ 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/tanks-backend/TanksServer/GameLogic/CollectPowerUp.cs b/tanks-backend/TanksServer/GameLogic/CollectPowerUp.cs index e0c5ec4..0195465 100644 --- a/tanks-backend/TanksServer/GameLogic/CollectPowerUp.cs +++ b/tanks-backend/TanksServer/GameLogic/CollectPowerUp.cs @@ -21,11 +21,16 @@ internal sealed class CollectPowerUp( continue; // now the tank overlaps the power up by at least 0.5 tiles + tank.Magazine = tank.Magazine with { UsedBullets = 0, - Type = MagazineType.Explosive + Type = tank.Magazine.Type | MagazineType.Explosive }; + + if (tank.ReloadingUntil >= DateTime.Now) + tank.ReloadingUntil = DateTime.Now; + tank.Owner.Scores.PowerUpsCollected++; return true; } diff --git a/tanks-backend/TanksServer/GameLogic/GameRules.cs b/tanks-backend/TanksServer/GameLogic/GameRules.cs index eb28672..5c4a98f 100644 --- a/tanks-backend/TanksServer/GameLogic/GameRules.cs +++ b/tanks-backend/TanksServer/GameLogic/GameRules.cs @@ -23,4 +23,6 @@ internal sealed class GameRules public int IdleTimeoutMs { get; set; } public byte MagazineSize { get; set; } = 5; + + public int ReloadDelayMs { get; set; } = 3000; } diff --git a/tanks-backend/TanksServer/GameLogic/ShootFromTanks.cs b/tanks-backend/TanksServer/GameLogic/ShootFromTanks.cs index 86a46d3..9df8a64 100644 --- a/tanks-backend/TanksServer/GameLogic/ShootFromTanks.cs +++ b/tanks-backend/TanksServer/GameLogic/ShootFromTanks.cs @@ -19,12 +19,25 @@ internal sealed class ShootFromTanks( { if (!tank.Owner.Controls.Shoot) return; - if (tank.Magazine.Empty) + + var now = DateTime.Now; + if (tank.NextShotAfter >= now) return; - if (tank.NextShotAfter >= DateTime.Now) + if (tank.ReloadingUntil >= now) return; - tank.NextShotAfter = DateTime.Now.AddMilliseconds(_config.ShootDelayMs); + if (tank.Magazine.Empty) + { + tank.ReloadingUntil = now.AddMilliseconds(_config.ReloadDelayMs); + tank.Magazine = tank.Magazine with + { + UsedBullets = 0, + Type = MagazineType.Basic + }; + return; + } + + tank.NextShotAfter = now.AddMilliseconds(_config.ShootDelayMs); tank.Magazine = tank.Magazine with { UsedBullets = (byte)(tank.Magazine.UsedBullets + 1) diff --git a/tanks-backend/TanksServer/Interactivity/PlayerInfoConnection.cs b/tanks-backend/TanksServer/Interactivity/PlayerInfoConnection.cs index b231ec5..bc4956b 100644 --- a/tanks-backend/TanksServer/Interactivity/PlayerInfoConnection.cs +++ b/tanks-backend/TanksServer/Interactivity/PlayerInfoConnection.cs @@ -1,5 +1,4 @@ using System.Net.WebSockets; -using System.Text; using System.Text.Json; using TanksServer.GameLogic; @@ -46,9 +45,14 @@ internal sealed class PlayerInfoConnection( private byte[]? GetMessageToSend() { var tank = entityManager.GetCurrentTankOfPlayer(player); - TankInfo? tankInfo = tank != null - ? new TankInfo(tank.Orientation, tank.Magazine.ToDisplayString(), tank.Position.ToPixelPosition(), tank.Moving) - : null; + + TankInfo? tankInfo = null; + if (tank != null) + { + var magazine = tank.ReloadingUntil > DateTime.Now ? "[ RELOADING ]" : tank.Magazine.ToDisplayString(); + tankInfo = new TankInfo(tank.Orientation, magazine, tank.Position.ToPixelPosition(), tank.Moving); + } + var info = new PlayerInfo(player.Name, player.Scores, player.Controls.ToDisplayString(), tankInfo); var response = JsonSerializer.SerializeToUtf8Bytes(info, _context.PlayerInfo); diff --git a/tanks-backend/TanksServer/Models/Tank.cs b/tanks-backend/TanksServer/Models/Tank.cs index 4d449a2..e9de961 100644 --- a/tanks-backend/TanksServer/Models/Tank.cs +++ b/tanks-backend/TanksServer/Models/Tank.cs @@ -31,4 +31,6 @@ internal sealed class Tank : IMapEntity public int Orientation => (int)Math.Round(Rotation * 16) % 16; public required Magazine Magazine { get; set; } + + public DateTime ReloadingUntil { get; set; } }