another data view

This commit is contained in:
Vinzenz Schroeter 2024-04-10 22:39:33 +02:00
parent 85ae3e302c
commit 91ab911f9c
9 changed files with 44 additions and 23 deletions

View file

@ -8,6 +8,6 @@ internal sealed class BulletDrawer(BulletManager bullets): IDrawStep
public void Draw(PixelDisplayBufferView buffer) public void Draw(PixelDisplayBufferView buffer)
{ {
foreach (var bullet in bullets.GetAll()) foreach (var bullet in bullets.GetAll())
buffer.Pixels[bullet.Position.ToPixelPosition().ToPixelIndex()] = true; buffer.Pixels[bullet.Position.ToPixelPosition()] = true;
} }
} }

View file

@ -17,8 +17,8 @@ internal sealed class MapDrawer(MapService map) : IDrawStep
for (byte pixelInTileY = 0; pixelInTileY < MapService.TileSize; pixelInTileY++) for (byte pixelInTileY = 0; pixelInTileY < MapService.TileSize; pixelInTileY++)
for (byte pixelInTileX = 0; pixelInTileX < MapService.TileSize; pixelInTileX++) for (byte pixelInTileX = 0; pixelInTileX < MapService.TileSize; pixelInTileX++)
{ {
var index = tile.GetPixelRelative(pixelInTileX, pixelInTileY).ToPixelIndex(); var position = tile.GetPixelRelative(pixelInTileX, pixelInTileY);
buffer.Pixels[index] = pixelInTileX % 2 == pixelInTileY % 2; buffer.Pixels[position] = pixelInTileX % 2 == pixelInTileY % 2;
} }
} }
} }

View file

@ -35,18 +35,15 @@ internal sealed class TankDrawer : IDrawStep
{ {
var pos = tank.Position.ToPixelPosition(); var pos = tank.Position.ToPixelPosition();
var rotationVariant = (int)Math.Round(tank.Rotation) % 16; var rotationVariant = (int)Math.Round(tank.Rotation) % 16;
for (var dy = 0; dy < MapService.TileSize; dy++)
{
var rowStartIndex = (pos.Y + dy) * MapService.PixelsPerRow;
for (var dy = 0; dy < MapService.TileSize; dy++)
for (var dx = 0; dx < MapService.TileSize; dx++) for (var dx = 0; dx < MapService.TileSize; dx++)
{ {
if (!TankSpriteAt(dx, dy, rotationVariant)) if (!TankSpriteAt(dx, dy, rotationVariant))
continue; continue;
var i = rowStartIndex + pos.X + dx; var position = new PixelPosition(pos.X + dx, pos.Y + dy);
buffer.Pixels[i] = true; buffer.Pixels[position] = true;
}
} }
} }
} }

View file

@ -5,8 +5,6 @@ namespace TanksServer.Models;
internal static class PositionHelpers internal static class PositionHelpers
{ {
public static int ToPixelIndex(this PixelPosition position) => position.Y * MapService.PixelsPerRow + position.X;
public static PixelPosition GetPixelRelative(this TilePosition position, byte subX, byte subY) public static PixelPosition GetPixelRelative(this TilePosition position, byte subX, byte subY)
{ {
Debug.Assert(subX < 8); Debug.Assert(subX < 8);

View file

@ -0,0 +1,23 @@
using System.Diagnostics;
namespace TanksServer.ServicePointDisplay;
internal sealed class FixedSizeBitGridView(Memory<byte> data, int columns, int rows)
{
private readonly FixedSizeBitRowView _bits = new(data);
public bool this[PixelPosition position]
{
get => _bits[ToPixelIndex(position)];
set => _bits[ToPixelIndex(position)] = value;
}
private int ToPixelIndex(PixelPosition position)
{
Debug.Assert(position.X < columns);
Debug.Assert(position.Y < rows);
var index = position.Y * columns + position.X;
ArgumentOutOfRangeException.ThrowIfNegative(index, nameof(position));
return index;
}
}

View file

@ -2,7 +2,7 @@ using System.Collections;
namespace TanksServer.ServicePointDisplay; namespace TanksServer.ServicePointDisplay;
internal sealed class FixedSizeBitFieldView(Memory<byte> data) : IList<bool> internal sealed class FixedSizeBitRowView(Memory<byte> data) : IList<bool>
{ {
public int Count => data.Length * 8; public int Count => data.Length * 8;
public bool IsReadOnly => false; public bool IsReadOnly => false;

View file

@ -1,20 +1,22 @@
using TanksServer.GameLogic;
namespace TanksServer.ServicePointDisplay; namespace TanksServer.ServicePointDisplay;
internal sealed class PixelDisplayBufferView : DisplayBufferView internal sealed class PixelDisplayBufferView : DisplayBufferView
{ {
private PixelDisplayBufferView(byte[] data) : base(data) private PixelDisplayBufferView(byte[] data, int columns, int pixelRows) : base(data)
{ {
Pixels = new FixedSizeBitFieldView(Data.AsMemory(10)); Pixels = new FixedSizeBitGridView(Data.AsMemory(10), columns, pixelRows);
} }
// ReSharper disable once CollectionNeverQueried.Global (setting values in collection updates underlying byte array) // ReSharper disable once CollectionNeverQueried.Global (setting values in collection updates underlying byte array)
public FixedSizeBitFieldView Pixels { get; } public FixedSizeBitGridView Pixels { get; }
public static PixelDisplayBufferView New(ushort x, ushort y, ushort widthInTiles, ushort pixelRows) public static PixelDisplayBufferView New(ushort x, ushort y, ushort widthInTiles, ushort pixelRows)
{ {
// 10 bytes header, one byte per tile row (with one bit each pixel) after that // 10 bytes header, one byte per tile row (with one bit each pixel) after that
var size = 10 + widthInTiles * pixelRows; var size = 10 + widthInTiles * pixelRows;
return new PixelDisplayBufferView(new byte[size]) return new PixelDisplayBufferView(new byte[size], widthInTiles * MapService.TileSize, pixelRows)
{ {
Mode = 19, Mode = 19,
TileX = x, TileX = x,

View file

@ -90,10 +90,10 @@ internal sealed class SendToServicePointDisplay : ITickStep, IDisposable
foreach (var p in playersToDisplay) foreach (var p in playersToDisplay)
{ {
var score = p.Kills.ToString(); var score = p.Kills.ToString();
var nameLength = ScoresWidth - score.Length; var nameLength = Math.Min(p.Name.Length, ScoresWidth - score.Length - 1);
var name = p.Name[..nameLength]; var name = p.Name[..nameLength];
var spaces = new string(' ', nameLength - name.Length + 1); var spaces = new string(' ', ScoresWidth - score.Length - nameLength);
_scoresBuffer.Rows[row] = name + spaces + score; _scoresBuffer.Rows[row] = name + spaces + score;
row++; row++;

View file

@ -3,7 +3,8 @@
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",
"Microsoft.AspNetCore": "Warning", "Microsoft.AspNetCore": "Warning",
"TanksServer": "Debug" "TanksServer": "Debug",
"Microsoft.AspNetCore.HttpLogging": "Debug"
} }
}, },
"AllowedHosts": "*", "AllowedHosts": "*",