infinite map

This commit is contained in:
Vinzenz Schroeter 2024-04-12 18:32:10 +02:00
parent 7213318838
commit 461a9139c2
20 changed files with 101 additions and 69 deletions

View file

@ -8,6 +8,7 @@ public sealed class Cp437Grid(ushort width, ushort height)
private readonly ByteGrid _byteGrid = new(width, height); private readonly ByteGrid _byteGrid = new(width, height);
public ushort Height { get; } = height; public ushort Height { get; } = height;
public ushort Width { get; } = width; public ushort Width { get; } = width;
internal Memory<byte> Data => _byteGrid.Data; internal Memory<byte> Data => _byteGrid.Data;

View file

@ -6,10 +6,10 @@ public interface IDisplayConnection
ValueTask SendCp437DataAsync(ushort x, ushort y, Cp437Grid grid); ValueTask SendCp437DataAsync(ushort x, ushort y, Cp437Grid grid);
ValueTask SendCharBrightnessAsync(ushort x, ushort y, ByteGrid luma);
ValueTask SendBrightnessAsync(byte brightness); ValueTask SendBrightnessAsync(byte brightness);
ValueTask SendCharBrightnessAsync(ushort x, ushort y, ByteGrid luma);
ValueTask SendHardResetAsync(); ValueTask SendHardResetAsync();
ValueTask SendFadeOutAsync(byte loops); ValueTask SendFadeOutAsync(byte loops);

View file

@ -1,6 +1,6 @@
namespace DisplayCommands.Internals; namespace DisplayCommands.Internals;
internal enum DisplayCommand: ushort internal enum DisplayCommand : ushort
{ {
Clear = 0x0002, Clear = 0x0002,
Cp437Data = 0x0003, Cp437Data = 0x0003,
@ -8,7 +8,7 @@ internal enum DisplayCommand: ushort
Brightness = 0x0007, Brightness = 0x0007,
HardReset = 0x000b, HardReset = 0x000b,
FadeOut = 0x000d, FadeOut = 0x000d,
BitmapLegacy = 0x0010, [Obsolete("ignored by display code")] BitmapLegacy = 0x0010,
BitmapLinear = 0x0012, BitmapLinear = 0x0012,
BitmapLinearWin = 0x0013, BitmapLinearWin = 0x0013,
BitmapLinearAnd = 0x0014, BitmapLinearAnd = 0x0014,

View file

@ -14,7 +14,7 @@ internal sealed class CollideBulletsWithTanks(
{ {
foreach (var tank in tanks) 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 || if (bullet.Position.X < topLeft.X || bullet.Position.X > bottomRight.X ||
bullet.Position.Y < topLeft.Y || bullet.Position.Y > bottomRight.Y) bullet.Position.Y < topLeft.Y || bullet.Position.Y > bottomRight.Y)
continue; continue;

View file

@ -2,15 +2,15 @@ namespace TanksServer.GameLogic;
internal sealed class MapService internal sealed class MapService
{ {
public const int TilesPerRow = 44; public const ushort TilesPerRow = 44;
public const int TilesPerColumn = 20; public const ushort TilesPerColumn = 20;
public const int TileSize = 8; public const ushort TileSize = 8;
public const int PixelsPerRow = TilesPerRow * TileSize; public const ushort PixelsPerRow = TilesPerRow * TileSize;
public const int PixelsPerColumn = TilesPerColumn * TileSize; public const ushort PixelsPerColumn = TilesPerColumn * TileSize;
private readonly string _map = private readonly string _map =
""" """
############################################ #############..###################.#########
#...................##.....................# #...................##.....................#
#...................##.....................# #...................##.....................#
#.....####......................####.......# #.....####......................####.......#
@ -29,7 +29,7 @@ internal sealed class MapService
#.....####......................####.......# #.....####......................####.......#
#...................##.....................# #...................##.....................#
#...................##.....................# #...................##.....................#
############################################ #############..###################.#########
""" """
.ReplaceLineEndings(string.Empty); .ReplaceLineEndings(string.Empty);

View file

@ -1,6 +1,6 @@
namespace TanksServer.GameLogic; namespace TanksServer.GameLogic;
internal sealed class MoveBullets(BulletManager bullets) : ITickStep internal sealed class MoveBullets(BulletManager bullets, IOptions<TanksConfiguration> config) : ITickStep
{ {
public Task TickAsync() public Task TickAsync()
{ {
@ -10,12 +10,12 @@ internal sealed class MoveBullets(BulletManager bullets) : ITickStep
return Task.CompletedTask; 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( bullet.Position = new FloatPosition(
X: bullet.Position.X + Math.Sin(angle) * 3, x: bullet.Position.X + Math.Sin(angle) * config.Value.BulletSpeed,
Y: bullet.Position.Y - Math.Cos(angle) * 3 y: bullet.Position.Y - Math.Cos(angle) * config.Value.BulletSpeed
); );
} }
} }

View file

@ -36,26 +36,30 @@ internal sealed class MoveTanks(
return false; 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 newX = tank.Position.X + Math.Sin(angle) * speed;
var newY = tank.Position.Y - Math.Cos(angle) * speed; var newY = tank.Position.Y - Math.Cos(angle) * speed;
return TryMoveTankTo(tank, new FloatPosition(newX, newY)) return TryMoveTankTo(tank, new FloatPosition(newX, newY))
|| TryMoveTankTo(tank, tank.Position with { X = newX }) || TryMoveTankTo(tank, new FloatPosition(newX, tank.Position.Y))
|| TryMoveTankTo(tank, tank.Position with { Y = newY }); || TryMoveTankTo(tank, new FloatPosition(tank.Position.X, newY));
} }
private bool TryMoveTankTo(Tank tank, FloatPosition newPosition) private bool TryMoveTankTo(Tank tank, FloatPosition newPosition)
{ {
var x0 = (int)Math.Floor(newPosition.X / MapService.TileSize); var (topLeft, bottomRight) = TankManager.GetTankBounds(newPosition.ToPixelPosition());
var x1 = (int)Math.Ceiling(newPosition.X / MapService.TileSize); TilePosition[] positions = [
var y0 = (int)Math.Floor(newPosition.Y / MapService.TileSize); topLeft.ToTilePosition(),
var y1 = (int)Math.Ceiling(newPosition.Y / MapService.TileSize); new PixelPosition(bottomRight.X, topLeft.Y).ToTilePosition(),
new PixelPosition(topLeft.X, bottomRight.Y).ToTilePosition(),
bottomRight.ToTilePosition(),
];
TilePosition[] positions = { new(x0, y0), new(x0, y1), new(x1, y0), new(x1, y1) };
if (positions.Any(map.IsCurrentlyWall)) if (positions.Any(map.IsCurrentlyWall))
return false; return false;
// TODO: check tanks
tank.Position = newPosition; tank.Position = newPosition;
return true; return true;
} }

View file

@ -11,9 +11,9 @@ internal sealed class RotateTanks(TankManager tanks, IOptions<TanksConfiguration
var player = tank.Owner; var player = tank.Owner;
if (player.Controls.TurnLeft) if (player.Controls.TurnLeft)
tank.Rotation -= _config.TurnSpeed; tank.Rotation -= _config.TurnSpeed / 16d;
if (player.Controls.TurnRight) if (player.Controls.TurnRight)
tank.Rotation += _config.TurnSpeed; tank.Rotation += _config.TurnSpeed / 16d;
} }
return Task.CompletedTask; return Task.CompletedTask;

View file

@ -25,10 +25,10 @@ internal sealed class ShootFromTanks(
tank.NextShotAfter = DateTime.Now.AddMilliseconds(_config.ShootDelayMs); 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( var position = new FloatPosition(
X: tank.Position.X + MapService.TileSize / 2d + Math.Sin(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 y: tank.Position.Y + MapService.TileSize / 2d - Math.Cos(angle) * _config.BulletSpeed
); );
bulletManager.Spawn(new Bullet(tank.Owner, position, tank.Rotation)); bulletManager.Spawn(new Bullet(tank.Owner, position, tank.Rotation));

View file

@ -14,7 +14,7 @@ internal sealed class SpawnNewTanks(
tanks.Add(new Tank(player, ChooseSpawnPosition()) tanks.Add(new Tank(player, ChooseSpawnPosition())
{ {
Rotation = Random.Shared.Next(0, 16) Rotation = Random.Shared.NextDouble()
}); });
return Task.CompletedTask; return Task.CompletedTask;
@ -24,8 +24,8 @@ internal sealed class SpawnNewTanks(
{ {
Dictionary<TilePosition, double> candidates = []; Dictionary<TilePosition, double> candidates = [];
for (var x = 0; x < MapService.TilesPerRow; x++) for (ushort x = 0; x < MapService.TilesPerRow; x++)
for (var y = 0; y < MapService.TilesPerColumn; y++) for (ushort y = 0; y < MapService.TilesPerColumn; y++)
{ {
var tile = new TilePosition(x, y); var tile = new TilePosition(x, y);

View file

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

View file

@ -12,6 +12,7 @@ internal sealed class DrawStateToFrame(
public Task TickAsync() public Task TickAsync()
{ {
// TODO: fix race condition with shared buffer access
_drawGrid.Clear(); _drawGrid.Clear();
foreach (var step in _drawSteps) foreach (var step in _drawSteps)
step.Draw(_drawGrid); step.Draw(_drawGrid);

View file

@ -7,8 +7,8 @@ internal sealed class MapDrawer(MapService map) : IDrawStep
{ {
public void Draw(PixelGrid buffer) public void Draw(PixelGrid buffer)
{ {
for (var tileY = 0; tileY < MapService.TilesPerColumn; tileY++) for (ushort tileY = 0; tileY < MapService.TilesPerColumn; tileY++)
for (var tileX = 0; tileX < MapService.TilesPerRow; tileX++) for (ushort tileX = 0; tileX < MapService.TilesPerRow; tileX++)
{ {
var tile = new TilePosition(tileX, tileY); var tile = new TilePosition(tileX, tileY);
if (!map.IsCurrentlyWall(tile)) if (!map.IsCurrentlyWall(tile))

View file

@ -33,16 +33,16 @@ internal sealed class TankDrawer : IDrawStep
{ {
foreach (var tank in _tanks) foreach (var tank in _tanks)
{ {
var pos = tank.Position.ToPixelPosition(); var tankPosition = tank.Position.ToPixelPosition();
var rotationVariant = (int)Math.Round(tank.Rotation) % 16; var orientation = (int)Math.Round(tank.Rotation * 16d) % 16;
for (var dy = 0; dy < MapService.TileSize; dy++) for (byte dy = 0; dy < MapService.TileSize; dy++)
for (var dx = 0; dx < MapService.TileSize; dx++) for (byte dx = 0; dx < MapService.TileSize; dx++)
{ {
if (!TankSpriteAt(dx, dy, rotationVariant)) if (!TankSpriteAt(dx, dy, orientation))
continue; continue;
var position = new PixelPosition((ushort)(pos.X + dx), (ushort)(pos.Y + dy)); var position = tankPosition.GetPixelRelative(dx, dy);
buffer[position.X, position.Y] = true; buffer[position.X, position.Y] = true;
} }
} }

View file

@ -1,3 +1,9 @@
using TanksServer.GameLogic;
namespace TanksServer.Models; namespace TanksServer.Models;
internal readonly record struct FloatPosition(double X, double Y); internal readonly struct FloatPosition(double x, double y)
{
public double X { get; } = (x + MapService.PixelsPerRow) % MapService.PixelsPerRow;
public double Y { get; } = (y + MapService.PixelsPerColumn) % MapService.PixelsPerColumn;
}

View file

@ -3,5 +3,4 @@ namespace TanksServer.Models;
internal interface IMapEntity internal interface IMapEntity
{ {
FloatPosition Position { get; set; } FloatPosition Position { get; set; }
double Rotation { get; set; }
} }

View file

@ -1,3 +1,9 @@
using TanksServer.GameLogic;
namespace TanksServer.Models; namespace TanksServer.Models;
internal record struct PixelPosition(ushort X, ushort Y); internal readonly struct PixelPosition(ushort x, ushort y)
{
public ushort X { get; } = (ushort)((x + MapService.PixelsPerRow) % MapService.PixelsPerRow);
public ushort Y { get; } = (ushort)((y + MapService.PixelsPerColumn) % MapService.PixelsPerColumn);
}

View file

@ -10,19 +10,25 @@ internal static class PositionHelpers
Debug.Assert(subX < 8); Debug.Assert(subX < 8);
Debug.Assert(subY < 8); Debug.Assert(subY < 8);
return new PixelPosition( return new PixelPosition(
X: (ushort)(position.X * MapService.TileSize + subX), x: (ushort)(position.X * MapService.TileSize + subX),
Y: (ushort)(position.Y * MapService.TileSize + subY) y: (ushort)(position.Y * MapService.TileSize + subY)
); );
} }
public static PixelPosition GetPixelRelative(this PixelPosition position, byte subX, byte subY)
{
Debug.Assert(subX < 8);
Debug.Assert(subY < 8);
return new PixelPosition((ushort)(position.X + subX), (ushort)(position.Y + subY));
}
public static PixelPosition ToPixelPosition(this FloatPosition position) => new( public static PixelPosition ToPixelPosition(this FloatPosition position) => new(
X: (ushort)((int)position.X % MapService.PixelsPerRow), x: (ushort)((int)position.X % MapService.PixelsPerRow),
Y: (ushort)((int)position.Y % MapService.PixelsPerRow) y: (ushort)((int)position.Y % MapService.PixelsPerRow)
); );
public static TilePosition ToTilePosition(this PixelPosition position) => new( public static TilePosition ToTilePosition(this PixelPosition position) => new(
X: position.X / MapService.TileSize, x: (ushort)(position.X / MapService.TileSize),
Y: position.Y / MapService.TileSize y: (ushort)(position.Y / MapService.TileSize)
); );
} }

View file

@ -1,20 +1,23 @@
using System.Diagnostics;
using TanksServer.GameLogic; using TanksServer.GameLogic;
namespace TanksServer.Models; namespace TanksServer.Models;
internal sealed class Tank(Player player, FloatPosition spawnPosition): IMapEntity internal sealed class Tank(Player player, FloatPosition spawnPosition) : IMapEntity
{ {
private double _rotation; private double _rotation;
public Player Owner { get; } = player; public Player Owner { get; } = player;
/// <summary>
/// Bounds: 0 (inclusive) .. 16 (exclusive)
/// </summary>
public double Rotation public double Rotation
{ {
get => _rotation; get => _rotation;
set => _rotation = (value + 16d) % 16d; set
{
var newRotation = (value % 1d + 1d) % 1d;
Debug.Assert(newRotation is >= 0 and < 1);
_rotation = newRotation;
}
} }
public FloatPosition Position { get; set; } = spawnPosition; public FloatPosition Position { get; set; } = spawnPosition;
@ -22,12 +25,4 @@ internal sealed class Tank(Player player, FloatPosition spawnPosition): IMapEnti
public DateTime NextShotAfter { get; set; } public DateTime NextShotAfter { get; set; }
public bool Moved { get; set; } public bool Moved { get; set; }
public (FloatPosition TopLeft, FloatPosition BottomRight) GetBounds()
{
return (
Position,
new FloatPosition(Position.X + MapService.TileSize , Position.Y + MapService.TileSize )
);
}
} }

View file

@ -1,3 +1,9 @@
using TanksServer.GameLogic;
namespace TanksServer.Models; namespace TanksServer.Models;
internal record struct TilePosition(int X, int Y); internal readonly struct TilePosition(ushort x, ushort y)
{
public ushort X { get; } = (ushort)((x + MapService.TilesPerRow) % MapService.TilesPerRow);
public ushort Y { get; } = (ushort)((y + MapService.TilesPerColumn) % MapService.TilesPerColumn);
}