servicepoint-tanks/TanksServer/TickSteps/SpawnNewTanks.cs

51 lines
1.3 KiB
C#
Raw Normal View History

2024-04-07 13:02:49 +02:00
using System.Collections.Concurrent;
2024-04-07 19:52:16 +02:00
namespace TanksServer.TickSteps;
2024-04-07 13:02:49 +02:00
2024-04-07 19:52:16 +02:00
internal sealed class SpawnNewTanks(TankManager tanks, MapService map) : ITickStep
2024-04-07 13:02:49 +02:00
{
private readonly ConcurrentQueue<Player> _playersToSpawn = new();
public Task TickAsync()
{
while (_playersToSpawn.TryDequeue(out var player))
{
var tank = new Tank(player, ChooseSpawnPosition())
{
Rotation = Random.Shared.Next(0, 16)
};
tanks.Add(tank);
}
return Task.CompletedTask;
}
2024-04-07 17:17:11 +02:00
private FloatPosition ChooseSpawnPosition()
2024-04-07 13:02:49 +02:00
{
List<TilePosition> candidates = new();
for (var x = 0; x < MapService.TilesPerRow; x++)
for (var y = 0; y < MapService.TilesPerColumn; y++)
{
var tile = new TilePosition(x, y);
if (map.IsCurrentlyWall(tile))
continue;
2024-04-07 17:17:11 +02:00
// TODO: check tanks and bullets
2024-04-07 13:02:49 +02:00
candidates.Add(tile);
}
var chosenTile = candidates[Random.Shared.Next(candidates.Count)];
2024-04-07 17:17:11 +02:00
return new FloatPosition(
2024-04-07 13:02:49 +02:00
chosenTile.X * MapService.TileSize,
chosenTile.Y * MapService.TileSize
);
}
public void SpawnTankForPlayer(Player player)
{
_playersToSpawn.Enqueue(player);
}
}