先本文看看最终效果
前言
前两期我们使用了随机游走算法已经实现了地牢的生成,本期再说另外一种生成地牢的方法,使用二叉空间分割算法,可以用来生成规则的房间或者不规则的地牢。
二叉空间分割算法
修改ProceduralGenerationAlgorithms,实现了二叉空间分割算法,用于将初始空间进行分割以创建房间
//二叉空间分割算法 public static List<BoundsInt> BinarySpacePartitioning(BoundsInt spaceToSplit, int minWidth, int minHeight) { Queue<BoundsInt> roomsQueue = new Queue<BoundsInt>(); // 创建队列来保存分割的空间 List<BoundsInt> roomsList = new List<BoundsInt>(); // 创建列表来保存最终的房间 roomsQueue.Enqueue(spaceToSplit); // 将初始空间加入队列中 while (roomsQueue.Count > 0) { var room = roomsQueue.Dequeue(); // 取出队列中的一个空间 if (room.size.y >= minHeight && room.size.x >= minWidth) // 如果空间的宽度和高度都大于等于最小值 { if (Random.value < 0.5f) // 随机选择垂直或水平分割 { if (room.size.y >= minHeight * 2) // 如果空间的高度大于等于最小高度的两倍,则进行水平分割 { SplitHorizontally(minHeight, roomsQueue, room); // 水平分割空间 } else if (room.size.x >= minWidth * 2) // 如果空间的宽度大于等于最小宽度的两倍,则进行垂直分割 { SplitVertically(minWidth, roomsQueue, room); // 垂直分割空间 } else if (room.size.x >= minWidth && room.size.y >= minHeight) // 如果空间的宽度和高度都大于等于最小值,则将其添加到房间列表中 { roomsList.Add(room); } } else { if (room.size.x >= minWidth * 2) // 如果空间的宽度大于等于最小宽度的两倍,则进行垂直分割 { SplitVertically(minWidth, roomsQueue, room); // 垂直分割空间 } else if (room.size.y >= minHeight * 2) // 如果空间的高度大于等于最小高度的两倍,则进行水平分割 { SplitHorizontally(minHeight, roomsQueue, room); // 水平分割空间 } else if (room.size.x >= minWidth && room.size.y >= minHeight) // 如果空间的宽度和高度都大于等于最小值,则将其添加到房间列表中 { roomsList.Add(room); } } } } return roomsList; // 返回最终的房间列表 } // 垂直分割空间 private static void SplitVertically(int minWidth, Queue<BoundsInt> roomsQueue, BoundsInt room) { var xSplit = Random.Range(1, room.size.x); // 随机选择分割点的x坐标 BoundsInt room1 = new BoundsInt(room.min, new Vector3Int(xSplit, room.size.y, room.size.z)); BoundsInt room2 = new BoundsInt(new Vector3Int(room.min.x + xSplit, room.min.y, room.min.z), new Vector3Int(room.size.x - xSplit, room.size.y, room.size.z)); roomsQueue.Enqueue(room1); // 添加分割后的两个新空间到队列中 roomsQueue.Enqueue(room2); } // 水平分割空间 private static void SplitHorizontally(int minHeight, Queue<BoundsInt> roomsQueue, BoundsInt room) { var ySplit = Random.Range(1, room.size.y); // 随机选择分割点的y坐标 BoundsInt room1 = new BoundsInt(room.min, new Vector3Int(room.size.x, ySplit, room.size.z)); BoundsInt room2 = new BoundsInt(new Vector3Int(room.min.x, room.min.y + ySplit, room.min.z), new Vector3Int(room.size.x, room.size.y - ySplit, room.size.z)); roomsQueue.Enqueue(room1); // 添加分割后的两个新空间到队列中 roomsQueue.Enqueue(room2); }
房间优先生成
新增RoomFirstDungeonGenerator,这段代码实现了一个基于房间的地牢生成器,通过使用偏移量,我们可以在房间的边界周围保留一定的间距,使得房间之间更加清晰可辨,避免它们彼此连接或重叠。
public class RoomFirstDungeonGenerator : SimpleRandomWalkDungeonGenerator { [SerializeField, Header("最小房间宽度和高度")] private int minRoomWidth = 4, minRoomHeight = 4; [SerializeField, Header("地牢宽度和高度")] private int dungeonWidth = 20, dungeonHeight = 20; [SerializeField, Header("偏移量")] [Range(0, 10)] private int offset = 1; protected override void RunProceduralGeneration() { CreateRooms(); // 创建房间 } private void CreateRooms() { var roomsList = ProceduralGenerationAlgorithms.BinarySpacePartitioning(new BoundsInt((Vector3Int)startPosition, new Vector3Int(dungeonWidth, dungeonHeight, 0)), minRoomWidth, minRoomHeight); // 使用二叉空间分割算法创建房间列表 HashSet<Vector2Int> floor = new HashSet<Vector2Int>(); // 用于保存地板坐标的集合 floor = CreateSimpleRooms(roomsList); // 创建简单房间 tilemapVisualizer.PaintFloorTiles(floor); // 绘制地板砖块 WallGenerator.CreateWalls(floor, tilemapVisualizer); // 创建墙壁 } private HashSet<Vector2Int> CreateSimpleRooms(List<BoundsInt> roomsList) { HashSet<Vector2Int> floor = new HashSet<Vector2Int>(); // 用于保存地板坐标的集合 foreach (var room in roomsList) // 遍历房间列表 { for (int col = offset; col < room.size.x - offset; col++) // 遍历列 { for (int row = offset; row < room.size.y - offset; row++) // 遍历行 { Vector2Int position = (Vector2Int)room.min + new Vector2Int(col, row); // 计算地板坐标 floor.Add(position); // 添加地板坐标到集合中 } } } return floor; // 返回地板集合 } }
挂载脚本,配置参数
效果
使用走廊连接各个房间
修改RoomFirstDungeonGenerator
private void CreateRooms() { var roomsList = ProceduralGenerationAlgorithms.BinarySpacePartitioning(new BoundsInt((Vector3Int)startPosition, new Vector3Int(dungeonWidth, dungeonHeight, 0)), minRoomWidth, minRoomHeight); // 使用二叉空间分割算法创建房间列表 HashSet<Vector2Int> floor = new HashSet<Vector2Int>(); // 用于保存地板坐标的集合 floor = CreateSimpleRooms(roomsList); // 创建简单房间 List<Vector2Int> roomCenters = new List<Vector2Int>(); // 存储所有房间中心坐标的列表 foreach (var room in roomsList) // 遍历所有房间 { roomCenters.Add((Vector2Int)Vector3Int.RoundToInt(room.center)); // 将房间中心坐标转换为Vector2Int类型后添加到列表中 } HashSet<Vector2Int> corridors = ConnectRooms(roomCenters); // 连接所有房间,得到走廊的坐标集合 floor.UnionWith(corridors); // 将走廊坐标集合和地板坐标集合合并 tilemapVisualizer.PaintFloorTiles(floor); // 绘制地板砖块 WallGenerator.CreateWalls(floor, tilemapVisualizer); // 创建墙壁 } // 连接所有房间并返回地板坐标集合 private HashSet<Vector2Int> ConnectRooms(List<Vector2Int> roomCenters) { HashSet<Vector2Int> corridors = new HashSet<Vector2Int>(); var currentRoomCenter = roomCenters[Random.Range(0, roomCenters.Count)]; // 随机选择一个房间中心作为当前房间 roomCenters.Remove(currentRoomCenter); // 从房间中心列表中移除当前房间中心 while (roomCenters.Count > 0) // 当还有未连接的房间时循环 { Vector2Int closest = FindClosestPointTo(currentRoomCenter, roomCenters); // 找到距离当前房间中心最近的房间中心 roomCenters.Remove(closest); // 从房间中心列表中移除最近的房间中心 HashSet<Vector2Int> newCorridor = CreateCorridor(currentRoomCenter, closest); // 创建当前房间中心和最近房间中心之间的连接通道 currentRoomCenter = closest; // 将最近房间中心设置为当前房间中心 corridors.UnionWith(newCorridor); // 将新创建的通道添加到总通道集合中 } return corridors; // 返回所有通道的地板坐标集合 } // 寻找当前房间中心到最近房间的路径上的点 private Vector2Int FindClosestPointTo(Vector2Int currentRoomCenter, List<Vector2Int> roomCenters) { Vector2Int closest = Vector2Int.zero; // 最近的点的坐标 float distance = float.MaxValue; // 初始距离设为最大值 foreach (var position in roomCenters) // 遍历所有的房间中心 { float currentDistance = Vector2.Distance(position, currentRoomCenter); // 计算当前点与当前房间中心之间的距离 if (currentDistance < distance) // 如果当前距离比之前记录的最小距离小 { distance = currentDistance; // 更新最小距离 closest = position; // 更新最近的点的坐标 } } return closest; // 返回最近的点的坐标 } // 创建连接两个房间的走廊 private HashSet<Vector2Int> CreateCorridor(Vector2Int currentRoomCenter, Vector2Int destination) { HashSet<Vector2Int> corridor = new HashSet<Vector2Int>(); // 存储走廊坐标的集合 var position = currentRoomCenter; // 初始位置设为当前房间中心 corridor.Add(position); // 将初始位置添加到走廊坐标集合中 while (position.y != destination.y) // 沿着y轴移动直到到达目标位置的y坐标 { if (destination.y > position.y) // 如果目标位置的y坐标大于当前位置的y坐标 { position += Vector2Int.up; // 向上移动一格 } else if (destination.y < position.y) // 如果目标位置的y坐标小于当前位置的y坐标 { position += Vector2Int.down; // 向下移动一格 } corridor.Add(position); // 将新位置添加到走廊坐标集合中 } while (position.x != destination.x) // 沿着x轴移动直到到达目标位置的x坐标 { if (destination.x > position.x) // 如果目标位置的x坐标大于当前位置的x坐标 { position += Vector2Int.right; // 向右移动一格 } else if (destination.x < position.x) // 如果目标位置的x坐标小于当前位置的x坐标 { position += Vector2Int.left; // 向左移动一格 } corridor.Add(position); // 将新位置添加到走廊坐标集合中 } return corridor; // 返回走廊坐标的集合 }
生成效果
BSP和随机游走
前面生成的房间都是方形的,我们加点随机元素
修改RoomFirstDungeonGenerator
private void CreateRooms() { //。。。 // floor = CreateSimpleRooms(roomsList); // 创建简单房间 if (randomWalkRooms) { floor = CreateRoomsRandomly(roomsList);// 创建随机房间 } else { floor = CreateSimpleRooms(roomsList);// 创建简单房间 } //。。。 } private HashSet<Vector2Int> CreateRoomsRandomly(List<BoundsInt> roomsList) { HashSet<Vector2Int> floor = new HashSet<Vector2Int>(); // 存储地板坐标的集合 for (int i = 0; i < roomsList.Count; i++) // 遍历所有房间 { var roomBounds = roomsList[i]; // 获取当前房间的边界 var roomCenter = new Vector2Int(Mathf.RoundToInt(roomBounds.center.x), Mathf.RoundToInt(roomBounds.center.y)); // 计算当前房间的中心坐标 var roomFloor = RunRandomWalk(randomWalkParameters, roomCenter); // 使用随机步行算法获取当前房间的地板坐标集合 foreach (var position in roomFloor) // 遍历当前房间的地板坐标集合 { // 如果坐标在房间边界加上偏移量的范围内,将其添加到地板坐标集合中 if (position.x >= (roomBounds.xMin + offset) && position.x <= (roomBounds.xMax - offset) && position.y >= (roomBounds.yMin - offset) && position.y <= (roomBounds.yMax - offset)) { floor.Add(position); } } } return floor; // 返回地板坐标的集合 }
配置参数
效果,现在就更像是地牢了