private void DoNextGenerationStep(List <MazeCell> activeCells) { // Get the last active cell to try to expand from it int currentIndex = activeCells.Count - 1; //Random.Range(0,activeCells.Count); MazeCell currentCell = activeCells[currentIndex]; // Cell is already fully expanded if (currentCell.IsFullyInitialized()) { activeCells.RemoveAt(currentIndex); return; } // Create neighbor from expansion MazeDirection direction = currentCell.RandomUninitializedDirection(); IntVector2 coordinates = currentCell.coordinates + direction.ToIntVector2(); // It is inside the maze if (ContainsCoordinates(coordinates)) { MazeCell neighbor = GetCell(coordinates); // Doesn't already exist if (neighbor == null) { neighbor = CreateCell(coordinates); CreatePassage(currentCell, neighbor, direction); activeCells.Add(neighbor); } else if (currentCell.room == neighbor.room && openRoom) { CreatePassageInSameRoom(currentCell, neighbor, direction); } // Already exists else { CreateWall(currentCell, neighbor, direction); } } // Outside maze else { CreateWall(currentCell, null, direction); } }