player can shoot bullet (crashes game when leaving map)
This commit is contained in:
parent
dd6b0fffc1
commit
b10ccf2da8
|
@ -74,21 +74,20 @@ internal sealed class ClientScreenServer(
|
|||
|
||||
public async Task SendAsync(DisplayPixelBuffer buf)
|
||||
{
|
||||
if (await _wantedFrames.WaitAsync(TimeSpan.Zero))
|
||||
{
|
||||
_logger.LogTrace("sending");
|
||||
try
|
||||
{
|
||||
await _channel.Writer.WriteAsync(buf.Data);
|
||||
}
|
||||
catch (ChannelClosedException)
|
||||
{
|
||||
_logger.LogWarning("send failed, channel is closed");
|
||||
}
|
||||
}
|
||||
else
|
||||
if (!await _wantedFrames.WaitAsync(TimeSpan.Zero))
|
||||
{
|
||||
_logger.LogTrace("client does not want a frame yet");
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogTrace("sending");
|
||||
try
|
||||
{
|
||||
await _channel.Writer.WriteAsync(buf.Data);
|
||||
}
|
||||
catch (ChannelClosedException)
|
||||
{
|
||||
_logger.LogWarning("send failed, channel is closed");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
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];
|
||||
}
|
||||
}
|
10
TanksServer/Models/Bullet.cs
Normal file
10
TanksServer/Models/Bullet.cs
Normal file
|
@ -0,0 +1,10 @@
|
|||
namespace TanksServer.Models;
|
||||
|
||||
internal sealed class Bullet(Player tankOwner, FloatPosition position, double rotation)
|
||||
{
|
||||
public Player TankOwner { get; } = tankOwner;
|
||||
|
||||
public FloatPosition Position { get; set; } = position;
|
||||
|
||||
public double Rotation { get; set; } = rotation;
|
||||
}
|
|
@ -16,4 +16,6 @@ internal sealed class Tank(Player player, FloatPosition spawnPosition)
|
|||
}
|
||||
|
||||
public FloatPosition Position { get; set; } = spawnPosition;
|
||||
|
||||
public DateTime NextShotAfter { get; set; }
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Http;
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using TanksServer.DrawSteps;
|
||||
using TanksServer.Helpers;
|
||||
using TanksServer.Services;
|
||||
|
||||
|
@ -76,10 +77,13 @@ internal static class Program
|
|||
builder.Services.AddSingleton<MapService>();
|
||||
|
||||
builder.Services.AddHostedService<GameTickService>();
|
||||
|
||||
builder.Services.AddSingleton<BulletManager>();
|
||||
builder.Services.AddSingleton<ITickStep>(sp => sp.GetRequiredService<BulletManager>());
|
||||
|
||||
builder.Services.AddSingleton<TankManager>();
|
||||
builder.Services.AddSingleton<ITickStep>(sp => sp.GetRequiredService<TankManager>());
|
||||
|
||||
|
||||
builder.Services.AddSingleton<ControlsServer>();
|
||||
builder.Services.AddHostedService(sp => sp.GetRequiredService<ControlsServer>());
|
||||
|
||||
|
@ -95,6 +99,10 @@ internal static class Program
|
|||
|
||||
builder.Services.AddSingleton<PlayerServer>();
|
||||
|
||||
builder.Services.AddSingleton<IDrawStep, MapDrawer>();
|
||||
builder.Services.AddSingleton<IDrawStep, TankDrawer>();
|
||||
builder.Services.AddSingleton<IDrawStep, BulletDrawer>();
|
||||
|
||||
return builder.Build();
|
||||
}
|
||||
}
|
||||
|
|
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…
Reference in a new issue