2024-04-29 18:03:23 +02:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
2024-04-17 19:34:19 +02:00
|
|
|
namespace TanksServer.GameLogic;
|
|
|
|
|
|
|
|
internal sealed class SpawnPowerUp(
|
|
|
|
IOptions<GameRules> options,
|
2024-05-03 15:47:33 +02:00
|
|
|
MapEntityManager entityManager,
|
|
|
|
EmptyTileFinder emptyTileFinder
|
2024-04-17 19:34:19 +02:00
|
|
|
) : 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-05-08 00:29:33 +02:00
|
|
|
var type = (PowerUpType)Random.Shared.Next((int)Enum.GetValues<PowerUpType>().Max());
|
2024-05-03 15:47:33 +02:00
|
|
|
var position = emptyTileFinder.ChooseEmptyTile().GetCenter().ToFloatPosition();
|
2024-05-08 00:29:33 +02:00
|
|
|
entityManager.SpawnPowerUp(position, type);
|
2024-04-28 15:34:32 +02:00
|
|
|
return ValueTask.CompletedTask;
|
2024-04-17 19:34:19 +02:00
|
|
|
}
|
|
|
|
}
|