servicepoint-tanks/tanks-backend/TanksServer/GameLogic/SpawnPowerUp.cs

41 lines
1.2 KiB
C#
Raw Normal View History

using System.Diagnostics;
2024-04-17 19:34:19 +02:00
namespace TanksServer.GameLogic;
internal sealed class SpawnPowerUp(
IOptions<GameRules> options,
MapEntityManager entityManager
) : ITickStep
{
private readonly double _spawnChance = options.Value.PowerUpSpawnChance;
2024-04-17 20:12:36 +02:00
private readonly int _maxCount = options.Value.MaxPowerUpCount;
2024-04-17 19:34:19 +02:00
2024-04-28 15:34:32 +02:00
public ValueTask TickAsync(TimeSpan delta)
2024-04-17 19:34:19 +02:00
{
2024-04-17 20:12:36 +02:00
if (entityManager.PowerUps.Count() >= _maxCount)
2024-04-28 15:34:32 +02:00
return ValueTask.CompletedTask;
2024-04-17 19:34:19 +02:00
if (Random.Shared.NextDouble() > _spawnChance * delta.TotalSeconds)
2024-04-28 15:34:32 +02:00
return ValueTask.CompletedTask;
2024-04-17 19:34:19 +02:00
2024-04-29 21:52:50 +02:00
var type = Random.Shared.Next(4) == 0
2024-04-30 11:17:02 +02:00
? PowerUpType.MagazineSize
: PowerUpType.MagazineType;
MagazineType? magazineType = type switch
{
2024-04-30 11:17:02 +02:00
PowerUpType.MagazineType => Random.Shared.Next(0, 3) switch
{
0 => MagazineType.Fast,
1 => MagazineType.Explosive,
2 => MagazineType.Smart,
_ => throw new UnreachableException()
},
_ => null
};
entityManager.SpawnPowerUp(type, magazineType);
2024-04-28 15:34:32 +02:00
return ValueTask.CompletedTask;
2024-04-17 19:34:19 +02:00
}
}