bullets collide with walls
This commit is contained in:
parent
b10ccf2da8
commit
898a9cedc1
|
@ -8,6 +8,6 @@ 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;
|
||||
buffer.Pixels[bullet.Position.ToPixelPosition().ToPixelIndex()] = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
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
|
||||
);
|
||||
}
|
||||
}
|
|
@ -18,7 +18,7 @@ internal sealed class MapDrawer(MapService map) : IDrawStep
|
|||
for (byte pixelInTileY = 0; pixelInTileY < MapService.TileSize; pixelInTileY++)
|
||||
for (byte pixelInTileX = 0; pixelInTileX < MapService.TileSize; pixelInTileX++)
|
||||
{
|
||||
var index = tile.GetPixel(pixelInTileX, pixelInTileY).GetPixelIndex();
|
||||
var index = tile.GetPixelRelative(pixelInTileX, pixelInTileY).ToPixelIndex();
|
||||
buffer.Pixels[index] = pixelInTileX % 2 == pixelInTileY % 2;
|
||||
}
|
||||
}
|
||||
|
|
35
TanksServer/Helpers/PositionHelpers.cs
Normal file
35
TanksServer/Helpers/PositionHelpers.cs
Normal file
|
@ -0,0 +1,35 @@
|
|||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using TanksServer.Models;
|
||||
using TanksServer.Services;
|
||||
|
||||
namespace TanksServer.Helpers;
|
||||
|
||||
internal static class PositionHelpers
|
||||
{
|
||||
public static int ToPixelIndex(this PixelPosition position)
|
||||
{
|
||||
return position.Y * MapService.PixelsPerRow + position.X;
|
||||
}
|
||||
|
||||
public static PixelPosition GetPixelRelative(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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public static PixelPosition ToPixelPosition(this FloatPosition position) => new(
|
||||
X: (int)position.X % MapService.PixelsPerRow,
|
||||
Y: (int)position.Y % MapService.PixelsPerRow
|
||||
);
|
||||
|
||||
public static TilePosition ToTilePosition(this PixelPosition position) => new(
|
||||
X: position.X / MapService.TileSize,
|
||||
Y: position.Y / MapService.TileSize
|
||||
);
|
||||
}
|
|
@ -4,5 +4,4 @@ namespace TanksServer.Models;
|
|||
|
||||
internal readonly record struct FloatPosition(double X, double Y)
|
||||
{
|
||||
public PixelPosition ToPixelPosition() => new((int)X % MapService.PixelsPerRow, (int)Y % MapService.PixelsPerRow);
|
||||
}
|
||||
|
|
|
@ -1,21 +1,29 @@
|
|||
using System.Collections;
|
||||
using TanksServer.Helpers;
|
||||
using TanksServer.Models;
|
||||
|
||||
namespace TanksServer.Services;
|
||||
|
||||
internal sealed class BulletManager : ITickStep
|
||||
internal sealed class BulletManager(MapService map) : ITickStep
|
||||
{
|
||||
private readonly HashSet<Bullet> _bullets = new();
|
||||
|
||||
public void Spawn(Bullet bullet) => _bullets.Add(bullet);
|
||||
|
||||
public IEnumerable<Bullet> GetAll() => _bullets;
|
||||
|
||||
public Task TickAsync()
|
||||
{
|
||||
HashSet<Bullet> bulletsToRemove = new();
|
||||
foreach (var bullet in _bullets)
|
||||
{
|
||||
MoveBullet(bullet);
|
||||
|
||||
if (BulletHitsWall(bullet))
|
||||
bulletsToRemove.Add(bullet);
|
||||
}
|
||||
|
||||
_bullets.RemoveWhere(b => bulletsToRemove.Contains(b));
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
@ -28,5 +36,8 @@ internal sealed class BulletManager : ITickStep
|
|||
);
|
||||
}
|
||||
|
||||
public IEnumerable<Bullet> GetAll() => _bullets;
|
||||
private bool BulletHitsWall(Bullet bullet)
|
||||
{
|
||||
return map.IsCurrentlyWall(bullet.Position.ToPixelPosition().ToTilePosition());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue