Files
systemrevivalserver/Game/Dungeon/DungeonVisualTileExtensions.cs
T

164 lines
5.5 KiB
C#

using DataServer.Data;
using DataServer.Data.Entities;
using Microsoft.EntityFrameworkCore;
using System.Text.Json;
namespace GameServer.Dungeon;
/// <summary>
/// Extension methods for visual tile data
/// </summary>
public static class DungeonVisualTileExtensions
{
/// <summary>
/// Build visual tile data for client from room templates used in the dungeon
/// </summary>
public static async Task<object?> BuildVisualTileDataAsync(this DungeonInstance instance, GameDbContext db)
{
var layout = instance.Simulation.Layout;
// Check if we have a V2 layout with tile details
if (layout is DungeonLayoutV2 layoutV2 && layoutV2.TileDetails.Count > 0)
{
return new
{
sheets = layoutV2.TileMapSheets.Select(s => new
{
id = s.Id,
path = s.CdnPath,
tileW = s.TileWidth,
tileH = s.TileHeight,
sheetW = s.SheetWidth,
sheetH = s.SheetHeight,
cols = s.Columns
}),
tiles = layoutV2.TileDetails.Select(t => new
{
x = t.X,
y = t.Y,
tid = t.TileDefinitionId,
l = t.Layer,
r = t.Rotation,
fx = t.FlipX,
fy = t.FlipY
})
};
}
// For V1 layouts, try to build visual data from room templates if any were used
// This is a fallback - in practice V2 generator should be used for visual tiles
// Get rooms that have template IDs (from V2 rooms stored in V1 layout)
var roomsWithTemplates = layout.Rooms
.OfType<DungeonRoomV2>()
.Where(r => r.TemplateId.HasValue)
.ToList();
if (roomsWithTemplates.Count == 0)
{
// No templates used, return null (client will use basic tilemap)
return null;
}
// Load templates and their tile data
var templateIds = roomsWithTemplates.Select(r => r.TemplateId!.Value).Distinct().ToList();
var templates = await db.RoomTemplates
.Where(t => templateIds.Contains(t.Id))
.ToListAsync();
// Collect all tile definition IDs
var allTileIds = new HashSet<int>();
foreach (var template in templates)
{
if (string.IsNullOrEmpty(template.TileDataJson)) continue;
try
{
var placements = JsonSerializer.Deserialize<List<TilePlacement>>(template.TileDataJson);
if (placements != null)
{
foreach (var p in placements)
{
allTileIds.Add(p.TileDefinitionId);
}
}
}
catch { /* Invalid JSON */ }
}
if (allTileIds.Count == 0)
{
return null;
}
// Load tile definitions with their sheets
var tileDefinitions = await db.TileDefinitions
.Include(t => t.TileMapSheet)
.Where(t => allTileIds.Contains(t.Id))
.ToDictionaryAsync(t => t.Id);
// Build sheets list
var sheets = tileDefinitions.Values
.Select(t => t.TileMapSheet)
.Where(s => s != null)
.DistinctBy(s => s!.Id)
.Select(s => new
{
id = s!.Id,
path = s.CdnPath,
tileW = s.TileWidth,
tileH = s.TileHeight,
sheetW = s.SheetWidth,
sheetH = s.SheetHeight,
cols = s.Columns
})
.ToList();
// Build tiles list
var tiles = new List<object>();
foreach (var room in roomsWithTemplates)
{
var template = templates.FirstOrDefault(t => t.Id == room.TemplateId);
if (template == null || string.IsNullOrEmpty(template.TileDataJson)) continue;
try
{
var placements = JsonSerializer.Deserialize<List<TilePlacement>>(template.TileDataJson);
if (placements == null) continue;
foreach (var p in placements)
{
var tileDef = tileDefinitions.GetValueOrDefault(p.TileDefinitionId);
if (tileDef == null) continue;
tiles.Add(new
{
x = room.X + p.X,
y = room.Y + p.Y,
tid = p.TileDefinitionId,
sid = tileDef.TileMapSheetId,
frame = tileDef.FrameIndex,
l = p.Layer,
r = p.Rotation,
fx = p.FlipX,
fy = p.FlipY
});
}
}
catch { /* Invalid JSON */ }
}
return new { sheets, tiles };
}
/// <summary>
/// Generate a dungeon using V2 generator with room templates
/// </summary>
public static async Task<DungeonLayoutV2> GenerateWithTemplatesAsync(GameDbContext db, int seed, int tier)
{
var generator = new DungeonGeneratorV2(db, seed, tier);
return await generator.GenerateAsync();
}
}