Files
systemrevivalserver/Game/Dungeon/DungeonLayoutV2.cs
T
2025-12-28 08:29:27 -05:00

189 lines
5.9 KiB
C#

using DataServer.Data.Entities;
using System.Text.Json;
namespace Server.Game.Dungeon;
/// <summary>
/// Enhanced dungeon layout with full tile visual information
/// </summary>
public class DungeonLayoutV2 : DungeonLayout
{
/// <summary>
/// Detailed tile placements with visual info
/// </summary>
public List<PlacedTile> TileDetails { get; set; } = new();
/// <summary>
/// Tilemap sheet data for client rendering
/// </summary>
public List<TileMapSheetInfo> TileMapSheets { get; set; } = new();
/// <summary>
/// Build tile data optimized for client transmission
/// </summary>
public void BuildClientTileData(Dictionary<int, TileDefinition> tileDefinitions, TileMapSheet? defaultSheet)
{
// Collect all unique sheets used
var sheetIds = TileDetails
.Select(t => tileDefinitions.GetValueOrDefault(t.TileDefinitionId)?.TileMapSheetId)
.Where(id => id.HasValue)
.Select(id => id!.Value)
.Distinct()
.ToList();
var sheets = tileDefinitions.Values
.Where(t => sheetIds.Contains(t.TileMapSheetId))
.Select(t => t.TileMapSheet)
.Where(s => s != null)
.DistinctBy(s => s!.Id)
.ToList();
// Add default sheet if not already included
if (defaultSheet != null && !sheets.Any(s => s?.Id == defaultSheet.Id))
{
sheets.Add(defaultSheet);
}
foreach (var sheet in sheets)
{
if (sheet == null) continue;
TileMapSheets.Add(new TileMapSheetInfo
{
Id = sheet.Id,
CdnPath = sheet.CdnPath,
TileWidth = sheet.TileWidth,
TileHeight = sheet.TileHeight,
SheetWidth = sheet.SheetWidth,
SheetHeight = sheet.SheetHeight,
Columns = sheet.Columns,
Rows = sheet.Rows
});
}
}
/// <summary>
/// Serialize to JSON for client
/// Includes full tile visual data
/// </summary>
public new string ToJson()
{
return JsonSerializer.Serialize(new
{
seed = Seed,
tier = Tier,
width = Width,
height = Height,
tileSize = TileSize,
tilesRLE = SerializeTilesRLE(),
spawn = new { x = SpawnPoint.X, y = SpawnPoint.Y },
rooms = Rooms.Select(r => new
{
x = r.X, y = r.Y, w = r.Width, h = r.Height,
type = r.Type.ToString(),
templateId = (r as DungeonRoomV2)?.TemplateId,
templateName = (r as DungeonRoomV2)?.TemplateName
}),
mobs = MobSpawns.Select(m => new
{
id = m.Id, x = m.X, y = m.Y,
type = m.MobType.ToString(),
roomIndex = m.RoomIndex
}),
chests = ChestSpawns.Select(c => new
{
id = c.Id, x = c.X, y = c.Y,
type = c.ChestType.ToString(),
roomIndex = c.RoomIndex
}),
// New: Visual tile data
tileSheets = 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,
rows = s.Rows
}),
visualTiles = 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
})
});
}
/// <summary>
/// Lightweight JSON for initial dungeon state (no visual data, sent separately)
/// </summary>
public string ToLightJson()
{
return JsonSerializer.Serialize(new
{
seed = Seed,
tier = Tier,
width = Width,
height = Height,
tileSize = TileSize,
tilesRLE = SerializeTilesRLE(),
spawn = new { x = SpawnPoint.X, y = SpawnPoint.Y },
rooms = Rooms.Select(r => new { x = r.X, y = r.Y, w = r.Width, h = r.Height, type = r.Type.ToString() }),
mobs = MobSpawns.Select(m => new { id = m.Id, x = m.X, y = m.Y, type = m.MobType.ToString() }),
chests = ChestSpawns.Select(c => new { id = c.Id, x = c.X, y = c.Y, type = c.ChestType.ToString() })
});
}
/// <summary>
/// Get visual tile data JSON (can be fetched separately for large dungeons)
/// </summary>
public string ToVisualTileJson()
{
return JsonSerializer.Serialize(new
{
sheets = 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 = 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
})
});
}
}
/// <summary>
/// Tilemap sheet info for client
/// </summary>
public class TileMapSheetInfo
{
public int Id { get; set; }
public string CdnPath { get; set; } = "";
public int TileWidth { get; set; }
public int TileHeight { get; set; }
public int SheetWidth { get; set; }
public int SheetHeight { get; set; }
public int Columns { get; set; }
public int Rows { get; set; }
}