tanks collide with each other
This commit is contained in:
parent
461a9139c2
commit
89494ef495
5 changed files with 46 additions and 30 deletions
|
@ -10,26 +10,26 @@ internal sealed class MapService
|
|||
|
||||
private readonly string _map =
|
||||
"""
|
||||
#############..###################.#########
|
||||
#######.##########################.#########
|
||||
#...................##.....................#
|
||||
#...................##.....................#
|
||||
#.....####......................####.......#
|
||||
#..........................................#
|
||||
#............###...........###.............#
|
||||
#............#...............#.............#
|
||||
#...##.......#...............#......##.....#
|
||||
#...##.......#....#....#.....#......##.....#
|
||||
#....#..............................#......#
|
||||
#....#..##......................##..#......#
|
||||
#....#..##......................##..#......#
|
||||
.....#...#......................#...#.......
|
||||
.....#...#......................#...#.......
|
||||
#....#..............................#......#
|
||||
#...##.......#...............#......##.....#
|
||||
#...##.......#....#....#.....#......##.....#
|
||||
#............#...............#.............#
|
||||
#............###...........###.............#
|
||||
#..........................................#
|
||||
#.....####......................####.......#
|
||||
#...................##.....................#
|
||||
#...................##.....................#
|
||||
#############..###################.#########
|
||||
#######.##########################.#########
|
||||
"""
|
||||
.ReplaceLineEndings(string.Empty);
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
namespace TanksServer.GameLogic;
|
||||
|
||||
internal sealed class MoveTanks(
|
||||
TankManager tanks,
|
||||
TankManager tanks,
|
||||
IOptions<TanksConfiguration> options,
|
||||
MapService map
|
||||
) : ITickStep
|
||||
|
@ -48,19 +48,30 @@ internal sealed class MoveTanks(
|
|||
private bool TryMoveTankTo(Tank tank, FloatPosition newPosition)
|
||||
{
|
||||
var (topLeft, bottomRight) = TankManager.GetTankBounds(newPosition.ToPixelPosition());
|
||||
TilePosition[] positions = [
|
||||
topLeft.ToTilePosition(),
|
||||
new PixelPosition(bottomRight.X, topLeft.Y).ToTilePosition(),
|
||||
new PixelPosition(topLeft.X, bottomRight.Y).ToTilePosition(),
|
||||
bottomRight.ToTilePosition(),
|
||||
];
|
||||
|
||||
if (positions.Any(map.IsCurrentlyWall))
|
||||
|
||||
if (HitsWall(topLeft, bottomRight))
|
||||
return false;
|
||||
if (HitsTank(tank, newPosition))
|
||||
return false;
|
||||
|
||||
// TODO: check tanks
|
||||
|
||||
tank.Position = newPosition;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private bool HitsTank(Tank tank, FloatPosition newPosition) =>
|
||||
tanks
|
||||
.Where(otherTank => otherTank != tank)
|
||||
.Any(otherTank => newPosition.Distance(otherTank.Position) < MapService.TileSize);
|
||||
|
||||
private bool HitsWall(PixelPosition topLeft, PixelPosition bottomRight)
|
||||
{
|
||||
TilePosition[] positions =
|
||||
[
|
||||
topLeft.ToTilePosition(),
|
||||
new PixelPosition(bottomRight.X, topLeft.Y).ToTilePosition(),
|
||||
new PixelPosition(topLeft.X, bottomRight.Y).ToTilePosition(),
|
||||
bottomRight.ToTilePosition(),
|
||||
];
|
||||
return positions.Any(map.IsCurrentlyWall);
|
||||
}
|
||||
}
|
|
@ -24,24 +24,22 @@ internal sealed class SpawnNewTanks(
|
|||
{
|
||||
Dictionary<TilePosition, double> candidates = [];
|
||||
|
||||
for (ushort x = 0; x < MapService.TilesPerRow; x++)
|
||||
for (ushort y = 0; y < MapService.TilesPerColumn; y++)
|
||||
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 (map.IsCurrentlyWall(tile))
|
||||
continue;
|
||||
|
||||
var tilePixelCenter = tile.GetPixelRelative(4, 4);
|
||||
var tilePixelCenter = tile.GetPixelRelative(4, 4).ToFloatPosition();
|
||||
|
||||
var minDistance = bullets.GetAll()
|
||||
.Cast<IMapEntity>()
|
||||
.Concat(tanks)
|
||||
.Select(entity => Math.Sqrt(
|
||||
Math.Pow(entity.Position.X - tilePixelCenter.X, 2) +
|
||||
Math.Pow(entity.Position.Y - tilePixelCenter.Y, 2)))
|
||||
.Select(entity => entity.Position.Distance(tilePixelCenter))
|
||||
.Aggregate(double.MaxValue, Math.Min);
|
||||
|
||||
|
||||
candidates.Add(tile, minDistance);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue