public WayCell(Cell finish, WayCell preview, float x, float y, float toStartDistance) : base(x, y, finish.Detalisation) { FinishCell = finish; PreviewCell = preview; InitializeWeight(toStartDistance); }
private static Stack <WayCell> CalculateWay(WayCell lastCell) { Debug.Log("Начали калькулировать путь"); Stack <WayCell> way = new Stack <WayCell>(); WayCell sameCell = lastCell; do { way.Push(sameCell); sameCell = sameCell.PreviewCell; } while (sameCell != null); return(way); }
private static Stack <WayCell> GetWay(Vector3 startPos, Vector3 finishPos, float detalisation, float characterRadius, bool diagonalAdjacent = false) { Detalisation = detalisation; MaxDeviation = GetMaxDeviation(Detalisation, FindPower); DiagonalAdjacent = diagonalAdjacent; Waiting = new List <WayCell>(); Checked = new List <WayCell>(); Stack <WayCell> way = new Stack <WayCell>(); CharacterRadius = characterRadius; Finish = GetAvailableTarget(finishPos); // Debug.LogError("Финиш: " + Finish); // Finish = new WayCell(finishCell, null, finishCell.X, finishCell.Y, Detalisation,float.MaxValue); if (Finish == null) { //Нет разницы, какая цель, поскольку она недействительна Start = new WayCell(new Cell(0f, 0f, 1f), null, startPos, 0f); way.Push(Start); Debug.Log("Финиш нулевой"); return(way); } Start = new WayCell(Finish, null, startPos, 0f); DiagonalAdjacent = diagonalAdjacent; Checked.Add(Start); Start.AddNewAdjacents(ref Waiting, diagonalAdjacent); while (Waiting.Count > 0) { //Вытаскиваем клетку с самым низким весом из ожидающих. //Какая-то клетка присвоить из ожидающих такую клетку, вес которой равен минимальному весу из ожидающих. var toCheck = Waiting.Where(x => x.Weight == Waiting.Min(y => y.Weight)).FirstOrDefault(); //Debug.Log("toCheck.X" + toCheck.X); //Debug.Log("Finish.X" + Finish.X); if (toCheck.Equals(Finish)) { Finish.PreviewCell = toCheck.PreviewCell; return(CalculateWay(Finish)); } else { Waiting.Remove(toCheck); //Случайное число 9, определяющее максимальное количество условий List <bool> conditions = new List <bool>(); conditions.Add(Physics2D.OverlapCircle(new Vector2(toCheck.X, toCheck.Y), CharacterRadius, LayerMask.GetMask("Wall"))); // conditions.Add(toCheck.GetDistanсe(Start) > MaxDeviation); if (Check.Disjunction(conditions)) { Checked.Add(toCheck); } else { var justHaveCells = Checked.Where(x => x.X == toCheck.X).Where(y => y.Y == toCheck.Y); if (!justHaveCells.Any()) { Checked.Add(toCheck); toCheck.AddNewAdjacents(ref Waiting, diagonalAdjacent); } //else //{ // var sameCells = justHaveCells.ToList(); //} } } } Debug.Log("Окончательный путь не найден"); return(way); }
public WayCell(Cell finish, WayCell preview, Cell currentCell, float toStartDistance) : this(finish, preview, currentCell.X, currentCell.Y, toStartDistance) { }
public WayCell(Cell finish, WayCell preview, Vector3 coord, float toStartDistance) : this(finish, preview, coord.x, coord.y, toStartDistance) { }