示例#1
0
    private IntVector2 dijkstra(IntVector2 start, IntVector2 end)
    {
        PriorityQueue <PqItem> pq = new PriorityQueue <PqItem>();
        Dictionary <IntVector2, IntVector2> prev    = new Dictionary <IntVector2, IntVector2>();
        Dictionary <IntVector2, IntVector2> visited = new Dictionary <IntVector2, IntVector2>();

        pq.Enqueue(new PqItem(start, 0, null));

        while (pq.Count > 0)
        {
            PqItem v = pq.Dequeue();
            if (visited.ContainsKey(v.val))
            {
                continue;
            }
            visited.Add(v.val, v.prev);
            Debug.Log("Visited " + v.val.x + " " + v.val.y);
            List <IntVector2> moves = manager.possibleMoves(v.val.x, v.val.y, true);
            foreach (IntVector2 move in moves)
            {
                HexagonTile tile = manager.getHexGrid()[move.y][move.x];
                float       dist = v.priority + tileToCost[tile.terrainType];
                pq.Enqueue(new PqItem(move, dist, v.val));
            }
        }

        if (!visited.ContainsKey(end))
        {
            Debug.Log("no end :(");
            return(null);
        }

        IntVector2 prevT = end;

        Debug.Log("prev x " + prevT.x + " y " + prevT.y);
        while (visited[prevT] != start)
        {
            prevT = visited[prevT];
            Debug.Log("prev x " + prevT.x + " y " + prevT.y);
        }

        if (prevT == end)
        {
            return(null);
        }

        return(prevT);
    }
示例#2
0
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                return(1);
            }

            PqItem otherItem = obj as PqItem;

            if (otherItem != null)
            {
                return(this.priority.CompareTo(otherItem.priority));
            }
            else
            {
                throw new ArgumentException("Object is not a PqItem");
            }
        }