reposition tanks on map switch, rework map logic
This commit is contained in:
parent
3d65c81b8b
commit
97144ae3b8
13 changed files with 208 additions and 117 deletions
32
tanks-backend/TanksServer/GameLogic/EmptyTileFinder.cs
Normal file
32
tanks-backend/TanksServer/GameLogic/EmptyTileFinder.cs
Normal file
|
@ -0,0 +1,32 @@
|
|||
namespace TanksServer.GameLogic;
|
||||
|
||||
internal sealed class EmptyTileFinder(
|
||||
MapEntityManager entityManager,
|
||||
MapService mapService
|
||||
)
|
||||
{
|
||||
public TilePosition ChooseEmptyTile()
|
||||
{
|
||||
var maxMinDistance = 0d;
|
||||
TilePosition spawnTile = default;
|
||||
for (ushort x = 1; x < MapService.TilesPerRow - 1; x++)
|
||||
for (ushort y = 1; y < MapService.TilesPerColumn - 1; y++)
|
||||
{
|
||||
var tile = new TilePosition(x, y);
|
||||
if (mapService.Current.IsWall(tile))
|
||||
continue;
|
||||
|
||||
var tilePixelCenter = tile.GetCenter().ToFloatPosition();
|
||||
var minDistance = entityManager.AllEntities
|
||||
.Select(entity => entity.Position.Distance(tilePixelCenter))
|
||||
.Aggregate(double.MaxValue, Math.Min);
|
||||
if (minDistance <= maxMinDistance)
|
||||
continue;
|
||||
|
||||
maxMinDistance = minDistance;
|
||||
spawnTile = tile;
|
||||
}
|
||||
|
||||
return spawnTile;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue