예제 #1
0
        public IEnumerator BreadthFirstTravelPathTrack()
        {
            Queue <GridCell2D> frontier = new Queue <GridCell2D>();

            cameFrom.Clear();

            frontier.Enqueue(grid.StartCell);
            cameFrom.Add(grid.StartCell, null);

            while (frontier.Count > 0)
            {
                GridCell2D current = frontier.Dequeue();
                foreach (GridCell2D t in current.Neighbors)
                {
                    if (t.IsWall())
                    {
                        continue;
                    }

                    if (cameFrom.ContainsKey(t))
                    {
                        continue;
                    }

                    frontier.Enqueue(t);
                    cameFrom.Add(t, current);
                }

                if (current == grid.TargetCell)
                {
                    break;
                }

                foreach (GridCell2D t in cameFrom.Keys)
                {
                    GridCell2D value = cameFrom[t];
                    if (value == null)
                    {
                        continue;
                    }
                    value.SetColor(GridCell2D.visitedColor);
                }

                foreach (GridCell2D t in frontier)
                {
                    t.SetColor(GridCell2D.frontierColor);
                }

                yield return(new WaitForSeconds(animationWait));
            }
        }
예제 #2
0
        private void DrawPathToTarget()
        {
            if (grid.TargetCell == null)
            {
                return;
            }

            GridCell2D current = grid.TargetCell;

            while (current != grid.StartCell)
            {
                if (cameFrom.ContainsKey(current) == false)
                {
                    break;
                }

                current = cameFrom[current];
                current.SetColor(GridCell2D.pathColor);
            }
        }
예제 #3
0
        public IEnumerator BreadthFirstTravelPathTrackAstar()
        {
            List <GridCell2D> frontier = new List <GridCell2D>();

            cameFrom.Clear();

            frontier.Add(grid.StartCell);
            frontier.Sort(new GridCell2DDistanceToTargetAndCostComparer());
            cameFrom.Add(grid.StartCell, null);
            Dictionary <GridCell2D, int> costSoFar = new Dictionary <GridCell2D, int>();

            costSoFar.Add(grid.StartCell, grid.StartCell.Cost);

            while (frontier.Count > 0)
            {
                GridCell2D current = frontier[frontier.Count - 1];
                current.SetColor(GridCell2D.visitedColor);
                frontier.RemoveAt(frontier.Count - 1);

                foreach (GridCell2D next in current.Neighbors)
                {
                    if (next.IsWall())
                    {
                        continue;
                    }

                    int newCost = costSoFar[current] + next.Cost;

                    if (costSoFar.ContainsKey(next) == false ||
                        newCost < costSoFar[next])
                    {
                        if (costSoFar.ContainsKey(next) == false)
                        {
                            costSoFar.Add(next, newCost);
                            next.Cost = newCost;
                        }
                        else
                        {
                            costSoFar[next] = newCost;
                            next.Cost       = newCost;
                        }

                        if (next == grid.TargetCell)
                        {
                            if (cameFrom.ContainsKey(next) == false)
                            {
                                cameFrom.Add(next, current);
                            }

                            frontier.Clear();
                            break;
                        }

                        next.DistanceToTarget = ManhattanDistance(next.Index, grid.TargetCell.Index);
                        frontier.Add(next);
                        frontier.Sort(new GridCell2DDistanceToTargetAndCostComparer());

                        if (cameFrom.ContainsKey(next) == false)
                        {
                            cameFrom.Add(next, current);
                        }
                    }
                }

                //if (current == grid.TargetCell)
                //    break;

                foreach (GridCell2D t in cameFrom.Keys)
                {
                    GridCell2D value = cameFrom[t];
                    if (value == null)
                    {
                        continue;
                    }
                    value.SetColor(GridCell2D.visitedColor);
                }

                foreach (GridCell2D t in frontier)
                {
                    t.SetColor(GridCell2D.frontierColor);
                }

                yield return(new WaitForSeconds(animationWait));
            }
        }