Exemplo n.º 1
0
        private static CEPathFindResult GetPathFindResult(CEPathFindNode _endNode)
        {
            var result = new CEPathFindResult();
            var maxNum = 1;
            var node   = _endNode;

            while (node.parent != null)
            {
                node = node.parent;
                maxNum++;
            }

            result.isHavePath = true;
            result.paths      = new Vector2Int[maxNum];

            node = _endNode;
            while (node != null)
            {
                result.paths[maxNum - 1] = new Vector2Int(node.x, node.y);
                node = node.parent;
                maxNum--;
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 节点评分,分数越小越优先遍历
        /// </summary>
        /// <param name="_a"></param>
        /// <param name="_b"></param>
        /// <returns></returns>
        private static int SortListByScore(CEPathFindNode _a, CEPathFindNode _b)
        {
            if (_a.score == _b.score)
            {
                return(0);
            }

            return(_a.score > _b.score ? 1 : -1);
        }
Exemplo n.º 3
0
        public void TickSearch(out bool _isFinish, out CEPathFindResult _result)
        {
            //起始节点和结束节点本身就无法走
            if (!mStarNode.isWalkable || !mEndNode.isWalkable)
            {
                _isFinish = true;
                _result   = new CEPathFindResult {
                    isHavePath = false
                };
                RecycleNodes();
                return;
            }


            for (var i = 0; i < EACH_TICK_SEARCH_NODE_NUM; i++)
            {
                if (mCurrentNode.x == mEndNode.x && mCurrentNode.y == mEndNode.y)
                {
                    _isFinish = true;
                    _result   = GetPathFindResult(mCurrentNode);
                    RecycleNodes();
                    return;
                }

                mCloseList.Add(mCurrentNode);

                CheckCurrentSearchAroundTile();

                if (mOpenList.Count == 0)
                {
                    //没有Open节点了,全部搜索过,但未找到路径
                    _isFinish = true;
                    _result   = new CEPathFindResult {
                        isHavePath = false
                    };
                    RecycleNodes();
                    return;
                }

//                mOpenList.Sort(SortListByScore);
//                mCurrentNode = mOpenList[0];
//                mOpenList.RemoveAt(0);
                mCurrentNode = mOpenList.Remove();
            }

            _isFinish = false;
            _result   = null;
            return;
        }
Exemplo n.º 4
0
        private static CEPathFindNode GetNewNode()
        {
            CEPathFindNode node;

            if (mNodePool.Count > 0)
            {
                node = (CEPathFindNode)mNodePool.Pop();
                node.Reset();
            }
            else
            {
                node = new CEPathFindNode();
            }

            return(node);
        }
Exemplo n.º 5
0
        public void Reset(CEPathFindMapAgent _mapAgent, int _startTileX, int _startTileY, int _endTileX, int _endTileY)
        {
            mMapAgent = _mapAgent;

            RecycleNodes();

            mStarNode            = GetNewNode();
            mStarNode.x          = _startTileX;
            mStarNode.y          = _startTileY;
            mStarNode.isWalkable = mMapAgent.IsTileWalkable(mStarNode.x, mStarNode.y);

            mEndNode            = GetNewNode();
            mEndNode.x          = _endTileX;
            mEndNode.y          = _endTileY;
            mEndNode.isWalkable = mMapAgent.IsTileWalkable(mEndNode.x, mEndNode.y);

            mCurrentNode            = mStarNode;
            mCurrentNode.isWalkable = mMapAgent.IsTileWalkable(mCurrentNode.x, mCurrentNode.y);
        }
Exemplo n.º 6
0
 private void SetNodeProperty(CEPathFindNode _node, int _tileX, int _tileY)
 {
     _node.x = _tileX;
     _node.y = _tileY;
     mMapAgent.GetTileProperty(_tileX, _tileY, mStarNode, mEndNode, out _node.isWalkable, out _node.score);
 }