implement reloading
This commit is contained in:
parent
9164d90443
commit
a5a3ca3013
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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; }
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue