public ProceduralPathArrow() { prevArrow = null; nextArrow = null; PossibleMoves = new PossibleMoves { Up = true, Down = true, Left = true, Right = true }; }
//Sometimes arrows are created diagonally from the previous IEnumerator GenerateRandomPath() { bool pathCreateSuccess = false; do { if (startingArrow != null) { Destroy(startingArrow.gameObject); } startingArrow = CreateArrow(startingColumn, startingRow, startingDirection); currentArrow = startingArrow; yield return new WaitForSeconds(0.5f); int currentRow = startingArrow.row; int currentColumn = startingArrow.column; Direction currentDirection = startingArrow.Direction; while (true) { //If the current arrow has no possible moves, delete it and return to the previous arrow, give the previous arrow a new possible direction while (currentArrow.PossibleMoves.NoPossibleMoves) { Debug.Log("This arrow has no possible moves!"); //Should never happen if (currentArrow.prevArrow == null) { break; } currentArrow.prevArrow.DisableFacingDirection(); currentArrow.prevArrow.RandomizePossibleDirection(); GameObject temp = currentArrow.gameObject; currentArrow = currentArrow.prevArrow; currentColumn = currentArrow.column; currentRow = currentArrow.row; currentArrow.nextArrow = null; Destroy(temp); } if (currentArrow.prevArrow == null && currentArrow.PossibleMoves.NoPossibleMoves) { Debug.Log("Could not construct the map!"); pathCreateSuccess = false; break; } //Move in front of the current arrow, end if there is an edge //Check for arrow collisions //If there is an arrow in the way, use a different direction currentDirection = currentArrow.Direction; GetNextSpace(currentDirection, ref currentColumn, ref currentRow); //Edge of the map (Check BEFORE checking arrow collisions if (currentColumn < 0 || currentColumn > columns - 1 || currentRow < 0 || currentRow > rows - 1) { //Move back a space currentColumn = currentArrow.column; currentRow = currentArrow.row; currentArrow.DisableFacingDirection(); currentArrow.RandomizePossibleDirection(); Debug.Log("Hit the edge of the map!"); continue; } if (arrowList[currentRow * rows + currentColumn] != null) { //Move back a space currentColumn = currentArrow.column; currentRow = currentArrow.row; currentArrow.DisableFacingDirection(); currentArrow.RandomizePossibleDirection(); Debug.Log("Arrow collision!"); continue; } if (currentColumn == endColumn && currentRow == endRow) { Debug.Log("Reached the defendable object!"); pathCreateSuccess = true; break; } //At this point, there should be an empty space in front of the current arrow //Create a new arrow in front of the current arrow ProceduralPathArrow nextArrow = CreateArrow(currentColumn, currentRow, currentDirection); nextArrow.DisableOppositeDirection(); //Randomize the new arrow's direction currentDirection = RandomDirection(currentDirection); nextArrow.Direction = currentDirection; currentArrow.nextArrow = nextArrow; nextArrow.prevArrow = currentArrow; currentArrow = nextArrow; } } while (!pathCreateSuccess); }