servicepoint-tanks/TanksServer/GameLogic/MapService.cs

44 lines
1.3 KiB
C#
Raw Normal View History

using System.IO;
2024-04-10 19:25:45 +02:00
namespace TanksServer.GameLogic;
2024-04-07 13:02:49 +02:00
internal sealed class MapService
{
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;
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;
}
public MapService(ILogger<MapService> logger)
{
_logger = logger;
var maps = LoadMaps();
_map = maps[Random.Shared.Next(0, maps.Length)];
}
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] == '#';
}