示例#1
0
    public void Search()
    {
        openNodes.Clear();
        closeNodes.Clear();
        closeNodes.Add(startPosition);
        PathSearchNode movePosition = startPosition;

        while (true)
        //for(int i = 0; i < 5; i++)
        {
            CalculateParentNodes(movePosition);
            movePosition = FindBestPath();
            if (movePosition == null || movePosition.Equal(targetPosition))
            {
                break;
            }
        }
        //Debug.Log("openNodes count ****** :" + openNodes.Count);
        //Debug.Log("closeNodes count ****** :" + closeNodes.Count);
        foreach (PathSearchNode n in closeNodes)
        {
            if (n.node.Parent != null)
            {
                Debug.Log(string.Format("close sort list col={0}, row={1}, node={2}, parent:{3}", n.col, n.row, n.node, n.node.Parent.ToString()));
            }
        }
        //foreach (PathSearchNode n in openNodes)
        //{
        //    Debug.Log(string.Format("open sort list col={0}, row={1}, f={2}, parent:{3}", n.col, n.row, n.node.f, n.node.Parent.ToString()));
        //}
    }
示例#2
0
    public List <Vector2> PathFinding()
    {
        pathNodes.Clear();
        if (closeNodes.Count > 0)
        {
            PathSearchNode currentPosition = targetPosition;
            List <int>     index           = new List <int>();
            for (int i = 0; i < closeNodes.Count; i++)
            {
                index.Add(i);
            }

            for (int i = 0; i < closeNodes.Count; i++)
            {
                for (int j = 0; j < index.Count; j++)
                {
                    int idx = index[j];
                    if (closeNodes[idx].node.Equal(currentPosition.node.Parent))
                    {
                        pathNodes.Add(currentPosition.ToVector2());
                        currentPosition = closeNodes[idx];
                        print("current position:" + currentPosition.ToString());
                        index.Remove(idx);
                        break;
                    }
                }
            }
        }
        pathNodes.Add(startPosition.ToVector2());
        Debug.Log("path node count:" + pathNodes.Count);
        return(pathNodes);
    }
示例#3
0
    private void CheckAddOpenNodes(int col, int row, PathSearchNode _sNode)
    {
        // 判断新加入的点是否已经包含在开启或关闭列表中
        if (IsInvolveOpenClose(col, row))
        {
            return;
        }
        // 判断是否是起点
        if (startPosition.Equal(col, row))
        {
            return;
        }
        PathNode node = AutoPathMap.pathMap[col, row];

        // 计算总体消耗
        node.f = CalculateF(node);
        // 设置父元素
        node.Parent = _sNode.node;

        PathSearchNode newNode = new PathSearchNode(col, row, node);

        if (node.type == NODE_TYPE.Normal)
        {
            openNodes.Add(newNode);
        }
    }
示例#4
0
 private void GetTargetNode()
 {
     if (target != null)
     {
         targetPosition = GetNode(target.position);
     }
 }
示例#5
0
 // 判断两对象是否相等
 public bool Equal(PathSearchNode other)
 {
     if (other == null)
     {
         return(false);
     }
     return(col == other.col && row == other.row);
 }
示例#6
0
    // 对开启列表里的对象进行排序并取出消耗值最小的值放入关闭列表中
    private PathSearchNode FindBestPath()
    {
        openNodes.Sort((x, y) => x.node.f.CompareTo(y.node.f));
        PathSearchNode sNode = null;

        if (openNodes.Count > 0)
        {
            sNode = openNodes[0];
            openNodes.Remove(sNode);
            closeNodes.Add(sNode);
        }
        return(sNode);
    }
示例#7
0
    // 根据父节点计算
    private void CalculateParentNodes(PathSearchNode searchNode)
    {
        int col, row;

        // 左侧
        if (searchNode.col > 0)
        {
            col = searchNode.col - 1;
            row = searchNode.row - 1;
            if (row >= 0)
            {
                CheckAddOpenNodes(col, row, searchNode);
            }
            row = searchNode.row + 1;
            if (row < AutoPathMap.Row)
            {
                CheckAddOpenNodes(col, row, searchNode);
            }
            row = searchNode.row;
            CheckAddOpenNodes(col, row, searchNode);
        }
        // 中间部分
        col = searchNode.col;
        row = searchNode.row - 1;
        if (row >= 0)
        {
            CheckAddOpenNodes(col, row, searchNode);
        }
        row = searchNode.row + 1;
        if (row < AutoPathMap.Row)
        {
            CheckAddOpenNodes(col, row, searchNode);
        }
        // 右侧部分
        if (searchNode.col + 1 < AutoPathMap.Col)
        {
            col = searchNode.col + 1;
            row = searchNode.row - 1;
            if (row >= 0)
            {
                CheckAddOpenNodes(col, row, searchNode);
            }
            row = searchNode.row;
            CheckAddOpenNodes(col, row, searchNode);
            row = searchNode.row + 1;
            if (row < AutoPathMap.Row)
            {
                CheckAddOpenNodes(col, row, searchNode);
            }
        }
    }
示例#8
0
    private void GetStartNode()
    {
        Vector3 position = transform.position;

        startPosition = GetNode(position);
    }
示例#9
0
 public void SetTarget(Transform tf)
 {
     targetPosition = null;
     target         = tf;
 }