/// <summary> /// Recursive generator method /// </summary> /// <param name="containingBranch">Branch in which it is contained</param> /// <param name="previousLoc">Previous generated location</param> /// <param name="previousDir">Previous generated direction</param> /// <param name="maxLength">Max branch length</param> /// <param name="count">Amount created</param> /// <param name="canHaveBranch">Can have sub branches</param> private void Generate(Branch containingBranch, Vector3Int previousLoc, Vector3Int previousDir, int maxLength, int count, bool canHaveBranch) { Vector3Int newLoc = previousLoc + previousDir; Vector3Int newDir = previousDir; // Check if it can continue the same direction bool selected = !PositionOccupied(newLoc); // Determine if it should change direction bool changeDir = DirectionChangeRoll || !selected; if (changeDir) { selected = false; ShuffleDirections(); for (int i = 0; i < AllPossibleDirections.Length; i++) { Vector3Int tempLoc = AllPossibleDirections[i] + previousLoc; if (!PositionOccupied(tempLoc)) { newDir = AllPossibleDirections[i]; newLoc = tempLoc; selected = true; break; } } } // If the selection was possible if (selected && count < maxLength) { _takenLocations.Add(newLoc); containingBranch.AddNewRoomPosition(newLoc); Generate(containingBranch, newLoc, newDir, maxLength, count + 1, canHaveBranch); // Try yo place the exit if (!_exitPlaced) { _exitPlaced = true; PlaceExit(newLoc); } // Try to create new branch if (canHaveBranch && HasOpenNeighbor(newLoc) && NewBranchRoll) { Branch subBranch = new Branch(containingBranch); containingBranch.AddNewSubBranch(subBranch); Generate(subBranch, newLoc, newDir, maxLength / 4, 0, false); } } }