load maps from text files on server start
This commit is contained in:
parent
7ce0e543ec
commit
fd3e195f69
8 changed files with 115 additions and 30 deletions
|
@ -1,3 +1,5 @@
|
|||
using System.IO;
|
||||
|
||||
namespace TanksServer.GameLogic;
|
||||
|
||||
internal sealed class MapService
|
||||
|
@ -7,31 +9,33 @@ internal sealed class MapService
|
|||
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 readonly string _map =
|
||||
"""
|
||||
#######.##########################.#########
|
||||
#...................##.....................#
|
||||
#...................##.....................#
|
||||
#.....####......................####.......#
|
||||
#..........................................#
|
||||
#............###...........###.............#
|
||||
#............#...............#.............#
|
||||
#...##.......#....#....#.....#......##.....#
|
||||
#....#..............................#......#
|
||||
.....#...#......................#...#.......
|
||||
.....#...#......................#...#.......
|
||||
#....#..............................#......#
|
||||
#...##.......#....#....#.....#......##.....#
|
||||
#............#...............#.............#
|
||||
#............###...........###.............#
|
||||
#..........................................#
|
||||
#.....####......................####.......#
|
||||
#...................##.....................#
|
||||
#...................##.....................#
|
||||
#######.##########################.#########
|
||||
"""
|
||||
.ReplaceLineEndings(string.Empty);
|
||||
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];
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue