public List <Node> CalculateDungeon(int maxPasses, int roomMinWidth, int roomMinLength, float bottomCornerModifier, float topCornerModifier, int roomOffset, int corridorWidth, EnemySpawner enemSpawner, DungeonCreator dungeonCreator, ItemSpawner itemSpawner, ObjectSpawner objSpawner) { BinarySpacePartitioner bsp = new BinarySpacePartitioner(dungeonWidth, dungeonLength); allSpaceNodes = bsp.PrepareNodesCollection(maxPasses, roomMinWidth, roomMinLength); List <Node> roomSpaces = StructureHelper.TraverseGraphToExtractLowestLeaves(bsp.rootNode); RoomGenerator roomGenerator = new RoomGenerator(maxPasses, roomMinLength, roomMinWidth); List <RoomNode> roomList = roomGenerator.GenerateRoomsInGivenSpaces(roomSpaces, bottomCornerModifier, topCornerModifier, roomOffset); //first room will be the spawn point for the player RoomNode firstRoom = roomList[0]; UnityEngine.CharacterController player = GameObject.FindWithTag("Player").GetComponent <UnityEngine.CharacterController>(); Vector2Int firstRoomCenter = StructureHelper.CalculateMiddlePoint(firstRoom.BottomLeftCorner, firstRoom.TopRightCorner); Vector3 newPos = new Vector3(firstRoomCenter.x, 0.0f, firstRoomCenter.y); player.enabled = false; player.transform.position = newPos; player.enabled = true; //generate spawn points before we add the corridors for (int i = 0; i < roomList.Count; i++) { RoomNode room = roomList[i]; if (enemSpawner != null) { GameObject newRoom = new GameObject("RoomObj", typeof(Room), typeof(BoxCollider)); newRoom.tag = "RoomObject"; BoxCollider col = newRoom.GetComponent <BoxCollider>(); col.isTrigger = true; col.size = new Vector3(1.2f, 1.2f, 1.2f); Vector2Int roomPos = StructureHelper.CalculateMiddlePoint(room.BottomLeftCorner, room.TopRightCorner); newRoom.transform.position = new Vector3(roomPos.x, 2, roomPos.y); Room roomComp = newRoom.GetComponent <Room>(); room.roomObjReference = roomComp; newRoom.transform.localScale = new Vector3(room.Width, 4, room.Length); roomComp.enemySpawnPoints = new List <Vector3>(); roomComp.itemSpawnPoints = new List <Vector3>(); roomComp.weaponSpawnPoints = new List <Vector3>(); roomComp.objectSpawnPoints = new List <Vector3>(); roomComp.doors = new List <Transform>(); dungeonCreator.spawnedRooms.Add(roomComp); if (i != 0) { enemSpawner.GenerateEnemySpawnPointsForRoom(room); itemSpawner.GenerateItemSpawnPointsForRoom(room); objSpawner.GenerateObjectSpawnPointsForRoom(room); } } } CorridorGenerator corridorGenerator = new CorridorGenerator(); var corridorList = corridorGenerator.CreateCorridor(allSpaceNodes, corridorWidth); dungeonCreator.GenerateDoors(corridorList); return(new List <Node>(roomList).Concat(corridorList).ToList()); }
private void ProcessRoomInRelationUpOrDown(Node structure1, Node structure2) { Node bottomStructure = null; List <Node> structureBottmChildren = StructureHelper.TraverseGraphToExtractLowestLeaves(structure1); Node topStructure = null; List <Node> structureAboveChildren = StructureHelper.TraverseGraphToExtractLowestLeaves(structure2); var sortedBottomStructure = structureBottmChildren.OrderByDescending(child => child.TopRightCorner.y).ToList(); if (sortedBottomStructure.Count == 1) { bottomStructure = structureBottmChildren[0]; } else { int maxY = sortedBottomStructure[0].TopLeftCorner.y; sortedBottomStructure = sortedBottomStructure.Where(child => Mathf.Abs(maxY - child.TopLeftCorner.y) < 10).ToList(); int index = UnityEngine.Random.Range(0, sortedBottomStructure.Count); bottomStructure = sortedBottomStructure[index]; } var possibleNeighboursInTopStructure = structureAboveChildren.Where( child => GetValidXForNeighbourUpDown( bottomStructure.TopLeftCorner, bottomStructure.TopRightCorner, child.BottomLeftCorner, child.BottomRightCorner) != -1).OrderBy(child => child.BottomRightCorner.y).ToList(); if (possibleNeighboursInTopStructure.Count == 0) { topStructure = structure2; } else { topStructure = possibleNeighboursInTopStructure[0]; } int x = GetValidXForNeighbourUpDown( bottomStructure.TopLeftCorner, bottomStructure.TopRightCorner, topStructure.BottomLeftCorner, topStructure.BottomRightCorner); while (x == -1 && sortedBottomStructure.Count > 1) { sortedBottomStructure = sortedBottomStructure.Where(child => child.TopLeftCorner.x != topStructure.TopLeftCorner.x).ToList(); bottomStructure = sortedBottomStructure[0]; x = GetValidXForNeighbourUpDown( bottomStructure.TopLeftCorner, bottomStructure.TopRightCorner, topStructure.BottomLeftCorner, topStructure.BottomRightCorner); } BottomLeftCorner = new Vector2Int(x, bottomStructure.TopLeftCorner.y); TopRightCorner = new Vector2Int(x + this.corridorWidth, topStructure.BottomLeftCorner.y); connectedRooms[0] = ((RoomNode)bottomStructure).roomObjReference; connectedRooms[1] = ((RoomNode)topStructure).roomObjReference; }
private void ProcessRoomInRelationRightOrLeft(Node structure1, Node structure2) { Node leftStructure = null; List <Node> leftStructureChildren = StructureHelper.TraverseGraphToExtractLowestLeaves(structure1); Node rightStructure = null; List <Node> rightStructureChildren = StructureHelper.TraverseGraphToExtractLowestLeaves(structure2); var sortedLeftStructure = leftStructureChildren.OrderByDescending(child => child.TopRightCorner.x).ToList(); if (sortedLeftStructure.Count == 1) { leftStructure = sortedLeftStructure[0]; } else { int maxX = sortedLeftStructure[0].TopRightCorner.x; sortedLeftStructure = sortedLeftStructure.Where(children => Math.Abs(maxX - children.TopRightCorner.x) < 10).ToList(); int index = UnityEngine.Random.Range(0, sortedLeftStructure.Count); leftStructure = sortedLeftStructure[index]; } var possibleNeighboursInRightStructureList = rightStructureChildren.Where( child => GetValidYForNeighourLeftRight( leftStructure.TopRightCorner, leftStructure.BottomRightCorner, child.TopLeftCorner, child.BottomLeftCorner ) != -1 ).OrderBy(child => child.BottomRightCorner.x).ToList(); if (possibleNeighboursInRightStructureList.Count <= 0) { rightStructure = structure2; } else { rightStructure = possibleNeighboursInRightStructureList[0]; } int y = GetValidYForNeighourLeftRight(leftStructure.TopLeftCorner, leftStructure.BottomRightCorner, rightStructure.TopLeftCorner, rightStructure.BottomLeftCorner); while (y == -1 && sortedLeftStructure.Count > 1) { sortedLeftStructure = sortedLeftStructure.Where( child => child.TopLeftCorner.y != leftStructure.TopLeftCorner.y).ToList(); leftStructure = sortedLeftStructure[0]; y = GetValidYForNeighourLeftRight(leftStructure.TopLeftCorner, leftStructure.BottomRightCorner, rightStructure.TopLeftCorner, rightStructure.BottomLeftCorner); } BottomLeftCorner = new Vector2Int(leftStructure.BottomRightCorner.x, y); TopRightCorner = new Vector2Int(rightStructure.TopLeftCorner.x, y + this.corridorWidth); connectedRooms[0] = ((RoomNode)leftStructure).roomObjReference; connectedRooms[1] = ((RoomNode)rightStructure).roomObjReference; }