load maps from text files on server start

This commit is contained in:
Vinzenz Schroeter 2024-04-14 18:50:20 +02:00
parent 7ce0e543ec
commit fd3e195f69
8 changed files with 115 additions and 30 deletions

View file

@ -1,3 +1,5 @@
using System.IO;
namespace TanksServer.GameLogic; namespace TanksServer.GameLogic;
internal sealed class MapService internal sealed class MapService
@ -7,31 +9,33 @@ internal sealed class MapService
public const ushort TileSize = 8; public const ushort TileSize = 8;
public const ushort PixelsPerRow = TilesPerRow * TileSize; public const ushort PixelsPerRow = TilesPerRow * TileSize;
public const ushort PixelsPerColumn = TilesPerColumn * TileSize; public const ushort PixelsPerColumn = TilesPerColumn * TileSize;
private readonly string _map;
private readonly ILogger<MapService> _logger;
private readonly string _map = 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)];
.ReplaceLineEndings(string.Empty); }
private char this[int tileX, int tileY] => _map[tileX + tileY * TilesPerRow]; private char this[int tileX, int tileY] => _map[tileX + tileY * TilesPerRow];

View file

@ -27,6 +27,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0"/> <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0"/>
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.3"/> <PackageReference Include="SixLabors.ImageSharp" Version="3.1.3"/>
<ProjectReference Include="../DisplayCommands/DisplayCommands.csproj"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -40,10 +41,7 @@
</Content> </Content>
<Content Include="../Makefile"/> <Content Include="../Makefile"/>
<Content Include="../.editorconfig"/> <Content Include="../.editorconfig"/>
</ItemGroup> <None Include="assets\maps\**" CopyToOutputDirectory="PreserveNewest"/>
<ItemGroup>
<ProjectReference Include="../DisplayCommands/DisplayCommands.csproj"/>
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -0,0 +1,20 @@
######......####################......######
#..........................................#
#.....#####......................#####.....#
#..........#....................#..........#
#...........#####..........#####...........#
#..........................................#
#.........##....................##.........#
#............#..#..........#..#............#
#..............#............#..............#
#.............#..............#.............#
#....................##....................#
#....................##....................#
#....................##....................#
#.............#......##......#.............#
#...........#..#............#..#...........#
#.........#.....#..........#.....#.........#
#.......#............##............#.......#
#........#........................#........#
#..........................................#
######......####################......######

View file

@ -0,0 +1,20 @@
#######.##########################.#########
#...................##.....................#
#...................##.....................#
#.....####......................####.......#
#..........................................#
#............###...........###.............#
#............#...............#.............#
#...##.......#....#....#.....#......##.....#
#....#..............................#......#
.....#...#......................#...#.......
.....#...#......................#...#.......
#....#..............................#......#
#...##.......#....#....#.....#......##.....#
#............#...............#.............#
#............###...........###.............#
#..........................................#
#.....####......................####.......#
#...................##.....................#
#...................##.....................#
#######.##########################.#########

View file

@ -0,0 +1,20 @@
#####......................................#
..#........................................#
..#........................................#
..#......###...............................#
..#.....#...#..............................#
........#####..............................#
........#...#...#...#......................#
........#...#...##..#......................#
................#.# #......................#
................#..##...#...#..............#
................#...#...#..#...............#
........................###................#
........................#..#......###......#
........................#...#...#..........#
.................................###.......#
....................................#...#..#
................................####....#..#
........................................#..#
...........................................#
........................................#..#

View file

@ -0,0 +1,20 @@
............................................
#.........##.....#.........................#
#........#..#..............................#
#.........##...............................#
#..................#.......................#
#.........#........##.............#........#
#....#.........#....#............#.........#
#....#.....................................#
#............#.............#..........#....#
#.........##.....#......................#..#
#........#..#..............................#
#.........##..................#............#
#..........................................#
#....#..............#...........#..........#
#....................#...##...#............#
#..........##..........#....#.........#....#
#.........#...#........#....#..............#
#.........#...#......#...##...#............#
#..........##......#............#.......#..#
............................................

View file

@ -6,4 +6,7 @@
max-width: 100vw; max-width: 100vw;
max-height: 100vh; max-height: 100vh;
flex-shrink: 0; flex-shrink: 0;
border: solid var(--border-size-thick);
border-color: var(--color-primary);
} }

View file

@ -14,7 +14,7 @@
kbd { kbd {
--kbd-key-size: 2em; --kbd-key-size: 2em;
background: var(--color-secondary); background: var(--color-primary);
padding: var(--padding-normal); padding: var(--padding-normal);
display: block; display: block;