IEnumerator FindPath(Vector2 from, Vector2 to) { mCurrentGridPos = from; Vector2 endPos = to; while (mCurrentGridPos != to) { MazeCellData cellToMove = _MazeGenerator._MazeCells[(int)mCurrentGridPos.x, (int)mCurrentGridPos.y]; List <Movements> possibleMovements = _MazeGenerator.FindNearestMovableCells((int)mCurrentGridPos.x, (int)mCurrentGridPos.y); float minimalCost = 0; for (int i = 0; i < possibleMovements.Count; i++) { float currentCost = CalculateCost(new Vector2(possibleMovements[i]._Cell._X, possibleMovements[i]._Cell._Y), mCurrentGridPos, endPos); if (minimalCost == 0) { minimalCost = currentCost; cellToMove = possibleMovements[i]._Cell; } else { if (currentCost < minimalCost) { minimalCost = currentCost; cellToMove = possibleMovements[i]._Cell; } } } transform.position = new Vector3(cellToMove._X, 0, cellToMove._Y); mCurrentGridPos = new Vector2(cellToMove._X, cellToMove._Y); yield return(new WaitForSeconds(0.5f)); } }
private void FindMazePathFrom(int x, int y) { _MazeCells[x, y]._IsVisited = true; _MazeCells[x, y]._Floor.GetComponent <Renderer>().material.color = Color.red; List <Movements> unvisitedCells = FindNearestUnvisitedCells(x, y); int randValue = default; if (unvisitedCells.Count > 0) { randValue = Random.Range(0, unvisitedCells.Count); } else { return; } MazeCellData foundCell = unvisitedCells[randValue]._Cell; int foundDir = unvisitedCells[randValue]._Direction; RemoveWallFromTo(_MazeCells[x, y], foundCell, foundDir); foundCell._IsVisited = true; foundCell._Floor.GetComponent <Renderer>().material.color = Color.red; //Recursion till we have no more possible movements FindMazePathFrom(foundCell._X, foundCell._Y); }
private void RemoveWall(MazeCellData cell, int dir) { if (dir == 0) { cell._BackWall.SetActive(false); } else if (dir == 1) { cell._LeftWall.SetActive(false); } else if (dir == 2) { cell._RightWall.SetActive(false); } else if (dir == 3) { cell._FrontWall.SetActive(false); } }
private void CreateMazeCells() { for (int i = 0; i < _MazeCells.GetLength(0); i++) { for (int j = 0; j < _MazeCells.GetLength(1); j++) { GameObject mazeCell = Instantiate(m_MazeCell); mazeCell.transform.position = new Vector3(i, 0, j); mazeCell.name = "Cell " + i + "_" + j; mazeCell.transform.parent = transform; MazeCellData mazeCellData = mazeCell.GetComponent <MazeCellData>(); mazeCellData._IsVisited = false; mazeCellData._X = i; mazeCellData._Y = j; _MazeCells[i, j] = mazeCellData; } } Camera.main.transform.position = new Vector3(m_MazeWidth / 2, m_MazeWidth + m_MazeLength, m_MazeLength / 2); GenerateMaze(); }
private void RemoveWallFromTo(MazeCellData fromCell, MazeCellData toCell, int dir) { if (dir == 0) { fromCell._BackWall.SetActive(false); toCell._FrontWall.SetActive(false); } else if (dir == 1) { fromCell._LeftWall.SetActive(false); toCell._RightWall.SetActive(false); } else if (dir == 2) { fromCell._RightWall.SetActive(false); toCell._LeftWall.SetActive(false); } else if (dir == 3) { fromCell._FrontWall.SetActive(false); toCell._BackWall.SetActive(false); } }
public Movements(MazeCellData cell, int dir) { _Cell = cell; _Direction = dir; }
private void OnMazeClicked(MazeCellData mazeCell) { Debug.Log(" >> " + mazeCell._X + " " + mazeCell._Y); StartCoroutine(FindPath(mCurrentGridPos, new Vector2(mazeCell._X, mazeCell._Y))); }