load maps from text files on server start
This commit is contained in:
parent
7ce0e543ec
commit
fd3e195f69
|
@ -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];
|
||||||
|
|
||||||
|
|
|
@ -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"/>
|
||||||
|
<None Include="assets\maps\**" CopyToOutputDirectory="PreserveNewest"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="../DisplayCommands/DisplayCommands.csproj"/>
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
20
TanksServer/assets/maps/buggie.txt
Normal file
20
TanksServer/assets/maps/buggie.txt
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
######......####################......######
|
||||||
|
#..........................................#
|
||||||
|
#.....#####......................#####.....#
|
||||||
|
#..........#....................#..........#
|
||||||
|
#...........#####..........#####...........#
|
||||||
|
#..........................................#
|
||||||
|
#.........##....................##.........#
|
||||||
|
#............#..#..........#..#............#
|
||||||
|
#..............#............#..............#
|
||||||
|
#.............#..............#.............#
|
||||||
|
#....................##....................#
|
||||||
|
#....................##....................#
|
||||||
|
#....................##....................#
|
||||||
|
#.............#......##......#.............#
|
||||||
|
#...........#..#............#..#...........#
|
||||||
|
#.........#.....#..........#.....#.........#
|
||||||
|
#.......#............##............#.......#
|
||||||
|
#........#........................#........#
|
||||||
|
#..........................................#
|
||||||
|
######......####################......######
|
20
TanksServer/assets/maps/orig.txt
Normal file
20
TanksServer/assets/maps/orig.txt
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
#######.##########################.#########
|
||||||
|
#...................##.....................#
|
||||||
|
#...................##.....................#
|
||||||
|
#.....####......................####.......#
|
||||||
|
#..........................................#
|
||||||
|
#............###...........###.............#
|
||||||
|
#............#...............#.............#
|
||||||
|
#...##.......#....#....#.....#......##.....#
|
||||||
|
#....#..............................#......#
|
||||||
|
.....#...#......................#...#.......
|
||||||
|
.....#...#......................#...#.......
|
||||||
|
#....#..............................#......#
|
||||||
|
#...##.......#....#....#.....#......##.....#
|
||||||
|
#............#...............#.............#
|
||||||
|
#............###...........###.............#
|
||||||
|
#..........................................#
|
||||||
|
#.....####......................####.......#
|
||||||
|
#...................##.....................#
|
||||||
|
#...................##.....................#
|
||||||
|
#######.##########################.#########
|
20
TanksServer/assets/maps/tanks.txt
Normal file
20
TanksServer/assets/maps/tanks.txt
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
#####......................................#
|
||||||
|
..#........................................#
|
||||||
|
..#........................................#
|
||||||
|
..#......###...............................#
|
||||||
|
..#.....#...#..............................#
|
||||||
|
........#####..............................#
|
||||||
|
........#...#...#...#......................#
|
||||||
|
........#...#...##..#......................#
|
||||||
|
................#.# #......................#
|
||||||
|
................#..##...#...#..............#
|
||||||
|
................#...#...#..#...............#
|
||||||
|
........................###................#
|
||||||
|
........................#..#......###......#
|
||||||
|
........................#...#...#..........#
|
||||||
|
.................................###.......#
|
||||||
|
....................................#...#..#
|
||||||
|
................................####....#..#
|
||||||
|
........................................#..#
|
||||||
|
...........................................#
|
||||||
|
........................................#..#
|
20
TanksServer/assets/maps/upside_down.txt
Normal file
20
TanksServer/assets/maps/upside_down.txt
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
............................................
|
||||||
|
#.........##.....#.........................#
|
||||||
|
#........#..#..............................#
|
||||||
|
#.........##...............................#
|
||||||
|
#..................#.......................#
|
||||||
|
#.........#........##.............#........#
|
||||||
|
#....#.........#....#............#.........#
|
||||||
|
#....#.....................................#
|
||||||
|
#............#.............#..........#....#
|
||||||
|
#.........##.....#......................#..#
|
||||||
|
#........#..#..............................#
|
||||||
|
#.........##..................#............#
|
||||||
|
#..........................................#
|
||||||
|
#....#..............#...........#..........#
|
||||||
|
#....................#...##...#............#
|
||||||
|
#..........##..........#....#.........#....#
|
||||||
|
#.........#...#........#....#..............#
|
||||||
|
#.........#...#......#...##...#............#
|
||||||
|
#..........##......#............#.......#..#
|
||||||
|
............................................
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue