DungeonCell Dig(DungeonCell fromCell, Direction dir) { DungeonCell toCell = null; DungeonCell midCell = null; switch (dir) { case Direction.N: toCell = cells[fromCell.i, fromCell.j + 2]; midCell = cells[fromCell.i, fromCell.j + 1]; break; case Direction.E: toCell = cells[fromCell.i + 2, fromCell.j]; midCell = cells[fromCell.i + 1, fromCell.j]; break; case Direction.S: toCell = cells[fromCell.i, fromCell.j - 2]; midCell = cells[fromCell.i, fromCell.j - 1]; break; case Direction.W: toCell = cells[fromCell.i - 2, fromCell.j]; midCell = cells[fromCell.i - 1, fromCell.j]; break; } toCell.SetEmpty(); midCell.SetEmpty(); return(toCell); }
IEnumerator AlgorithmCO() { // Initialise the dungeon as full of walls for (int i = 0; i < dungeonSize; i++) { for (int j = 0; j < dungeonSize; j++) { cells[i, j].SetWall(); } } // Choose a start cell int start_i = 0; // dungeonSize / 2; int start_j = 0; //dungeonSize / 2; cells[start_i, start_j].SetEmpty(); yield return(null); Stack <DungeonCell> stack = new Stack <DungeonCell>(); stack.Push(cells[start_i, start_j]); while (stack.Count > 0) { // Peek at the top cell on the stack DungeonCell currentCell = stack.Peek(); // Check the possible directions List <Direction> possibleDirections = GetPossibleDirections(currentCell.i, currentCell.j); if (possibleDirections.Count == 0) { // No more directions from here, let's remove this from the stack stack.Pop(); } else { // Get a random possible direction int index = Random.Range(0, possibleDirections.Count); Direction dir = possibleDirections[index]; // We dig to the new cell var newCell = Dig(currentCell, dir); // We add it to the stack stack.Push(newCell); } yield return(new WaitForSeconds(0.0001f)); } }