player can shoot bullet (crashes game when leaving map)
This commit is contained in:
parent
dd6b0fffc1
commit
b10ccf2da8
12 changed files with 231 additions and 99 deletions
13
TanksServer/DrawSteps/BulletDrawer.cs
Normal file
13
TanksServer/DrawSteps/BulletDrawer.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using TanksServer.Helpers;
|
||||
using TanksServer.Services;
|
||||
|
||||
namespace TanksServer.DrawSteps;
|
||||
|
||||
internal sealed class BulletDrawer(BulletManager bullets): IDrawStep
|
||||
{
|
||||
public void Draw(DisplayPixelBuffer buffer)
|
||||
{
|
||||
foreach (var bullet in bullets.GetAll())
|
||||
buffer.Pixels[bullet.Position.ToPixelPosition().GetPixelIndex()] = true;
|
||||
}
|
||||
}
|
23
TanksServer/DrawSteps/DrawHelpers.cs
Normal file
23
TanksServer/DrawSteps/DrawHelpers.cs
Normal file
|
@ -0,0 +1,23 @@
|
|||
using System.Diagnostics;
|
||||
using TanksServer.Models;
|
||||
using TanksServer.Services;
|
||||
|
||||
namespace TanksServer.DrawSteps;
|
||||
|
||||
internal static class DrawHelpers
|
||||
{
|
||||
public static int GetPixelIndex(this PixelPosition position)
|
||||
{
|
||||
return position.Y * MapService.PixelsPerRow + position.X;
|
||||
}
|
||||
|
||||
public static PixelPosition GetPixel(this TilePosition position, byte subX, byte subY)
|
||||
{
|
||||
Debug.Assert(subX < 8);
|
||||
Debug.Assert(subY < 8);
|
||||
return new PixelPosition(
|
||||
X: position.X * MapService.TileSize + subX,
|
||||
Y: position.Y * MapService.TileSize + subY
|
||||
);
|
||||
}
|
||||
}
|
8
TanksServer/DrawSteps/IDrawStep.cs
Normal file
8
TanksServer/DrawSteps/IDrawStep.cs
Normal file
|
@ -0,0 +1,8 @@
|
|||
using TanksServer.Helpers;
|
||||
|
||||
namespace TanksServer.DrawSteps;
|
||||
|
||||
internal interface IDrawStep
|
||||
{
|
||||
void Draw(DisplayPixelBuffer buffer);
|
||||
}
|
26
TanksServer/DrawSteps/MapDrawer.cs
Normal file
26
TanksServer/DrawSteps/MapDrawer.cs
Normal file
|
@ -0,0 +1,26 @@
|
|||
using TanksServer.Helpers;
|
||||
using TanksServer.Models;
|
||||
using TanksServer.Services;
|
||||
|
||||
namespace TanksServer.DrawSteps;
|
||||
|
||||
internal sealed class MapDrawer(MapService map) : IDrawStep
|
||||
{
|
||||
public void Draw(DisplayPixelBuffer buffer)
|
||||
{
|
||||
for (var tileY = 0; tileY < MapService.TilesPerColumn; tileY++)
|
||||
for (var 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 index = tile.GetPixel(pixelInTileX, pixelInTileY).GetPixelIndex();
|
||||
buffer.Pixels[index] = pixelInTileX % 2 == pixelInTileY % 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
62
TanksServer/DrawSteps/TankDrawer.cs
Normal file
62
TanksServer/DrawSteps/TankDrawer.cs
Normal file
|
@ -0,0 +1,62 @@
|
|||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using TanksServer.Helpers;
|
||||
using TanksServer.Services;
|
||||
|
||||
namespace TanksServer.DrawSteps;
|
||||
|
||||
internal sealed class TankDrawer : IDrawStep
|
||||
{
|
||||
private readonly TankManager _tanks;
|
||||
private readonly bool[] _tankSprite;
|
||||
private readonly int _tankSpriteWidth;
|
||||
|
||||
public TankDrawer(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(DisplayPixelBuffer buffer)
|
||||
{
|
||||
foreach (var tank in _tanks)
|
||||
{
|
||||
var pos = tank.Position.ToPixelPosition();
|
||||
var rotationVariant = (int)Math.Floor(tank.Rotation);
|
||||
for (var dy = 0; dy < MapService.TileSize; dy++)
|
||||
{
|
||||
var rowStartIndex = (pos.Y + dy) * MapService.PixelsPerRow;
|
||||
|
||||
for (var dx = 0; dx < MapService.TileSize; dx++)
|
||||
{
|
||||
if (!TankSpriteAt(dx, dy, rotationVariant))
|
||||
continue;
|
||||
|
||||
var i = rowStartIndex + pos.X + dx;
|
||||
buffer.Pixels[i] = 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];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue