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;
|
||||
|
||||
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];
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0"/>
|
||||
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.3"/>
|
||||
<ProjectReference Include="../DisplayCommands/DisplayCommands.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -40,10 +41,7 @@
|
|||
</Content>
|
||||
<Content Include="../Makefile"/>
|
||||
<Content Include="../.editorconfig"/>
|
||||
<None Include="assets\maps\**" CopyToOutputDirectory="PreserveNewest"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../DisplayCommands/DisplayCommands.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</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-height: 100vh;
|
||||
flex-shrink: 0;
|
||||
|
||||
border: solid var(--border-size-thick);
|
||||
border-color: var(--color-primary);
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
kbd {
|
||||
--kbd-key-size: 2em;
|
||||
|
||||
background: var(--color-secondary);
|
||||
background: var(--color-primary);
|
||||
padding: var(--padding-normal);
|
||||
display: block;
|
||||
|
||||
|
|
Loading…
Reference in a new issue