position is now center, socket improvements
This commit is contained in:
parent
40eba7a7c7
commit
d4d0abd013
5 changed files with 129 additions and 101 deletions
|
|
@ -1,16 +0,0 @@
|
|||
using DisplayCommands;
|
||||
using TanksServer.GameLogic;
|
||||
|
||||
namespace TanksServer.Graphics;
|
||||
|
||||
internal sealed class BulletDrawer(BulletManager bullets) : IDrawStep
|
||||
{
|
||||
public void Draw(PixelGrid buffer)
|
||||
{
|
||||
foreach (var bullet in bullets.GetAll())
|
||||
{
|
||||
var pos = bullet.Position.ToPixelPosition();
|
||||
buffer[pos.X, pos.Y] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
13
TanksServer/Graphics/DrawBulletsStep.cs
Normal file
13
TanksServer/Graphics/DrawBulletsStep.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
using DisplayCommands;
|
||||
using TanksServer.GameLogic;
|
||||
|
||||
namespace TanksServer.Graphics;
|
||||
|
||||
internal sealed class DrawBulletsStep(BulletManager bullets) : IDrawStep
|
||||
{
|
||||
public void Draw(PixelGrid buffer)
|
||||
{
|
||||
foreach (var position in bullets.GetAll().Select(b => b.Position.ToPixelPosition()))
|
||||
buffer[position.X, position.Y] = true;
|
||||
}
|
||||
}
|
||||
25
TanksServer/Graphics/DrawMapStep.cs
Normal file
25
TanksServer/Graphics/DrawMapStep.cs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
using DisplayCommands;
|
||||
using TanksServer.GameLogic;
|
||||
|
||||
namespace TanksServer.Graphics;
|
||||
|
||||
internal sealed class DrawMapStep(MapService map) : IDrawStep
|
||||
{
|
||||
public void Draw(PixelGrid buffer)
|
||||
{
|
||||
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))
|
||||
continue;
|
||||
|
||||
for (byte pixelInTileY = 0; pixelInTileY < MapService.TileSize; pixelInTileY++)
|
||||
for (byte pixelInTileX = 0; pixelInTileX < MapService.TileSize; pixelInTileX++)
|
||||
{
|
||||
var position = tile.GetPixelRelative(pixelInTileX, pixelInTileY);
|
||||
buffer[position.X, position.Y] = pixelInTileX % 2 == pixelInTileY % 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
57
TanksServer/Graphics/DrawTanksStep.cs
Normal file
57
TanksServer/Graphics/DrawTanksStep.cs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
using DisplayCommands;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using TanksServer.GameLogic;
|
||||
|
||||
namespace TanksServer.Graphics;
|
||||
|
||||
internal sealed class DrawTanksStep : IDrawStep
|
||||
{
|
||||
private readonly TankManager _tanks;
|
||||
private readonly bool[] _tankSprite;
|
||||
private readonly int _tankSpriteWidth;
|
||||
|
||||
public DrawTanksStep(TankManager tanks)
|
||||
{
|
||||
_tanks = tanks;
|
||||
|
||||
using var tankImage = Image.Load<Rgba32>("assets/tank.png");
|
||||
_tankSprite = new bool[tankImage.Height * tankImage.Width];
|
||||
|
||||
var whitePixel = new Rgba32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);
|
||||
var i = 0;
|
||||
for (var y = 0; y < tankImage.Height; y++)
|
||||
for (var x = 0; x < tankImage.Width; x++, i++)
|
||||
_tankSprite[i] = tankImage[x, y] == whitePixel;
|
||||
|
||||
_tankSpriteWidth = tankImage.Width;
|
||||
}
|
||||
|
||||
public void Draw(PixelGrid buffer)
|
||||
{
|
||||
foreach (var tank in _tanks)
|
||||
{
|
||||
var tankPosition = tank.Bounds.TopLeft;
|
||||
var orientation = (int)Math.Round(tank.Rotation * 16d) % 16;
|
||||
|
||||
for (byte dy = 0; dy < MapService.TileSize; dy++)
|
||||
for (byte dx = 0; dx < MapService.TileSize; dx++)
|
||||
{
|
||||
if (!TankSpriteAt(dx, dy, orientation))
|
||||
continue;
|
||||
|
||||
var position = tankPosition.GetPixelRelative(dx, dy);
|
||||
buffer[position.X, position.Y] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool TankSpriteAt(int dx, int dy, int tankRotation)
|
||||
{
|
||||
var x = tankRotation % 4 * (MapService.TileSize + 1);
|
||||
var y = (int)Math.Floor(tankRotation / 4d) * (MapService.TileSize + 1);
|
||||
var index = (y + dy) * _tankSpriteWidth + x + dx;
|
||||
|
||||
return _tankSprite[index];
|
||||
}
|
||||
}
|
||||
21
TanksServer/Graphics/GeneratePixelsTickStep.cs
Normal file
21
TanksServer/Graphics/GeneratePixelsTickStep.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using DisplayCommands;
|
||||
using TanksServer.GameLogic;
|
||||
|
||||
namespace TanksServer.Graphics;
|
||||
|
||||
internal sealed class GeneratePixelsTickStep(
|
||||
IEnumerable<IDrawStep> drawSteps,
|
||||
LastFinishedFrameProvider lastFrameProvider
|
||||
) : ITickStep
|
||||
{
|
||||
private readonly List<IDrawStep> _drawSteps = drawSteps.ToList();
|
||||
|
||||
public Task TickAsync()
|
||||
{
|
||||
var drawGrid = new PixelGrid(MapService.PixelsPerRow, MapService.PixelsPerColumn);
|
||||
foreach (var step in _drawSteps)
|
||||
step.Draw(drawGrid);
|
||||
lastFrameProvider.LastFrame = drawGrid;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in a new issue