implement reloading

This commit is contained in:
Vinzenz Schroeter 2024-04-29 17:17:44 +02:00
parent 9164d90443
commit a5a3ca3013
5 changed files with 34 additions and 8 deletions

View file

@ -21,11 +21,16 @@ internal sealed class CollectPowerUp(
continue; continue;
// 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
tank.Magazine = tank.Magazine with tank.Magazine = tank.Magazine with
{ {
UsedBullets = 0, UsedBullets = 0,
Type = MagazineType.Explosive Type = tank.Magazine.Type | MagazineType.Explosive
}; };
if (tank.ReloadingUntil >= DateTime.Now)
tank.ReloadingUntil = DateTime.Now;
tank.Owner.Scores.PowerUpsCollected++; tank.Owner.Scores.PowerUpsCollected++;
return true; return true;
} }

View file

@ -23,4 +23,6 @@ internal sealed class GameRules
public int IdleTimeoutMs { get; set; } public int IdleTimeoutMs { get; set; }
public byte MagazineSize { get; set; } = 5; public byte MagazineSize { get; set; } = 5;
public int ReloadDelayMs { get; set; } = 3000;
} }

View file

@ -19,12 +19,25 @@ internal sealed class ShootFromTanks(
{ {
if (!tank.Owner.Controls.Shoot) if (!tank.Owner.Controls.Shoot)
return; return;
if (tank.Magazine.Empty)
var now = DateTime.Now;
if (tank.NextShotAfter >= now)
return; return;
if (tank.NextShotAfter >= DateTime.Now) if (tank.ReloadingUntil >= now)
return; 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 tank.Magazine = tank.Magazine with
{ {
UsedBullets = (byte)(tank.Magazine.UsedBullets + 1) UsedBullets = (byte)(tank.Magazine.UsedBullets + 1)

View file

@ -1,5 +1,4 @@
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Text;
using System.Text.Json; using System.Text.Json;
using TanksServer.GameLogic; using TanksServer.GameLogic;
@ -46,9 +45,14 @@ internal sealed class PlayerInfoConnection(
private byte[]? GetMessageToSend() private byte[]? GetMessageToSend()
{ {
var tank = entityManager.GetCurrentTankOfPlayer(player); var tank = entityManager.GetCurrentTankOfPlayer(player);
TankInfo? tankInfo = tank != null
? new TankInfo(tank.Orientation, tank.Magazine.ToDisplayString(), tank.Position.ToPixelPosition(), tank.Moving) TankInfo? tankInfo = null;
: 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 info = new PlayerInfo(player.Name, player.Scores, player.Controls.ToDisplayString(), tankInfo);
var response = JsonSerializer.SerializeToUtf8Bytes(info, _context.PlayerInfo); var response = JsonSerializer.SerializeToUtf8Bytes(info, _context.PlayerInfo);

View file

@ -31,4 +31,6 @@ internal sealed class Tank : IMapEntity
public int Orientation => (int)Math.Round(Rotation * 16) % 16; public int Orientation => (int)Math.Round(Rotation * 16) % 16;
public required Magazine Magazine { get; set; } public required Magazine Magazine { get; set; }
public DateTime ReloadingUntil { get; set; }
} }