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
32
TanksServer/Services/BulletManager.cs
Normal file
32
TanksServer/Services/BulletManager.cs
Normal file
|
@ -0,0 +1,32 @@
|
|||
using System.Collections;
|
||||
using TanksServer.Models;
|
||||
|
||||
namespace TanksServer.Services;
|
||||
|
||||
internal sealed class BulletManager : ITickStep
|
||||
{
|
||||
private readonly HashSet<Bullet> _bullets = new();
|
||||
|
||||
public void Spawn(Bullet bullet) => _bullets.Add(bullet);
|
||||
|
||||
public Task TickAsync()
|
||||
{
|
||||
foreach (var bullet in _bullets)
|
||||
{
|
||||
MoveBullet(bullet);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static void MoveBullet(Bullet bullet)
|
||||
{
|
||||
var angle = bullet.Rotation / 16 * 2 * Math.PI;
|
||||
bullet.Position = new FloatPosition(
|
||||
X: bullet.Position.X + Math.Sin(angle) * 3,
|
||||
Y: bullet.Position.Y - Math.Cos(angle) * 3
|
||||
);
|
||||
}
|
||||
|
||||
public IEnumerable<Bullet> GetAll() => _bullets;
|
||||
}
|
|
@ -3,38 +3,17 @@ using System.Net.Mime;
|
|||
using Microsoft.Extensions.Logging;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using TanksServer.DrawSteps;
|
||||
using TanksServer.Helpers;
|
||||
using TanksServer.Models;
|
||||
|
||||
namespace TanksServer.Services;
|
||||
|
||||
internal sealed class PixelDrawer : ITickStep
|
||||
internal sealed class PixelDrawer(IEnumerable<IDrawStep> drawSteps) : ITickStep
|
||||
{
|
||||
private const uint GameFieldPixelCount = MapService.PixelsPerRow * MapService.PixelsPerColumn;
|
||||
private DisplayPixelBuffer? _lastFrame;
|
||||
private readonly MapService _map;
|
||||
private readonly TankManager _tanks;
|
||||
private readonly bool[] _tankSprite;
|
||||
private readonly int _tankSpriteWidth;
|
||||
|
||||
public PixelDrawer(MapService map, TankManager tanks, ILogger<PixelDrawer> logger)
|
||||
{
|
||||
_map = map;
|
||||
_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;
|
||||
}
|
||||
private readonly List<IDrawStep> _drawSteps = drawSteps.ToList();
|
||||
|
||||
public DisplayPixelBuffer LastFrame
|
||||
{
|
||||
|
@ -45,66 +24,12 @@ internal sealed class PixelDrawer : ITickStep
|
|||
public Task TickAsync()
|
||||
{
|
||||
var buffer = CreateGameFieldPixelBuffer();
|
||||
DrawMap(buffer);
|
||||
DrawTanks(buffer);
|
||||
foreach (var step in _drawSteps)
|
||||
step.Draw(buffer);
|
||||
LastFrame = buffer;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void DrawMap(DisplayPixelBuffer buf)
|
||||
{
|
||||
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;
|
||||
|
||||
var absoluteTilePixelY = tileY * MapService.TileSize;
|
||||
for (var pixelInTileY = 0; pixelInTileY < MapService.TileSize; pixelInTileY++)
|
||||
{
|
||||
var absoluteRowStartPixelIndex = (absoluteTilePixelY + pixelInTileY) * MapService.PixelsPerRow
|
||||
+ tileX * MapService.TileSize;
|
||||
for (var pixelInTileX = 0; pixelInTileX < MapService.TileSize; pixelInTileX++)
|
||||
buf.Pixels[absoluteRowStartPixelIndex + pixelInTileX] = pixelInTileX % 2 == pixelInTileY % 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawTanks(DisplayPixelBuffer buf)
|
||||
{
|
||||
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;
|
||||
buf.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;
|
||||
|
||||
if (index < 0 || index > _tankSprite.Length)
|
||||
Debugger.Break();
|
||||
|
||||
return _tankSprite[index];
|
||||
}
|
||||
|
||||
|
||||
private static DisplayPixelBuffer CreateGameFieldPixelBuffer()
|
||||
{
|
||||
var data = new byte[10 + GameFieldPixelCount / 8];
|
||||
|
|
|
@ -6,8 +6,12 @@ using TanksServer.Models;
|
|||
|
||||
namespace TanksServer.Services;
|
||||
|
||||
internal sealed class TankManager(ILogger<TankManager> logger, IOptions<TanksConfiguration> options, MapService map)
|
||||
: ITickStep, IEnumerable<Tank>
|
||||
internal sealed class TankManager(
|
||||
ILogger<TankManager> logger,
|
||||
IOptions<TanksConfiguration> options,
|
||||
MapService map,
|
||||
BulletManager bullets
|
||||
) : ITickStep, IEnumerable<Tank>
|
||||
{
|
||||
private readonly ConcurrentBag<Tank> _tanks = new();
|
||||
private readonly TanksConfiguration _config = options.Value;
|
||||
|
@ -22,7 +26,9 @@ internal sealed class TankManager(ILogger<TankManager> logger, IOptions<TanksCon
|
|||
{
|
||||
foreach (var tank in _tanks)
|
||||
{
|
||||
TryMoveTank(tank);
|
||||
if (TryMoveTank(tank))
|
||||
continue;
|
||||
Shoot(tank);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
@ -69,7 +75,7 @@ internal sealed class TankManager(ILogger<TankManager> logger, IOptions<TanksCon
|
|||
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) };
|
||||
if (positions.Any(map.IsCurrentlyWall))
|
||||
return false;
|
||||
|
@ -78,6 +84,24 @@ internal sealed class TankManager(ILogger<TankManager> logger, IOptions<TanksCon
|
|||
return true;
|
||||
}
|
||||
|
||||
private void Shoot(Tank tank)
|
||||
{
|
||||
if (!tank.Owner.Controls.Shoot)
|
||||
return;
|
||||
if (tank.NextShotAfter >= DateTime.Now)
|
||||
return;
|
||||
|
||||
tank.NextShotAfter = DateTime.Now.AddMilliseconds(_config.ShootDelayMs);
|
||||
|
||||
var angle = tank.Rotation / 16 * 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
|
||||
);
|
||||
|
||||
bullets.Spawn(new Bullet(tank.Owner, position, tank.Rotation));
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
public IEnumerator<Tank> GetEnumerator() => _tanks.GetEnumerator();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue