Ejemplo n.º 1
0
        private List <AStarNode> Pathfinding(AStarNode startNode, AStarNode endNode)
        {
            //重置起点的估价
            startNode.GCost = 0;
            startNode.HCost = 0;

            _openList.Clear();
            _openSet.Clear();
            _closeSet.Clear();

            //将开始节点加入到开启列表中
            _openList.Add(startNode);
            _openSet.Add(startNode);
            while (_openList.Count > 0)
            {
                //从开启列表中找到总估价最低,且距离终点最近的点
                AStarNode currentNode = _openList[0];
                for (int i = 1; i < _openList.Count; i++)
                {
                    if (_openList[i].TotalCost <= currentNode.TotalCost && _openList[i].HCost < currentNode.HCost)
                    {
                        currentNode = _openList[i];
                    }
                }
                _openList.Remove(currentNode);
                _openSet.Remove(currentNode);
                _closeSet.Add(currentNode);

                //如果当前为终点节点,则创建最终路径
                if (currentNode == endNode)
                {
                    return(GeneratePath(startNode, endNode));
                }

                //遍历当前节点的相邻节点,更新估价并加入开启列表
                GetNeighbor(currentNode);
                for (int i = 0; i < _neighbors.Count; i++)
                {
                    if (!_neighbors[i].IsCanWalk || _closeSet.Contains(_neighbors[i]))
                    {
                        continue;
                    }

                    int gCost = currentNode.GCost + _evaluation.Evaluation(currentNode, _neighbors[i]) + _neighbors[i].OCost;
                    if (gCost < _neighbors[i].GCost || !_openSet.Contains(_neighbors[i]))
                    {
                        _neighbors[i].GCost  = gCost;
                        _neighbors[i].HCost  = _evaluation.Evaluation(_neighbors[i], endNode);
                        _neighbors[i].Parent = currentNode;
                        if (!_openSet.Contains(_neighbors[i]))
                        {
                            _openList.Add(_neighbors[i]);
                            _openSet.Add(_neighbors[i]);
                        }
                    }
                }
            }

            GlobalTools.LogWarning("A*:寻路失败,未找到合适的路径!");
            return(null);
        }
Ejemplo n.º 2
0
        private List <AStarNode> WalkableNodefinding(AStarNode startNode, int cost)
        {
            //重置起点的估价
            startNode.GCost = 0;
            startNode.HCost = 0;

            _openList.Clear();
            _openSet.Clear();
            _closeSet.Clear();

            //将开始节点加入到开启列表中
            _openList.Add(startNode);
            _openSet.Add(startNode);
            while (_openList.Count > 0)
            {
                //从开启列表中任意取出一个节点
                AStarNode currentNode = _openList[0];

                _openList.Remove(currentNode);
                _openSet.Remove(currentNode);
                _closeSet.Add(currentNode);

                //遍历当前节点的相邻节点,更新估价,估价低于限制估价的加入开启列表,否则加入关闭列表
                GetNeighbor(currentNode);
                for (int i = 0; i < _neighbors.Count; i++)
                {
                    if (!_neighbors[i].IsCanWalk || _closeSet.Contains(_neighbors[i]))
                    {
                        continue;
                    }

                    int gCost = currentNode.GCost + _evaluation.Evaluation(currentNode, _neighbors[i]) + _neighbors[i].OCost;
                    if (gCost <= cost)
                    {
                        if (gCost < _neighbors[i].GCost || !_openSet.Contains(_neighbors[i]))
                        {
                            _neighbors[i].GCost  = gCost;
                            _neighbors[i].Parent = currentNode;
                            if (!_openSet.Contains(_neighbors[i]))
                            {
                                _openList.Add(_neighbors[i]);
                                _openSet.Add(_neighbors[i]);
                            }
                        }
                    }
                    else
                    {
                        _neighbors[i].GCost  = gCost;
                        _neighbors[i].Parent = currentNode;

                        if (_openSet.Contains(_neighbors[i]))
                        {
                            _openList.Remove(_neighbors[i]);
                            _openSet.Remove(_neighbors[i]);
                        }
                        _closeSet.Add(_neighbors[i]);
                    }
                }
            }

            //找到所有估价合格的节点
            _resultNodes.Clear();
            foreach (AStarNode node in _closeSet)
            {
                if (node.GCost <= cost)
                {
                    _resultNodes.Add(node);
                }
            }
            return(_resultNodes);
        }
Ejemplo n.º 3
0
 public override int Evaluation(AStarNode nodea, AStarNode nodeb)
 {
     return(Mathf.Abs(nodea.XIndex - nodeb.XIndex) + Mathf.Abs(nodea.YIndex - nodeb.YIndex));
 }