private Stack <GameObject> GetPath(GameObject currentCell) { Stack <GameObject> pathCells = new Stack <GameObject>(); PathFindingCellModell currentCellModel = currentCell.GetComponent <PathFindingCellModell>(); GameObject nextPathCell = currentCellModel.PreviousCell; while (nextPathCell != null) { pathCells.Push(nextPathCell); nextPathCell = nextPathCell.GetComponent <PathFindingCellModell>().PreviousCell; } pathCells.Pop(); return(pathCells); }
public int CompareTo(object obj) { if (obj == null) { return(1); } PathFindingCellModell otherCell = obj as PathFindingCellModell; if (otherCell != null) { return(DistanceValue.CompareTo(otherCell.DistanceValue)); } else { throw new ArgumentException("Object is not a PathFindingCell"); } }
public void AStartPathFinding() { m_StartCell = m_MazeComponent.StartingCell; m_FinishCell = m_MazeComponent.FinishCell; PriorityQueue <PathFindingCellModell> cells = new PriorityQueue <PathFindingCellModell>(); HashSet <PathFindingCellModell> closedCells = new HashSet <PathFindingCellModell>(); PathFindingCellModell startingCellModel = m_StartCell.GetComponent <PathFindingCellModell>(); cells.Enqueue(startingCellModel); closedCells.Add(startingCellModel); PathFindingCellModell currentCell; while (cells.Count() > 0) { currentCell = cells.Dequeue(); if (currentCell.transform.gameObject.Equals(m_FinishCell)) { Stack <GameObject> path = GetPath(currentCell.transform.gameObject); ShowPath(path); break; } foreach (GameObject pathCell in currentCell.Links) { PathFindingCellModell pathCellModel = pathCell.GetComponent <PathFindingCellModell>(); if (!closedCells.Contains(pathCellModel)) { closedCells.Add(pathCellModel); pathCellModel.PreviousCell = currentCell.transform.gameObject; cells.Enqueue(pathCellModel); } } } }