check walls for spawn

This commit is contained in:
Vinzenz Schroeter 2024-04-14 22:45:51 +02:00
parent 6bed7d918f
commit 359e0235f9
5 changed files with 34 additions and 22 deletions

View file

@ -9,5 +9,5 @@ internal sealed class CollideBulletsWithMap(BulletManager bullets, MapService ma
} }
private bool BulletHitsWall(Bullet bullet) => private bool BulletHitsWall(Bullet bullet) =>
map.Current.IsCurrentlyWall(bullet.Position.ToPixelPosition()); map.Current.IsWall(bullet.Position.ToPixelPosition());
} }

View file

@ -25,21 +25,19 @@ internal sealed class MapService
private static Map LoadMapPng(string file) private static Map LoadMapPng(string file)
{ {
var dict = new Dictionary<PixelPosition, bool>();
using var image = Image.Load<Rgba32>(file); using var image = Image.Load<Rgba32>(file);
if (image.Width != PixelsPerRow || image.Height != PixelsPerColumn) if (image.Width != PixelsPerRow || image.Height != PixelsPerColumn)
throw new FileLoadException($"invalid image size in file {file}"); throw new FileLoadException($"invalid image size in file {file}");
var whitePixel = new Rgba32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue); var whitePixel = new Rgba32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);
var walls = new bool[PixelsPerRow, PixelsPerColumn];
for (var y = 0; y < image.Height; y++) for (var y = 0; y < image.Height; y++)
for (var x = 0; x < image.Width; x++) for (var x = 0; x < image.Width; x++)
{ walls[x, y] = image[x, y] == whitePixel;
if (image[x, y] == whitePixel)
dict[new PixelPosition(x, y)] = true;
}
return new Map(dict); return new Map(walls);
} }
private static Map LoadMapString(string file) private static Map LoadMapString(string file)
@ -48,28 +46,44 @@ internal sealed class MapService
if (map.Length != TilesPerColumn * TilesPerRow) if (map.Length != TilesPerColumn * TilesPerRow)
throw new FileLoadException($"cannot load map {file}: invalid length"); throw new FileLoadException($"cannot load map {file}: invalid length");
var dict = new Dictionary<PixelPosition, bool>(); var walls = new bool[PixelsPerRow, PixelsPerColumn];
for (ushort tileY = 0; tileY < MapService.TilesPerColumn; tileY++) for (ushort tileX = 0; tileX < TilesPerRow; tileX++)
for (ushort tileX = 0; tileX < MapService.TilesPerRow; tileX++) for (ushort tileY = 0; tileY < TilesPerColumn; tileY++)
{ {
var tile = new TilePosition(tileX, tileY); var tile = new TilePosition(tileX, tileY);
if (map[tileX + tileY * TilesPerRow] != '#') if (map[tileX + tileY * TilesPerRow] != '#')
continue; continue;
for (byte pixelInTileY = 0; pixelInTileY < MapService.TileSize; pixelInTileY++) for (byte pixelInTileX = 0; pixelInTileX < TileSize; pixelInTileX++)
for (byte pixelInTileX = 0; pixelInTileX < MapService.TileSize; pixelInTileX++) for (byte pixelInTileY = 0; pixelInTileY < TileSize; pixelInTileY++)
{ {
var pixel = tile.ToPixelPosition().GetPixelRelative(pixelInTileX, pixelInTileY); var (x, y) = tile.ToPixelPosition().GetPixelRelative(pixelInTileX, pixelInTileY);
dict[pixel] = true; walls[x, y] = true;
} }
} }
return new Map(dict); return new Map(walls);
} }
} }
internal sealed class Map(IReadOnlyDictionary<PixelPosition, bool> walls) internal sealed class Map(bool[,] walls)
{ {
public bool IsCurrentlyWall(PixelPosition position) => walls.TryGetValue(position, out var value) && value; public bool IsWall(int x, int y) => walls[x, y];
public bool IsWall(PixelPosition position) => walls[position.X, position.Y];
public bool IsWall(TilePosition position)
{
var pixel = position.ToPixelPosition();
for (short dx = 1; dx < MapService.TilesPerRow - 1; dx++)
for (short dy = 1; dy < MapService.TilesPerColumn - 1; dy++)
{
if (IsWall(pixel.GetPixelRelative(dx, dy)))
return true;
}
return false;
}
} }

View file

@ -69,7 +69,7 @@ internal sealed class MoveTanks(
for (short x = 0; x < MapService.TileSize; x++) for (short x = 0; x < MapService.TileSize; x++)
{ {
var pixelToCheck = topLeft.GetPixelRelative(x, y); var pixelToCheck = topLeft.GetPixelRelative(x, y);
if (map.Current.IsCurrentlyWall(pixelToCheck)) if (map.Current.IsWall(pixelToCheck))
return true; return true;
} }

View file

@ -28,9 +28,7 @@ internal sealed class SpawnNewTanks(
for (ushort y = 1; y < MapService.TilesPerColumn - 1; y++) for (ushort y = 1; y < MapService.TilesPerColumn - 1; y++)
{ {
var tile = new TilePosition(x, y); var tile = new TilePosition(x, y);
if (map.Current.IsWall(tile))
// TODO: implement lookup for non tile aligned walls
if (map.Current.IsCurrentlyWall(tile.ToPixelPosition()))
continue; continue;
var tilePixelCenter = tile.ToPixelPosition().GetPixelRelative(4, 4).ToFloatPosition(); var tilePixelCenter = tile.ToPixelPosition().GetPixelRelative(4, 4).ToFloatPosition();

View file

@ -11,7 +11,7 @@ internal sealed class DrawMapStep(MapService map) : IDrawStep
for (ushort x = 0; x < MapService.PixelsPerRow; x++) for (ushort x = 0; x < MapService.PixelsPerRow; x++)
{ {
var pixel = new PixelPosition(x, y); var pixel = new PixelPosition(x, y);
if (!map.Current.IsCurrentlyWall(pixel)) if (!map.Current.IsWall(pixel))
continue; continue;
buffer[x, y] = true; buffer[x, y] = true;
} }