2024-04-07 13:02:49 +02:00
|
|
|
using System.Collections;
|
|
|
|
|
2024-04-10 19:25:45 +02:00
|
|
|
namespace TanksServer.GameLogic;
|
2024-04-07 13:02:49 +02:00
|
|
|
|
2024-04-07 19:52:16 +02:00
|
|
|
internal sealed class TankManager(ILogger<TankManager> logger) : IEnumerable<Tank>
|
2024-04-07 13:02:49 +02:00
|
|
|
{
|
2024-04-07 21:09:36 +02:00
|
|
|
private readonly ConcurrentDictionary<Tank, byte> _tanks = new();
|
2024-04-07 13:02:49 +02:00
|
|
|
|
|
|
|
public void Add(Tank tank)
|
|
|
|
{
|
2024-04-07 17:17:11 +02:00
|
|
|
logger.LogInformation("Tank added for player {}", tank.Owner.Id);
|
2024-04-07 21:09:36 +02:00
|
|
|
_tanks.TryAdd(tank, 0);
|
2024-04-07 13:02:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
2024-04-07 21:09:36 +02:00
|
|
|
public IEnumerator<Tank> GetEnumerator() => _tanks.Keys.GetEnumerator();
|
|
|
|
|
|
|
|
public void Remove(Tank tank)
|
|
|
|
{
|
|
|
|
logger.LogInformation("Tank removed for player {}", tank.Owner.Id);
|
|
|
|
_tanks.Remove(tank, out _);
|
|
|
|
}
|
2024-04-12 18:32:10 +02:00
|
|
|
|
|
|
|
public static (PixelPosition TopLeft, PixelPosition BottomRight) GetTankBounds(PixelPosition tankPosition)
|
|
|
|
{
|
|
|
|
return (tankPosition, new PixelPosition(
|
|
|
|
(ushort)(tankPosition.X + MapService.TileSize - 1),
|
|
|
|
(ushort)(tankPosition.Y + MapService.TileSize - 1)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|