Ejemplo n.º 1
0
        /// <summary>
        /// Searchs the path.寻路
        /// </summary>
        /// <returns><c>true</c>, if path was searched,可以到达 <c>false</c> otherwise.不可到过</returns>
        /// <param name="from">From.出发点坐标</param>
        /// <param name="to">To.目标点坐标</param>
        /// <param name="vectorList">Vector list.路径点坐标列表</param>
        public bool searchPath(Vector3 from, Vector3 to, ref List <Vector3> vectorList)
        {
            if (!isIninted)
            {
                init();
            }

            if (vectorList == null)
            {
                vectorList = new List <Vector3>();
            }
            else
            {
                vectorList.Clear();
            }
            int fromIndex = grid.GetCellIndex(from);
            int toIndex   = grid.GetCellIndex(to);

            if (fromIndex < 0 || toIndex < 0)
            {
                Debug.LogWarning("Can not reach");
                return(false);
            }
            if (fromIndex == toIndex)
            {
                //就在目标点,直接判断为到达
                vectorList.Add(from);
                vectorList.Add(to);
                return(true);
            }

            CLAStarNode fromNode = nodesMap[fromIndex];

            if (fromNode.isObstruct)
            {
                fromNode = reviseFromNode(from, fromNode);
                if (fromNode == null)
                {
                    Debug.LogWarning("无法到达");
                    //无法到达
                    return(false);
                }
            }
            CLAStarNode toNode = nodesMap[toIndex];

            // 本次寻路的唯一key,(并发同时处理多个寻路时会用到)
            string key = fromNode.index + "_" + toNode.index;

            List <CLAStarNode>     openList   = new List <CLAStarNode>();
            Dictionary <int, bool> closedList = new Dictionary <int, bool>();
            // F值缓存
            Dictionary <int, float> fValueMap = new Dictionary <int, float>();

            //先把开始点加入到closedList
            closedList[fromIndex] = true;
            //计算一次open点列表
            calculationOpenList(key, fromNode, toNode, ref fValueMap, ref openList, closedList);

            //离目标点最近的节点
            CLAStarNode nodeNearest   = fromNode;
            CLAStarNode node          = null;
            float       dis4Target    = -1;
            float       tmpdis4Target = 0;
            int         count         = openList.Count;
            bool        canReach      = false;

            while (count > 0)
            {
                node = openList[count - 1];
                openList.RemoveAt(count - 1);  //从openlist中移除
                closedList[node.index] = true; //设为closed

                if (node.index == toNode.index)
                {
                    //reached
                    nodeNearest = node;
                    canReach    = true;
                    break;
                }
                // 设置离目标点最新的点
                tmpdis4Target = distance(node, toNode);
                if (dis4Target < 0 || tmpdis4Target < dis4Target)
                {
                    dis4Target  = tmpdis4Target;
                    nodeNearest = node;
                }
                //重新处理新的open点
                calculationOpenList(key, node, toNode, ref fValueMap, ref openList, closedList);
                count = openList.Count;
            }
            //回溯路径======================
            CLAStarNode parentNode = null;

            if (canReach)
            {
                vectorList.Insert(0, to);
                parentNode = nodeNearest.getParentNode(key);
            }
            else
            {
                parentNode = nodeNearest;
            }

            while (parentNode != null && parentNode != fromNode)
            {
                vectorList.Insert(0, parentNode.position);
                parentNode = parentNode.getParentNode(key);
            }
            vectorList.Insert(0, from);
            if (isFilterPathByRay)
            {
                filterPath(ref vectorList);
            }
            if (isSoftenPath)
            {
                CLAIPathUtl.softenPath(ref vectorList, softenPathType, softenFactor, cellSize);
            }

            return(canReach);
        }