コード例 #1
0
 public void softenPath(ref List <Vector3> vectorList)
 {
     if (isFilterPathByRay)
     {
         filterPath(ref vectorList);
     }
     if (isSoftenPath)
     {
         CLAIPathUtl.softenPath(ref vectorList, softenPathType, softenFactor, cellSize);
     }
 }
コード例 #2
0
        public override List <Vector3> seek(Vector3 toPos)
        {
            targetPos = toPos;
            canMove   = false;
            resetCache();
            begainTime = Time.time;
            bool           isCanReach = false;
            List <Vector3> leftPath   = trySearchPathLeft(toPos);
            List <Vector3> rightPath  = trySearchPathRight(toPos);

            if (leftPath != null && leftPath.Count > 0 && (rightPath == null || rightPath.Count == 0))
            {
                isCanReach = true;
                pathList   = leftPath;
            }
            else if ((leftPath == null || leftPath.Count == 0) && rightPath != null && rightPath.Count > 0)
            {
                isCanReach = true;
                pathList   = rightPath;
            }
            else if (leftPath != null && leftPath.Count > 0 && rightPath != null && rightPath.Count > 0)
            {
                isCanReach = true;
                pathList   = getShortList(leftPath, rightPath);
            }
            else
            {
                filterPath(ref tmpVectorList1);
                leftPath = tmpVectorList1;
                leftPath.Insert(0, mTransform.position);
                filterPath(ref tmpVectorList2);
                rightPath = tmpVectorList2;
                rightPath.Insert(0, mTransform.position);
                float dis1 = Vector3.Distance(leftPath[leftPath.Count - 1], toPos);
                float dis2 = Vector3.Distance(rightPath[rightPath.Count - 1], toPos);
                if (dis1 < dis2)
                {
                    pathList = leftPath;
                }
                else
                {
                    pathList = rightPath;
                }

                //计算离目标点最近的点
                float minDis    = -1;
                float tmpMinDis = 0;
                int   index     = -1;
                for (int i = 0; i < pathList.Count; i++)
                {
                    tmpMinDis = Vector3.Distance(pathList[i], toPos);
                    if (minDis < 0 || tmpMinDis < minDis)
                    {
                        minDis = tmpMinDis;
                        index  = i;
                    }
                }
                for (int i = index + 1; i < pathList.Count; i++)
                {
                    pathList.RemoveAt(i);
                }

                isCanReach = false;
#if UNITY_EDITOR
                Debug.LogWarning("Cannot search path");
#endif
            }

            if (isSoftenPath)
            {
                CLAIPathUtl.softenPath(ref pathList, softenPathType, softenFactor, cellSize);
            }

            //回调的第一个参数是路径,第二个参数是能否到达目标点
            Utl.doCallback(onFinishSeekCallback, pathList, isCanReach);

            if (autoMoveOnFinishSeek)
            {
                //开始移动
                startMove();
            }
            return(pathList);
        }
コード例 #3
0
ファイル: CLAStarPathSearch.cs プロジェクト: coolape/mibao
        /// <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);
        }