/// <summary> /// Reset values to 0 because of script /// </summary> void ResetValues() { m_currentCell = 0; m_totalCells = 0; m_currentNeighbor = 0; m_previousCell = 0; m_wallToBreak = 0; m_endTileDistance = 0; m_markerCount = 0; m_endCell = null; if (m_markers != null) { m_markers.Clear(); } else { m_markers = new List <Marker>(); } if (m_cellStack != null) { m_cellStack.Clear(); } }
/// <summary> /// Checks for neighbours. If a neighbour exists, traverse to that cell and break the wall linking them /// Else backtrack to previous cell /// </summary> void GetNeighbor() { int index = 0; int[] neighbors = new int[4]; WALLINDEX[] connectingWall = new WALLINDEX[4]; // Check for neighbour EAST of current cell if (m_currentCell + 1 < m_totalCells && (m_currentCell + 1) % m_numColumns != 0) { if (!m_cells[m_currentCell + 1].visited) { neighbors[index] = m_currentCell + 1; connectingWall[index] = WALLINDEX.EAST; index++; } } // Check for neighbour WEST of current cell if (m_currentCell - 1 >= 0 && (m_currentCell % m_numColumns) != 0) { if (!m_cells[m_currentCell - 1].visited) { neighbors[index] = m_currentCell - 1; connectingWall[index] = WALLINDEX.WEST; index++; } } // Check for neighbour NORTH of current cell if (m_currentCell + m_numColumns < m_totalCells) { if (!m_cells[m_currentCell + m_numColumns].visited) { neighbors[index] = m_currentCell + m_numColumns; connectingWall[index] = WALLINDEX.NORTH; index++; } } // Check for neighbor SOUTH of current cell if (m_currentCell - m_numColumns >= 0) { if (!m_cells[m_currentCell - m_numColumns].visited) { neighbors[index] = m_currentCell - m_numColumns; connectingWall[index] = WALLINDEX.SOUTH; index++; } } // If a neighbour exists if (index != 0) { // Select a random neighbour and make the current cell that neighbour and destroy the wall linking them int selectedNeighbor = Random.Range(0, index); m_currentNeighbor = neighbors[selectedNeighbor]; m_wallToBreak = connectingWall[selectedNeighbor]; if (m_deadEnd) { m_deadEnd = false; } } else { SetMarker(); SetEndTile(); // Backtrack to previous cell if (m_previousCell > 0) { m_currentCell = m_cellStack[m_previousCell]; --m_previousCell; } } }