public int CompareTo(CellCost <T> cellCost) { var compare = f.CompareTo(cellCost.f); if (compare == 0) { compare = h.CompareTo(cellCost.h); } return(compare); }
private bool TryGetMinCostCellFromNeighbours(CellCost <GridCell> currentCellCost, out List <CellCost <GridCell> > minCostCells) { var isImpasse = true; minCostCells = new List <CellCost <GridCell> >(); var minCostCell = new CellCost <GridCell>(); var neighbours = gridManager.GetNeighbours(currentCellCost.gridCell); for (var i = 0; i < neighbours.Count; i++) { var item = neighbours[i]; if (!item.walkable || closeCells.Contains(item) || OpenListContains(item)) { continue; } var cellCost = new CellCost <GridCell> { gridCell = item, g = currentCellCost.g + item.GetG(currentCellCost.gridCell), h = item.GetH(endCell), parent = currentCellCost }; if (minCostCell.CompareTo(cellCost) > 0 || minCostCell.f == 0) { minCostCell = cellCost; if (minCostCells.Count > 0) { minCostCells.Clear(); } minCostCells.Add(cellCost); } else if (minCostCell.CompareTo(cellCost) == 0) { minCostCells.Add(cellCost); } isImpasse = false; } return(!isImpasse); }
public IEnumerator SearchMinCostLinePath() { var spw = new System.Diagnostics.Stopwatch(); spw.Start(); while (true) { var assessCell = OpenListPopFirst(); if (assessCell.gridCell.worldPosition == endCell.worldPosition) { spw.Stop(); Debug.Log("use time : " + spw.ElapsedMilliseconds); endCellCost = assessCell; yield break; } ; List <CellCost <GridCell> > minCostCells; if (TryGetMinCostCellFromNeighbours(assessCell, out minCostCells)) { openCellCosts.AddRange(minCostCells); foreach (var item in minCostCells) { gridManager.DrawCell(item.gridCell, true, true); } } else { //Debug.Log("close cell count : " + closeCells.Count); openCellCosts.Remove(assessCell); closeCells.Add(assessCell.gridCell); gridManager.DrawCell(assessCell.gridCell, true, true, (m) => { m.color = Color.yellow; }); } OpenListSort(); yield return(new WaitForSeconds(0.05f)); } }