//This is the same idea as the left/right function just above, but on the X-Axis private void ProcessRoomRelationUpOrDown(Node structure1, Node structure2) { Node bottomStructure = null; List <Node> structureBottomChildren = StructureHelper.TraverseGraphToExtractLowestLeaf(structure1); Node topStructure = null; List <Node> structureTopChildren = StructureHelper.TraverseGraphToExtractLowestLeaf(structure2); List <Node> sortedBottomStructure = structureBottomChildren.OrderByDescending(child => child.TopRightAreaCorner.y).ToList(); if (sortedBottomStructure.Count == 1) { bottomStructure = structureBottomChildren[0]; } else { int maxY = sortedBottomStructure[0].TopLeftAreaCorner.y; sortedBottomStructure = sortedBottomStructure.Where(child => Mathf.Abs(maxY - child.TopLeftAreaCorner.y) < 10).ToList(); int index = Random.Range(0, sortedBottomStructure.Count); bottomStructure = sortedBottomStructure[index]; } List <Node> topStructurePossibleNeighbors = structureTopChildren .Where(child => GetXForNeighbor(bottomStructure.TopLeftAreaCorner, bottomStructure.TopRightAreaCorner, child.BottomLeftAreaCorner, child.BottomRightAreaCorner) != 1).OrderBy(child => child.BottomRightAreaCorner.y).ToList(); if (topStructurePossibleNeighbors.Count == 0) { topStructure = structure2; } else { topStructure = topStructurePossibleNeighbors[0]; } int x = GetXForNeighbor(bottomStructure.TopLeftAreaCorner, bottomStructure.TopRightAreaCorner, topStructure.BottomLeftAreaCorner, topStructure.BottomRightAreaCorner); while (x == checkValue && sortedBottomStructure.Count > 1) { sortedBottomStructure = sortedBottomStructure.Where(child => child.TopLeftAreaCorner.x != topStructure.TopLeftAreaCorner.x).ToList(); bottomStructure = sortedBottomStructure[0]; x = GetXForNeighbor(bottomStructure.TopLeftAreaCorner, bottomStructure.TopRightAreaCorner, topStructure.BottomLeftAreaCorner, topStructure.BottomRightAreaCorner); } BottomLeftAreaCorner = new Vector2Int(x, bottomStructure.TopLeftAreaCorner.y); TopRightAreaCorner = new Vector2Int(x + this.corridorWidth, topStructure.BottomLeftAreaCorner.y); }
public List <Node> BuildLevel( int maxIterations, int roomWidthMin, int roomLengthMin, int corridorWidth, float bottomLeftPointModifier, float topRightPointModifier, int offset) { BinarySpacePartitioner bsp = new BinarySpacePartitioner(levelWidth, levelLength); allNodesCollection = bsp.PrepareNodesCollection(maxIterations, roomWidthMin, roomLengthMin); List <Node> roomSpaces = StructureHelper.TraverseGraphToExtractLowestLeaf(bsp.RootNode); RoomGenerator roomGenerator = new RoomGenerator(maxIterations, roomLengthMin, roomWidthMin); List <RoomNode> roomList = roomGenerator.GenerateRoomInGivenSpace(roomSpaces, bottomLeftPointModifier, topRightPointModifier, offset); CorridorGenerator corridorGenerator = new CorridorGenerator(); List <Node> corridors = corridorGenerator.CreateCorridor(allNodesCollection, corridorWidth); return(new List <Node>(roomList).Concat(corridors).ToList()); }
private void ProcessRoomRelationRightOrLeft(Node structure1, Node structure2) { Node leftStructure = null; List <Node> leftStructureChildren = StructureHelper.TraverseGraphToExtractLowestLeaf(structure1); Node rightStructure = null; List <Node> rightStructureChildren = StructureHelper.TraverseGraphToExtractLowestLeaf(structure2); //We extracted all the children from the two structures before ordering the children list by the top right area for the left structure List <Node> sortedLeftStructure = leftStructureChildren.OrderByDescending(child => child.TopRightAreaCorner.x).ToList(); if (sortedLeftStructure.Count == 1) { leftStructure = sortedLeftStructure[0]; } else { // we then choose one of the children to be the left structure int maxX = sortedLeftStructure[0].TopRightAreaCorner.x; sortedLeftStructure = sortedLeftStructure.Where(children => Math.Abs(maxX - children.TopRightAreaCorner.x) < 10).ToList(); int index = Random.Range(0, sortedLeftStructure.Count); leftStructure = sortedLeftStructure[index]; } // Using the left structure we just found, we can search for possible neighbors for the left one in the right structure List <Node> rightStructurePossibleNeighbors = rightStructureChildren.Where(child => GetYForNeighbor(leftStructure.TopRightAreaCorner, leftStructure.BottomRightAreaCorner, child.TopLeftAreaCorner, child.BottomLeftAreaCorner) != -1) .OrderBy(child => child.BottomRightAreaCorner.x).ToList(); if (rightStructurePossibleNeighbors.Count <= 0) { rightStructure = structure2; } else { //And we take the first one in the list rightStructure = rightStructurePossibleNeighbors[0]; } //Y will give us where the corridor has to be placed on the Z-Axis on the side of the structure int y = GetYForNeighbor(leftStructure.TopLeftAreaCorner, leftStructure.BottomRightAreaCorner, rightStructure.TopLeftAreaCorner, rightStructure.BottomLeftAreaCorner); while (y == checkValue && sortedLeftStructure.Count > 1) { //we pass through the sorted structure to see which one is the best candidate for the second end of the corridor sortedLeftStructure = sortedLeftStructure.Where(child => child.TopLeftAreaCorner.y != leftStructure.TopLeftAreaCorner.y).ToList(); leftStructure = sortedLeftStructure[0]; y = GetYForNeighbor(leftStructure.TopLeftAreaCorner, leftStructure.BottomRightAreaCorner, rightStructure.TopLeftAreaCorner, rightStructure.BottomLeftAreaCorner); } BottomLeftAreaCorner = new Vector2Int(leftStructure.BottomRightAreaCorner.x, y); TopRightAreaCorner = new Vector2Int(rightStructure.TopLeftAreaCorner.x, y + this.corridorWidth); }