infinite map
This commit is contained in:
parent
7213318838
commit
461a9139c2
|
@ -8,6 +8,7 @@ public sealed class Cp437Grid(ushort width, ushort height)
|
|||
private readonly ByteGrid _byteGrid = new(width, height);
|
||||
|
||||
public ushort Height { get; } = height;
|
||||
|
||||
public ushort Width { get; } = width;
|
||||
|
||||
internal Memory<byte> Data => _byteGrid.Data;
|
||||
|
|
|
@ -6,9 +6,9 @@ public interface IDisplayConnection
|
|||
|
||||
ValueTask SendCp437DataAsync(ushort x, ushort y, Cp437Grid grid);
|
||||
|
||||
ValueTask SendCharBrightnessAsync(ushort x, ushort y, ByteGrid luma);
|
||||
|
||||
ValueTask SendBrightnessAsync(byte brightness);
|
||||
|
||||
ValueTask SendCharBrightnessAsync(ushort x, ushort y, ByteGrid luma);
|
||||
|
||||
ValueTask SendHardResetAsync();
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace DisplayCommands.Internals;
|
||||
|
||||
internal enum DisplayCommand: ushort
|
||||
internal enum DisplayCommand : ushort
|
||||
{
|
||||
Clear = 0x0002,
|
||||
Cp437Data = 0x0003,
|
||||
|
@ -8,10 +8,10 @@ internal enum DisplayCommand: ushort
|
|||
Brightness = 0x0007,
|
||||
HardReset = 0x000b,
|
||||
FadeOut = 0x000d,
|
||||
BitmapLegacy = 0x0010,
|
||||
[Obsolete("ignored by display code")] BitmapLegacy = 0x0010,
|
||||
BitmapLinear = 0x0012,
|
||||
BitmapLinearWin = 0x0013,
|
||||
BitmapLinearAnd = 0x0014,
|
||||
BitmapLinearOr = 0x0015,
|
||||
BitmapLinearXor = 0x0016,
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
));
|
||||
}
|
||||
}
|
|
@ -12,6 +12,7 @@ internal sealed class DrawStateToFrame(
|
|||
|
||||
public Task TickAsync()
|
||||
{
|
||||
// TODO: fix race condition with shared buffer access
|
||||
_drawGrid.Clear();
|
||||
foreach (var step in _drawSteps)
|
||||
step.Draw(_drawGrid);
|
||||
|
|
|
@ -7,8 +7,8 @@ internal sealed class MapDrawer(MapService map) : IDrawStep
|
|||
{
|
||||
public void Draw(PixelGrid buffer)
|
||||
{
|
||||
for (var tileY = 0; tileY < MapService.TilesPerColumn; tileY++)
|
||||
for (var tileX = 0; tileX < MapService.TilesPerRow; tileX++)
|
||||
for (ushort tileY = 0; tileY < MapService.TilesPerColumn; tileY++)
|
||||
for (ushort tileX = 0; tileX < MapService.TilesPerRow; tileX++)
|
||||
{
|
||||
var tile = new TilePosition(tileX, tileY);
|
||||
if (!map.IsCurrentlyWall(tile))
|
||||
|
|
|
@ -33,16 +33,16 @@ internal sealed class TankDrawer : IDrawStep
|
|||
{
|
||||
foreach (var tank in _tanks)
|
||||
{
|
||||
var pos = tank.Position.ToPixelPosition();
|
||||
var rotationVariant = (int)Math.Round(tank.Rotation) % 16;
|
||||
var tankPosition = tank.Position.ToPixelPosition();
|
||||
var orientation = (int)Math.Round(tank.Rotation * 16d) % 16;
|
||||
|
||||
for (var dy = 0; dy < MapService.TileSize; dy++)
|
||||
for (var dx = 0; dx < MapService.TileSize; dx++)
|
||||
for (byte dy = 0; dy < MapService.TileSize; dy++)
|
||||
for (byte dx = 0; dx < MapService.TileSize; dx++)
|
||||
{
|
||||
if (!TankSpriteAt(dx, dy, rotationVariant))
|
||||
if (!TankSpriteAt(dx, dy, orientation))
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,9 @@
|
|||
using TanksServer.GameLogic;
|
||||
|
||||
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;
|
||||
}
|
|
@ -3,5 +3,4 @@ namespace TanksServer.Models;
|
|||
internal interface IMapEntity
|
||||
{
|
||||
FloatPosition Position { get; set; }
|
||||
double Rotation { get; set; }
|
||||
}
|
|
@ -1,3 +1,9 @@
|
|||
using TanksServer.GameLogic;
|
||||
|
||||
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);
|
||||
}
|
|
@ -10,19 +10,25 @@ internal static class PositionHelpers
|
|||
Debug.Assert(subX < 8);
|
||||
Debug.Assert(subY < 8);
|
||||
return new PixelPosition(
|
||||
X: (ushort)(position.X * MapService.TileSize + subX),
|
||||
Y: (ushort)(position.Y * MapService.TileSize + subY)
|
||||
x: (ushort)(position.X * MapService.TileSize + subX),
|
||||
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(
|
||||
X: (ushort)((int)position.X % MapService.PixelsPerRow),
|
||||
Y: (ushort)((int)position.Y % MapService.PixelsPerRow)
|
||||
x: (ushort)((int)position.X % MapService.PixelsPerRow),
|
||||
y: (ushort)((int)position.Y % MapService.PixelsPerRow)
|
||||
);
|
||||
|
||||
public static TilePosition ToTilePosition(this PixelPosition position) => new(
|
||||
X: position.X / MapService.TileSize,
|
||||
Y: position.Y / MapService.TileSize
|
||||
x: (ushort)(position.X / MapService.TileSize),
|
||||
y: (ushort)(position.Y / MapService.TileSize)
|
||||
);
|
||||
}
|
|
@ -1,20 +1,23 @@
|
|||
using System.Diagnostics;
|
||||
using TanksServer.GameLogic;
|
||||
|
||||
namespace TanksServer.Models;
|
||||
|
||||
internal sealed class Tank(Player player, FloatPosition spawnPosition): IMapEntity
|
||||
internal sealed class Tank(Player player, FloatPosition spawnPosition) : IMapEntity
|
||||
{
|
||||
private double _rotation;
|
||||
|
||||
public Player Owner { get; } = player;
|
||||
|
||||
/// <summary>
|
||||
/// Bounds: 0 (inclusive) .. 16 (exclusive)
|
||||
/// </summary>
|
||||
public double 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;
|
||||
|
@ -22,12 +25,4 @@ internal sealed class Tank(Player player, FloatPosition spawnPosition): IMapEnti
|
|||
public DateTime NextShotAfter { 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 )
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,3 +1,9 @@
|
|||
using TanksServer.GameLogic;
|
||||
|
||||
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);
|
||||
}
|
Loading…
Reference in a new issue