position is now center, socket improvements
This commit is contained in:
parent
40eba7a7c7
commit
d4d0abd013
18 changed files with 134 additions and 106 deletions
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue