position is now center, socket improvements

This commit is contained in:
Vinzenz Schroeter 2024-04-13 14:07:14 +02:00
parent 40eba7a7c7
commit d4d0abd013
18 changed files with 134 additions and 106 deletions

View file

@ -1,7 +1,9 @@
namespace TanksServer.GameLogic;
internal sealed class CollideBulletsWithTanks(
BulletManager bullets, TankManager tanks, SpawnQueue spawnQueue
BulletManager bullets,
TankManager tanks,
SpawnQueue spawnQueue
) : ITickStep
{
public Task TickAsync()
@ -14,7 +16,7 @@ internal sealed class CollideBulletsWithTanks(
{
foreach (var tank in tanks)
{
var (topLeft, bottomRight) = TankManager.GetTankBounds(tank.Position.ToPixelPosition());
var (topLeft, bottomRight) = tank.Bounds;
if (bullet.Position.X < topLeft.X || bullet.Position.X > bottomRight.X ||
bullet.Position.Y < topLeft.Y || bullet.Position.Y > bottomRight.Y)
continue;

View file

@ -47,9 +47,7 @@ internal sealed class MoveTanks(
private bool TryMoveTankTo(Tank tank, FloatPosition newPosition)
{
var (topLeft, bottomRight) = TankManager.GetTankBounds(newPosition.ToPixelPosition());
if (HitsWall(topLeft, bottomRight))
if (HitsWall(newPosition))
return false;
if (HitsTank(tank, newPosition))
return false;
@ -63,15 +61,16 @@ internal sealed class MoveTanks(
.Where(otherTank => otherTank != tank)
.Any(otherTank => newPosition.Distance(otherTank.Position) < MapService.TileSize);
private bool HitsWall(PixelPosition topLeft, PixelPosition bottomRight)
private bool HitsWall(FloatPosition newPosition)
{
var (topLeft, bottomRight) = Tank.GetBoundsForCenter(newPosition);
TilePosition[] positions =
[
topLeft.ToTilePosition(),
new PixelPosition(bottomRight.X, topLeft.Y).ToTilePosition(),
new PixelPosition(topLeft.X, bottomRight.Y).ToTilePosition(),
bottomRight.ToTilePosition(),
bottomRight.ToTilePosition()
];
return positions.Any(map.IsCurrentlyWall);
}
}
}

View file

@ -27,10 +27,10 @@ internal sealed class ShootFromTanks(
var angle = tank.Rotation * 2 * Math.PI;
var position = new FloatPosition(
x: tank.Position.X + MapService.TileSize / 2d + Math.Sin(angle) * _config.BulletSpeed,
y: tank.Position.Y + MapService.TileSize / 2d - Math.Cos(angle) * _config.BulletSpeed
tank.Position.X + Math.Sin(angle) * _config.BulletSpeed,
tank.Position.Y - Math.Cos(angle) * _config.BulletSpeed
);
bulletManager.Spawn(new Bullet(tank.Owner, position, tank.Rotation));
}
}
}

View file

@ -44,9 +44,6 @@ internal sealed class SpawnNewTanks(
}
var min = candidates.MaxBy(kvp => kvp.Value).Key;
return new FloatPosition(
min.X * MapService.TileSize,
min.Y * MapService.TileSize
);
return min.ToPixelPosition().GetPixelRelative(4, 4).ToFloatPosition();
}
}
}

View file

@ -20,12 +20,4 @@ internal sealed class TankManager(ILogger<TankManager> logger) : IEnumerable<Tan
logger.LogInformation("Tank removed for player {}", tank.Owner.Id);
_tanks.Remove(tank, out _);
}
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)
));
}
}
}