infinite map
This commit is contained in:
parent
7213318838
commit
461a9139c2
20 changed files with 101 additions and 69 deletions
|
@ -14,7 +14,7 @@ internal sealed class CollideBulletsWithTanks(
|
|||
{
|
||||
foreach (var tank in tanks)
|
||||
{
|
||||
var (topLeft, bottomRight) = tank.GetBounds();
|
||||
var (topLeft, bottomRight) = TankManager.GetTankBounds(tank.Position.ToPixelPosition());
|
||||
if (bullet.Position.X < topLeft.X || bullet.Position.X > bottomRight.X ||
|
||||
bullet.Position.Y < topLeft.Y || bullet.Position.Y > bottomRight.Y)
|
||||
continue;
|
||||
|
|
|
@ -2,15 +2,15 @@ namespace TanksServer.GameLogic;
|
|||
|
||||
internal sealed class MapService
|
||||
{
|
||||
public const int TilesPerRow = 44;
|
||||
public const int TilesPerColumn = 20;
|
||||
public const int TileSize = 8;
|
||||
public const int PixelsPerRow = TilesPerRow * TileSize;
|
||||
public const int PixelsPerColumn = TilesPerColumn * TileSize;
|
||||
public const ushort TilesPerRow = 44;
|
||||
public const ushort TilesPerColumn = 20;
|
||||
public const ushort TileSize = 8;
|
||||
public const ushort PixelsPerRow = TilesPerRow * TileSize;
|
||||
public const ushort PixelsPerColumn = TilesPerColumn * TileSize;
|
||||
|
||||
private readonly string _map =
|
||||
"""
|
||||
############################################
|
||||
#############..###################.#########
|
||||
#...................##.....................#
|
||||
#...................##.....................#
|
||||
#.....####......................####.......#
|
||||
|
@ -29,7 +29,7 @@ internal sealed class MapService
|
|||
#.....####......................####.......#
|
||||
#...................##.....................#
|
||||
#...................##.....................#
|
||||
############################################
|
||||
#############..###################.#########
|
||||
"""
|
||||
.ReplaceLineEndings(string.Empty);
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace TanksServer.GameLogic;
|
||||
|
||||
internal sealed class MoveBullets(BulletManager bullets) : ITickStep
|
||||
internal sealed class MoveBullets(BulletManager bullets, IOptions<TanksConfiguration> config) : ITickStep
|
||||
{
|
||||
public Task TickAsync()
|
||||
{
|
||||
|
@ -10,12 +10,12 @@ internal sealed class MoveBullets(BulletManager bullets) : ITickStep
|
|||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static void MoveBullet(Bullet bullet)
|
||||
private void MoveBullet(Bullet bullet)
|
||||
{
|
||||
var angle = bullet.Rotation / 16 * 2 * Math.PI;
|
||||
var angle = bullet.Rotation * 2 * Math.PI;
|
||||
bullet.Position = new FloatPosition(
|
||||
X: bullet.Position.X + Math.Sin(angle) * 3,
|
||||
Y: bullet.Position.Y - Math.Cos(angle) * 3
|
||||
x: bullet.Position.X + Math.Sin(angle) * config.Value.BulletSpeed,
|
||||
y: bullet.Position.Y - Math.Cos(angle) * config.Value.BulletSpeed
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -36,26 +36,30 @@ internal sealed class MoveTanks(
|
|||
return false;
|
||||
}
|
||||
|
||||
var angle = tank.Rotation / 16d * 2d * Math.PI;
|
||||
var angle = tank.Rotation * 2d * Math.PI;
|
||||
var newX = tank.Position.X + Math.Sin(angle) * speed;
|
||||
var newY = tank.Position.Y - Math.Cos(angle) * speed;
|
||||
|
||||
return TryMoveTankTo(tank, new FloatPosition(newX, newY))
|
||||
|| TryMoveTankTo(tank, tank.Position with { X = newX })
|
||||
|| TryMoveTankTo(tank, tank.Position with { Y = newY });
|
||||
|| TryMoveTankTo(tank, new FloatPosition(newX, tank.Position.Y))
|
||||
|| TryMoveTankTo(tank, new FloatPosition(tank.Position.X, newY));
|
||||
}
|
||||
|
||||
private bool TryMoveTankTo(Tank tank, FloatPosition newPosition)
|
||||
{
|
||||
var x0 = (int)Math.Floor(newPosition.X / MapService.TileSize);
|
||||
var x1 = (int)Math.Ceiling(newPosition.X / MapService.TileSize);
|
||||
var y0 = (int)Math.Floor(newPosition.Y / MapService.TileSize);
|
||||
var y1 = (int)Math.Ceiling(newPosition.Y / MapService.TileSize);
|
||||
|
||||
TilePosition[] positions = { new(x0, y0), new(x0, y1), new(x1, y0), new(x1, y1) };
|
||||
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))
|
||||
return false;
|
||||
|
||||
// TODO: check tanks
|
||||
|
||||
tank.Position = newPosition;
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -11,9 +11,9 @@ internal sealed class RotateTanks(TankManager tanks, IOptions<TanksConfiguration
|
|||
var player = tank.Owner;
|
||||
|
||||
if (player.Controls.TurnLeft)
|
||||
tank.Rotation -= _config.TurnSpeed;
|
||||
tank.Rotation -= _config.TurnSpeed / 16d;
|
||||
if (player.Controls.TurnRight)
|
||||
tank.Rotation += _config.TurnSpeed;
|
||||
tank.Rotation += _config.TurnSpeed / 16d;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
|
|
@ -25,10 +25,10 @@ internal sealed class ShootFromTanks(
|
|||
|
||||
tank.NextShotAfter = DateTime.Now.AddMilliseconds(_config.ShootDelayMs);
|
||||
|
||||
var angle = tank.Rotation / 16 * 2 * Math.PI;
|
||||
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
|
||||
x: tank.Position.X + MapService.TileSize / 2d + Math.Sin(angle) * _config.BulletSpeed,
|
||||
y: tank.Position.Y + MapService.TileSize / 2d - Math.Cos(angle) * _config.BulletSpeed
|
||||
);
|
||||
|
||||
bulletManager.Spawn(new Bullet(tank.Owner, position, tank.Rotation));
|
||||
|
|
|
@ -14,7 +14,7 @@ internal sealed class SpawnNewTanks(
|
|||
|
||||
tanks.Add(new Tank(player, ChooseSpawnPosition())
|
||||
{
|
||||
Rotation = Random.Shared.Next(0, 16)
|
||||
Rotation = Random.Shared.NextDouble()
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
@ -24,8 +24,8 @@ internal sealed class SpawnNewTanks(
|
|||
{
|
||||
Dictionary<TilePosition, double> candidates = [];
|
||||
|
||||
for (var x = 0; x < MapService.TilesPerRow; x++)
|
||||
for (var y = 0; y < MapService.TilesPerColumn; y++)
|
||||
for (ushort x = 0; x < MapService.TilesPerRow; x++)
|
||||
for (ushort y = 0; y < MapService.TilesPerColumn; y++)
|
||||
{
|
||||
var tile = new TilePosition(x, y);
|
||||
|
||||
|
|
|
@ -20,4 +20,12 @@ 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