90 lines
2.6 KiB
C#
90 lines
2.6 KiB
C#
using System.Text.Json;
|
|
|
|
namespace Server.Game.Dungeon;
|
|
|
|
/// <summary>
|
|
/// Contains the generated dungeon layout data
|
|
/// </summary>
|
|
public class DungeonLayout
|
|
{
|
|
public int Seed { get; set; }
|
|
public int Tier { get; set; }
|
|
public int Width { get; set; }
|
|
public int Height { get; set; }
|
|
public int TileSize { get; set; }
|
|
|
|
public int[,] Tiles { get; set; } = new int[0, 0];
|
|
public List<DungeonRoom> Rooms { get; set; } = new();
|
|
public DungeonPoint SpawnPoint { get; set; }
|
|
public DungeonPoint? ExitPosition { get; set; } // Set when dungeon is cleared
|
|
|
|
public List<DungeonMobSpawn> MobSpawns { get; set; } = new();
|
|
public List<DungeonChestSpawn> ChestSpawns { get; set; } = new();
|
|
|
|
public int TotalMobs { get; set; }
|
|
public int TotalChests { get; set; }
|
|
|
|
// Serialize tiles to string for DB storage
|
|
public string SerializeTiles()
|
|
{
|
|
var rows = new List<string>();
|
|
for (int y = 0; y < Height; y++)
|
|
{
|
|
var row = new char[Width];
|
|
for (int x = 0; x < Width; x++)
|
|
{
|
|
row[x] = Tiles[x, y] == 1 ? '.' : '#';
|
|
}
|
|
rows.Add(new string(row));
|
|
}
|
|
return string.Join("\n", rows);
|
|
}
|
|
|
|
public void DeserializeTiles(string data)
|
|
{
|
|
var rows = data.Split('\n');
|
|
Height = rows.Length;
|
|
Width = rows.Length > 0 ? rows[0].Length : 0;
|
|
Tiles = new int[Width, Height];
|
|
|
|
for (int y = 0; y < Height; y++)
|
|
{
|
|
for (int x = 0; x < Width && x < rows[y].Length; x++)
|
|
{
|
|
Tiles[x, y] = rows[y][x] == '.' ? 1 : 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
public string ToJson()
|
|
{
|
|
return JsonSerializer.Serialize(new
|
|
{
|
|
seed = Seed,
|
|
tier = Tier,
|
|
width = Width,
|
|
height = Height,
|
|
tileSize = TileSize,
|
|
tiles = SerializeTiles(),
|
|
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(),
|
|
roomIndex = m.RoomIndex
|
|
}),
|
|
chests = ChestSpawns.Select(c => new
|
|
{
|
|
id = c.Id, x = c.X, y = c.Y,
|
|
type = c.ChestType.ToString(),
|
|
roomIndex = c.RoomIndex
|
|
})
|
|
});
|
|
}
|
|
}
|