map switching
This commit is contained in:
parent
359e0235f9
commit
fcd84d2c83
|
@ -12,15 +12,17 @@ internal sealed class MapService
|
|||
public const ushort PixelsPerRow = TilesPerRow * TileSize;
|
||||
public const ushort PixelsPerColumn = TilesPerColumn * TileSize;
|
||||
|
||||
public Map Current { get; }
|
||||
public Map[] All { get; }
|
||||
|
||||
public Map Current { get; set; }
|
||||
|
||||
public MapService()
|
||||
{
|
||||
var textMaps = Directory.EnumerateFiles("./assets/maps/", "*.txt").Select(LoadMapString);
|
||||
var pngMaps = Directory.EnumerateFiles("./assets/maps/", "*.png").Select(LoadMapPng);
|
||||
|
||||
var maps = textMaps.Concat(pngMaps).ToList();
|
||||
Current = maps[Random.Shared.Next(maps.Count)];
|
||||
All = textMaps.Concat(pngMaps).ToArray();
|
||||
Current = All[Random.Shared.Next(All.Length)];
|
||||
}
|
||||
|
||||
private static Map LoadMapPng(string file)
|
||||
|
@ -37,7 +39,7 @@ internal sealed class MapService
|
|||
for (var x = 0; x < image.Width; x++)
|
||||
walls[x, y] = image[x, y] == whitePixel;
|
||||
|
||||
return new Map(walls);
|
||||
return new Map(Path.GetFileName(file),walls);
|
||||
}
|
||||
|
||||
private static Map LoadMapString(string file)
|
||||
|
@ -63,12 +65,14 @@ internal sealed class MapService
|
|||
}
|
||||
}
|
||||
|
||||
return new Map(walls);
|
||||
return new Map(Path.GetFileName(file), walls);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class Map(bool[,] walls)
|
||||
internal sealed class Map(string name, bool[,] walls)
|
||||
{
|
||||
public string Name => name;
|
||||
|
||||
public bool IsWall(int x, int y) => walls[x, y];
|
||||
|
||||
public bool IsWall(PixelPosition position) => walls[position.X, position.Y];
|
||||
|
|
|
@ -6,4 +6,5 @@ namespace TanksServer.Interactivity;
|
|||
[JsonSerializable(typeof(IEnumerable<Player>))]
|
||||
[JsonSerializable(typeof(Guid))]
|
||||
[JsonSerializable(typeof(NameId))]
|
||||
[JsonSerializable(typeof(Dictionary<int, string>))]
|
||||
internal sealed partial class AppSerializerContext : JsonSerializerContext;
|
||||
|
|
|
@ -22,6 +22,7 @@ public static class Program
|
|||
var clientScreenServer = app.Services.GetRequiredService<ClientScreenServer>();
|
||||
var playerService = app.Services.GetRequiredService<PlayerServer>();
|
||||
var controlsServer = app.Services.GetRequiredService<ControlsServer>();
|
||||
var mapService = app.Services.GetRequiredService<MapService>();
|
||||
|
||||
var clientFileProvider = new PhysicalFileProvider(Path.Combine(app.Environment.ContentRootPath, "client"));
|
||||
app.UseDefaultFiles(new DefaultFilesOptions { FileProvider = clientFileProvider });
|
||||
|
@ -40,6 +41,7 @@ public static class Program
|
|||
? Results.Ok(new NameId(player.Name, player.Id))
|
||||
: Results.Unauthorized();
|
||||
});
|
||||
|
||||
app.MapGet("/player", ([FromQuery] Guid id) =>
|
||||
playerService.TryGet(id, out var foundPlayer)
|
||||
? Results.Ok((object?)foundPlayer)
|
||||
|
@ -71,6 +73,22 @@ public static class Program
|
|||
return Results.Empty;
|
||||
});
|
||||
|
||||
app.MapGet("/map", () =>
|
||||
{
|
||||
var dict = mapService.All
|
||||
.Select((m, i) => (m.Name, i))
|
||||
.ToDictionary(pair => pair.i, pair => pair.Name);
|
||||
return dict;
|
||||
});
|
||||
|
||||
app.MapPost("/map", ([FromQuery] int index) =>
|
||||
{
|
||||
if (index < 0 || index >= mapService.All.Length)
|
||||
return Results.NotFound("index does not exist");
|
||||
mapService.Current = mapService.All[index];
|
||||
return Results.Ok();
|
||||
});
|
||||
|
||||
app.Run();
|
||||
}
|
||||
|
||||
|
|
|
@ -6,3 +6,9 @@ GET localhost/scores
|
|||
|
||||
###
|
||||
|
||||
GET localhost/map
|
||||
|
||||
###
|
||||
|
||||
POST localhost/map?index=0
|
||||
|
||||
|
|
Loading…
Reference in a new issue