Esempio n. 1
0
 public void StartShortestPath(PathfinderTile start, PathfinderTile end)
 {
     this.start         = start;
     this.end           = end;
     shortestPath       = null;
     shortestPathThread = new System.Threading.Thread(ShortestPath);
     shortestPathThread.Start();
 }
    void Update()
    {
        PathfinderPath shortest = finder.CheckShortestPath();

        if (shortest != null)
        {
            Debug.Log(shortest.ToString());
            clickedUnit.MovePath = shortest;
            finder.ClearShortestPath();
        }
    }
Esempio n. 3
0
    public PathfinderPath Clone()
    {
        PathfinderPath path = new PathfinderPath();

        for (int i = 0; i < tileList.Count; i++)
        {
            path.tileList.Add(tileList[i]);
        }
        path.cost = cost;
        return(path);
    }
Esempio n. 4
0
    private void ShortestPath()
    {
        Debug.Log("Shortest path between " + start.ToString() + " and " + end.ToString());

        List <PathfinderTile> explored = new List <PathfinderTile> ();
        List <PathfinderPath> frontier = new List <PathfinderPath> ();
        PathfinderPath        path     = new PathfinderPath();

        path.AddTile(start);
        frontier.Add(path);

        while (true)
        {
            if (frontier.Count == 0)
            {
                shortestPath = new PathfinderPath();                 //failed to find a path
                return;
            }
            double lowestCost      = double.MaxValue;
            int    lowestCostIndex = 0;
            for (int i = 0; i < frontier.Count; i++)
            {
                if (frontier[i].Cost < lowestCost)
                {
                    lowestCost      = frontier[i].Cost;
                    lowestCostIndex = i;
                }
            }
            path = frontier[lowestCostIndex];
            if (path.End.Equals(end))
            {
                shortestPath = path;
                return;
            }
            frontier.RemoveAt(lowestCostIndex);
            explored.Add(path.End);
            List <PathfinderTile> neighbors = path.End.GetNeighbors();
            for (int i = 0; i < neighbors.Count; i++)
            {
                if (!explored.Contains(neighbors[i]))
                {
                    int index = -1;
                    for (int j = 0; j < frontier.Count; j++)
                    {
                        if (frontier[j].End.Equals(neighbors[i]))
                        {
                            index = j;
                            break;
                        }
                    }
                    if (index < 0)
                    {
                        PathfinderPath neighborPath = path.Clone();
                        neighborPath.AddTile(neighbors[i]);
                        if (!double.IsNaN(neighborPath.Cost))
                        {
                            frontier.Add(neighborPath);
                        }
                    }
                    else if (frontier[index].Cost < path.Cost)
                    {
                        path = frontier[index];
                    }
                }
            }
        }
    }
Esempio n. 5
0
 public void ClearShortestPath()
 {
     shortestPath = null;
 }