2024-04-14 18:50:20 +02:00
|
|
|
using System.IO;
|
|
|
|
|
2024-04-10 19:25:45 +02:00
|
|
|
namespace TanksServer.GameLogic;
|
2024-04-06 13:46:34 +02:00
|
|
|
|
2024-04-07 13:02:49 +02:00
|
|
|
internal sealed class MapService
|
2024-04-06 13:46:34 +02:00
|
|
|
{
|
2024-04-12 18:32:10 +02:00
|
|
|
public const ushort TilesPerRow = 44;
|
|
|
|
public const ushort TilesPerColumn = 20;
|
|
|
|
public const ushort TileSize = 8;
|
|
|
|
public const ushort PixelsPerRow = TilesPerRow * TileSize;
|
|
|
|
public const ushort PixelsPerColumn = TilesPerColumn * TileSize;
|
2024-04-14 18:50:20 +02:00
|
|
|
private readonly string _map;
|
|
|
|
private readonly ILogger<MapService> _logger;
|
|
|
|
|
|
|
|
private string[] LoadMaps() => Directory.EnumerateFiles("./assets/maps/", "*.txt")
|
|
|
|
.Select(LoadMap)
|
|
|
|
.Where(s => s != null)
|
|
|
|
.Select(s => s!)
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
private string? LoadMap(string file)
|
|
|
|
{
|
|
|
|
var text = File.ReadAllText(file).ReplaceLineEndings(string.Empty).Trim();
|
|
|
|
if (text.Length != TilesPerColumn * TilesPerRow)
|
|
|
|
{
|
|
|
|
_logger.LogWarning("cannot load map {}: invalid length", file);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
2024-04-06 13:46:34 +02:00
|
|
|
|
2024-04-14 18:50:20 +02:00
|
|
|
public MapService(ILogger<MapService> logger)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
var maps = LoadMaps();
|
|
|
|
_map = maps[Random.Shared.Next(0, maps.Length)];
|
|
|
|
}
|
2024-04-06 13:46:34 +02:00
|
|
|
|
|
|
|
private char this[int tileX, int tileY] => _map[tileX + tileY * TilesPerRow];
|
|
|
|
|
2024-04-13 14:08:51 +02:00
|
|
|
public bool IsCurrentlyWall(TilePosition position) => this[position.X, position.Y] == '#';
|
2024-04-06 13:46:34 +02:00
|
|
|
}
|