232 lines
7.5 KiB
C#
232 lines
7.5 KiB
C#
using DataServer.Data.Entities;
|
|
using System.Text.Json;
|
|
|
|
namespace GameServer.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>
|
|
/// Placed objects (decorations, containers, traps)
|
|
/// </summary>
|
|
public List<PlacedObject> Objects { get; set; } = new();
|
|
|
|
/// <summary>
|
|
/// Player spawn position
|
|
/// </summary>
|
|
public int PlayerSpawnX { get; set; }
|
|
public int PlayerSpawnY { get; set; }
|
|
|
|
/// <summary>
|
|
/// Exit portal position
|
|
/// </summary>
|
|
public int ExitX { get; set; }
|
|
public int ExitY { get; set; }
|
|
|
|
public DungeonLayoutV2() : base() { }
|
|
|
|
public DungeonLayoutV2(int width, int height)
|
|
{
|
|
Width = width;
|
|
Height = height;
|
|
Tiles = new int[width, height];
|
|
Walkable = new bool[width, height];
|
|
}
|
|
|
|
/// <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();
|
|
|
|
// Also add sheets from direct SheetId references
|
|
var directSheetIds = TileDetails
|
|
.Where(t => t.SheetId > 0)
|
|
.Select(t => t.SheetId)
|
|
.Distinct();
|
|
sheetIds.AddRange(directSheetIds);
|
|
sheetIds = sheetIds.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 = PlayerSpawnX > 0 ? PlayerSpawnX : SpawnPoint.X, y = PlayerSpawnY > 0 ? PlayerSpawnY : SpawnPoint.Y },
|
|
exit = new { x = ExitX, y = ExitY },
|
|
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
|
|
}),
|
|
// Objects (decorations, containers, traps)
|
|
objects = Objects.Select(o => new
|
|
{
|
|
x = o.X,
|
|
y = o.Y,
|
|
id = o.ObjectTemplateId,
|
|
name = o.Name,
|
|
type = o.Type,
|
|
sheetId = o.SpriteSheetId,
|
|
frame = o.FrameIndex,
|
|
blocks = o.BlocksMovement,
|
|
interact = o.IsInteractable,
|
|
trap = o.IsTrap,
|
|
damage = o.TrapDamage,
|
|
loot = o.LootTableId
|
|
}),
|
|
// Tile sheets
|
|
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
|
|
}),
|
|
// Visual tile details
|
|
visualTiles = TileDetails.Select(t => new
|
|
{
|
|
x = t.X,
|
|
y = t.Y,
|
|
tid = t.TileDefinitionId,
|
|
sid = t.SheetId,
|
|
f = t.FrameIndex,
|
|
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 = PlayerSpawnX > 0 ? PlayerSpawnX : SpawnPoint.X, y = PlayerSpawnY > 0 ? PlayerSpawnY : SpawnPoint.Y },
|
|
exit = new { x = ExitX, y = ExitY },
|
|
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() }),
|
|
objects = Objects.Select(o => new { x = o.X, y = o.Y, id = o.ObjectTemplateId, type = o.Type, blocks = o.BlocksMovement })
|
|
});
|
|
}
|
|
|
|
/// <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,
|
|
sid = t.SheetId,
|
|
f = t.FrameIndex,
|
|
l = t.Layer,
|
|
r = t.Rotation,
|
|
fx = t.FlipX,
|
|
fy = t.FlipY
|
|
})
|
|
});
|
|
}
|
|
} |