Dungeon Building p10
This commit is contained in:
@@ -137,6 +137,9 @@ public class DungeonGeneratorV2
|
||||
// Store rooms
|
||||
layout.Rooms = rooms.Cast<DungeonRoom>().ToList();
|
||||
|
||||
// Auto-generate walls around floor tiles
|
||||
GenerateAutoWalls(layout);
|
||||
|
||||
// Build tile data for client
|
||||
layout.BuildClientTileData(_tileDefinitions, _defaultSheet);
|
||||
|
||||
@@ -333,6 +336,32 @@ public class DungeonGeneratorV2
|
||||
catch { /* Invalid JSON, fall through to basic carve */ }
|
||||
}
|
||||
|
||||
// Parse connection points from template
|
||||
if (!string.IsNullOrEmpty(template.ConnectionPointsJson))
|
||||
{
|
||||
try
|
||||
{
|
||||
var connections = JsonSerializer.Deserialize<List<ConnectionPoint>>(template.ConnectionPointsJson);
|
||||
if (connections != null)
|
||||
{
|
||||
room.ConnectionPoints = connections;
|
||||
}
|
||||
}
|
||||
catch { /* Invalid JSON */ }
|
||||
}
|
||||
|
||||
// If no connection points defined, create default ones on each edge
|
||||
if (room.ConnectionPoints.Count == 0)
|
||||
{
|
||||
room.ConnectionPoints = new List<ConnectionPoint>
|
||||
{
|
||||
new() { X = room.Width / 2, Y = 0, Direction = "north", Width = 2 },
|
||||
new() { X = room.Width / 2, Y = room.Height - 1, Direction = "south", Width = 2 },
|
||||
new() { X = 0, Y = room.Height / 2, Direction = "west", Width = 2 },
|
||||
new() { X = room.Width - 1, Y = room.Height / 2, Direction = "east", Width = 2 }
|
||||
};
|
||||
}
|
||||
|
||||
// If no tiles were placed, do basic carve
|
||||
if (!layout.TileDetails.Any(t => t.X >= x && t.X < x + room.Width && t.Y >= y && t.Y < y + room.Height))
|
||||
{
|
||||
@@ -410,42 +439,86 @@ public class DungeonGeneratorV2
|
||||
|
||||
private async Task ConnectRoomsAsync(DungeonLayoutV2 layout, DungeonRoomV2 a, DungeonRoomV2 b)
|
||||
{
|
||||
// Check for hallway templates
|
||||
// For now, use basic corridor carving
|
||||
|
||||
int x = a.CenterX;
|
||||
int y = a.CenterY;
|
||||
int targetX = b.CenterX;
|
||||
int targetY = b.CenterY;
|
||||
|
||||
var floorTile = _tileDefinitions.Values.FirstOrDefault(t => t.Type == TileType.Floor);
|
||||
int floorId = floorTile?.Id ?? 1;
|
||||
int sheetId = floorTile?.TileMapSheetId ?? 0;
|
||||
int frameIndex = floorTile?.FrameIndex ?? 0;
|
||||
|
||||
// L-shaped corridor
|
||||
if (_rng.Next(2) == 0)
|
||||
// Determine best connection points based on relative positions
|
||||
string aDirection, bDirection;
|
||||
int deltaX = b.CenterX - a.CenterX;
|
||||
int deltaY = b.CenterY - a.CenterY;
|
||||
|
||||
// Choose connection directions based on which room is where
|
||||
if (Math.Abs(deltaX) > Math.Abs(deltaY))
|
||||
{
|
||||
CarveHorizontalCorridor(layout, x, targetX, y, floorId, sheetId, frameIndex);
|
||||
CarveVerticalCorridor(layout, y, targetY, targetX, floorId, sheetId, frameIndex);
|
||||
// Horizontal connection preferred
|
||||
aDirection = deltaX > 0 ? "east" : "west";
|
||||
bDirection = deltaX > 0 ? "west" : "east";
|
||||
}
|
||||
else
|
||||
{
|
||||
CarveVerticalCorridor(layout, y, targetY, x, floorId, sheetId, frameIndex);
|
||||
CarveHorizontalCorridor(layout, x, targetX, targetY, floorId, sheetId, frameIndex);
|
||||
// Vertical connection preferred
|
||||
aDirection = deltaY > 0 ? "south" : "north";
|
||||
bDirection = deltaY > 0 ? "north" : "south";
|
||||
}
|
||||
|
||||
// Get connection points (or fall back to center)
|
||||
var cpA = a.GetConnectionPoint(aDirection);
|
||||
var cpB = b.GetConnectionPoint(bDirection);
|
||||
|
||||
int startX, startY, endX, endY;
|
||||
int corridorWidth = 2;
|
||||
|
||||
if (cpA != null)
|
||||
{
|
||||
var (wx, wy) = a.GetConnectionWorldPos(cpA);
|
||||
startX = wx;
|
||||
startY = wy;
|
||||
corridorWidth = Math.Max(corridorWidth, cpA.Width);
|
||||
}
|
||||
else
|
||||
{
|
||||
startX = a.CenterX;
|
||||
startY = a.CenterY;
|
||||
}
|
||||
|
||||
if (cpB != null)
|
||||
{
|
||||
var (wx, wy) = b.GetConnectionWorldPos(cpB);
|
||||
endX = wx;
|
||||
endY = wy;
|
||||
corridorWidth = Math.Max(corridorWidth, cpB.Width);
|
||||
}
|
||||
else
|
||||
{
|
||||
endX = b.CenterX;
|
||||
endY = b.CenterY;
|
||||
}
|
||||
|
||||
// L-shaped corridor from connection point to connection point
|
||||
if (_rng.Next(2) == 0)
|
||||
{
|
||||
CarveHorizontalCorridor(layout, startX, endX, startY, floorId, sheetId, frameIndex, corridorWidth);
|
||||
CarveVerticalCorridor(layout, startY, endY, endX, floorId, sheetId, frameIndex, corridorWidth);
|
||||
}
|
||||
else
|
||||
{
|
||||
CarveVerticalCorridor(layout, startY, endY, startX, floorId, sheetId, frameIndex, corridorWidth);
|
||||
CarveHorizontalCorridor(layout, startX, endX, endY, floorId, sheetId, frameIndex, corridorWidth);
|
||||
}
|
||||
}
|
||||
|
||||
private void CarveHorizontalCorridor(DungeonLayoutV2 layout, int x1, int x2, int y, int floorId, int sheetId, int frameIndex)
|
||||
private void CarveHorizontalCorridor(DungeonLayoutV2 layout, int x1, int x2, int y, int floorId, int sheetId, int frameIndex, int width = 2)
|
||||
{
|
||||
int minX = Math.Min(x1, x2);
|
||||
int maxX = Math.Max(x1, x2);
|
||||
|
||||
for (int x = minX; x <= maxX; x++)
|
||||
{
|
||||
for (int dy = 0; dy < 2; dy++) // 2-tile wide corridor
|
||||
for (int dy = 0; dy < width; dy++)
|
||||
{
|
||||
int ty = y + dy;
|
||||
int ty = y + dy - width / 2; // Center the corridor
|
||||
if (x >= 0 && x < MapWidth && ty >= 0 && ty < MapHeight && layout.Tiles[x, ty] == 0)
|
||||
{
|
||||
layout.Tiles[x, ty] = floorId;
|
||||
@@ -472,16 +545,16 @@ public class DungeonGeneratorV2
|
||||
}
|
||||
}
|
||||
|
||||
private void CarveVerticalCorridor(DungeonLayoutV2 layout, int y1, int y2, int x, int floorId, int sheetId, int frameIndex)
|
||||
private void CarveVerticalCorridor(DungeonLayoutV2 layout, int y1, int y2, int x, int floorId, int sheetId, int frameIndex, int width = 2)
|
||||
{
|
||||
int minY = Math.Min(y1, y2);
|
||||
int maxY = Math.Max(y1, y2);
|
||||
|
||||
for (int y = minY; y <= maxY; y++)
|
||||
{
|
||||
for (int dx = 0; dx < 2; dx++) // 2-tile wide corridor
|
||||
for (int dx = 0; dx < width; dx++)
|
||||
{
|
||||
int tx = x + dx;
|
||||
int tx = x + dx - width / 2; // Center the corridor
|
||||
if (tx >= 0 && tx < MapWidth && y >= 0 && y < MapHeight && layout.Tiles[tx, y] == 0)
|
||||
{
|
||||
layout.Tiles[tx, y] = floorId;
|
||||
@@ -508,6 +581,99 @@ public class DungeonGeneratorV2
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Automatically generate wall tiles around floor tiles that border void
|
||||
/// </summary>
|
||||
private void GenerateAutoWalls(DungeonLayoutV2 layout)
|
||||
{
|
||||
// Find wall tiles for each direction
|
||||
var wallTiles = _tileDefinitions.Values.Where(t => t.Type == TileType.Wall).ToList();
|
||||
if (wallTiles.Count == 0)
|
||||
{
|
||||
Console.WriteLine("[DungeonGenV2] No wall tiles found - skipping auto-wall generation");
|
||||
return;
|
||||
}
|
||||
|
||||
// Try to find directional walls by tags, fall back to first wall
|
||||
var wallNorth = wallTiles.FirstOrDefault(t => t.Tags?.Contains("north") == true) ?? wallTiles[0];
|
||||
var wallSouth = wallTiles.FirstOrDefault(t => t.Tags?.Contains("south") == true) ?? wallTiles[0];
|
||||
var wallEast = wallTiles.FirstOrDefault(t => t.Tags?.Contains("east") == true) ?? wallTiles[0];
|
||||
var wallWest = wallTiles.FirstOrDefault(t => t.Tags?.Contains("west") == true) ?? wallTiles[0];
|
||||
var wallCornerNE = wallTiles.FirstOrDefault(t => t.Tags?.Contains("corner_ne") == true) ?? wallTiles[0];
|
||||
var wallCornerNW = wallTiles.FirstOrDefault(t => t.Tags?.Contains("corner_nw") == true) ?? wallTiles[0];
|
||||
var wallCornerSE = wallTiles.FirstOrDefault(t => t.Tags?.Contains("corner_se") == true) ?? wallTiles[0];
|
||||
var wallCornerSW = wallTiles.FirstOrDefault(t => t.Tags?.Contains("corner_sw") == true) ?? wallTiles[0];
|
||||
|
||||
// Track positions that need walls
|
||||
var wallPositions = new List<(int X, int Y, TileDefinition Tile)>();
|
||||
|
||||
// Check each void tile to see if it borders a floor
|
||||
for (int x = 0; x < MapWidth; x++)
|
||||
{
|
||||
for (int y = 0; y < MapHeight; y++)
|
||||
{
|
||||
// Skip if not void
|
||||
if (layout.Tiles[x, y] != 0) continue;
|
||||
|
||||
// Check neighbors for floor tiles (walkable)
|
||||
bool hasFloorNorth = y > 0 && layout.IsWalkable(x, y - 1);
|
||||
bool hasFloorSouth = y < MapHeight - 1 && layout.IsWalkable(x, y + 1);
|
||||
bool hasFloorEast = x < MapWidth - 1 && layout.IsWalkable(x + 1, y);
|
||||
bool hasFloorWest = x > 0 && layout.IsWalkable(x - 1, y);
|
||||
|
||||
// If any adjacent tile is floor, this should be a wall
|
||||
if (hasFloorNorth || hasFloorSouth || hasFloorEast || hasFloorWest)
|
||||
{
|
||||
// Determine wall type based on which direction has floor
|
||||
TileDefinition wallTile;
|
||||
|
||||
// Check for corners first
|
||||
if (hasFloorSouth && hasFloorEast)
|
||||
wallTile = wallCornerNW;
|
||||
else if (hasFloorSouth && hasFloorWest)
|
||||
wallTile = wallCornerNE;
|
||||
else if (hasFloorNorth && hasFloorEast)
|
||||
wallTile = wallCornerSW;
|
||||
else if (hasFloorNorth && hasFloorWest)
|
||||
wallTile = wallCornerSE;
|
||||
else if (hasFloorSouth)
|
||||
wallTile = wallNorth; // Wall is north of floor, so it's a north-facing wall
|
||||
else if (hasFloorNorth)
|
||||
wallTile = wallSouth;
|
||||
else if (hasFloorEast)
|
||||
wallTile = wallWest;
|
||||
else
|
||||
wallTile = wallEast;
|
||||
|
||||
wallPositions.Add((x, y, wallTile));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Place wall tiles
|
||||
foreach (var (x, y, tile) in wallPositions)
|
||||
{
|
||||
layout.Tiles[x, y] = tile.Id;
|
||||
|
||||
// Walls are not walkable
|
||||
if (layout.Walkable != null)
|
||||
{
|
||||
layout.Walkable[x, y] = false;
|
||||
}
|
||||
|
||||
layout.TileDetails.Add(new PlacedTile
|
||||
{
|
||||
X = x, Y = y,
|
||||
TileDefinitionId = tile.Id,
|
||||
SheetId = tile.TileMapSheetId,
|
||||
FrameIndex = tile.FrameIndex,
|
||||
Layer = 2 // Walls on layer 2
|
||||
});
|
||||
}
|
||||
|
||||
Console.WriteLine($"[DungeonGenV2] Generated {wallPositions.Count} auto-walls");
|
||||
}
|
||||
|
||||
private RoomTemplate? SelectWeightedTemplate(List<RoomTemplate> templates)
|
||||
{
|
||||
if (templates.Count == 0) return null;
|
||||
@@ -598,7 +764,23 @@ public class DungeonRoomV2 : DungeonRoom
|
||||
public string? TemplateName { get; set; }
|
||||
public new RoomType Type { get; set; } = RoomType.Normal;
|
||||
|
||||
/// <summary>Connection points parsed from template</summary>
|
||||
public List<ConnectionPoint> ConnectionPoints { get; set; } = new();
|
||||
|
||||
public DungeonRoomV2(int x, int y, int width, int height) : base(x, y, width, height) { }
|
||||
|
||||
/// <summary>Get the best connection point for a given direction</summary>
|
||||
public ConnectionPoint? GetConnectionPoint(string direction)
|
||||
{
|
||||
return ConnectionPoints.FirstOrDefault(c => c.Direction.Equals(direction, StringComparison.OrdinalIgnoreCase))
|
||||
?? ConnectionPoints.FirstOrDefault(); // Fallback to any
|
||||
}
|
||||
|
||||
/// <summary>Get world position of a connection point</summary>
|
||||
public (int X, int Y) GetConnectionWorldPos(ConnectionPoint cp)
|
||||
{
|
||||
return (X + cp.X, Y + cp.Y);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user