Пример #1
0
        /// <summary>
        /// 寻找通路
        /// </summary>
        public bool FindPath(AstarNode start, AstarNode end)
        {
            if (start == null || end == null)
            {
                return(false);
            }
            openList.Clear();
            closeList.Clear();
            start.Clear();
            openList.Add(start);
            AstarNode current = null;

            while (openList.Count > 0)
            {
                current = MinCost(openList);
                openList.Remove(current);
                closeList.Add(current);
                if (current == end)
                {
                    break;
                }
                else
                {
                    foreach (var node in CanArriveNode(current))
                    {
                        if (closeList.Contains(node))
                        {
                            continue;
                        }
                        else if (openList.Contains(node))
                        {
                            if (current.g + 1 < node.g)
                            {
                                node.g        = current.g + 1;
                                node.lastNode = current;
                            }
                        }
                        else
                        {
                            openList.Add(node);
                            node.h        = node.Distance(end);
                            node.g        = current.g + 1;
                            node.lastNode = current;
                        }
                    }
                }
            }
            if (current == end)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #2
0
        /// <summary>
        /// 获取所有可走节点
        /// </summary>
        public List <AstarNode> CanArriveNode(AstarNode center)
        {
            var arrive = new List <AstarNode>();

            arrive.Add(this[center.x + 1, center.y]);
            arrive.Add(this[center.x - 1, center.y]);
            arrive.Add(this[center.x, center.y + 1]);
            arrive.Add(this[center.x, center.y - 1]);
            return(ClearNull(arrive));
        }
Пример #3
0
 /// <summary>
 /// 初始化寻路信息
 /// </summary>
 /// <param name="newMap">0代表可通过1代表障碍物</param>
 public void Init(int[,] newMap)
 {
     map = new AstarNode[newMap.GetLength(0), newMap.GetLength(1)];
     for (int i = 0; i < map.GetLength(0); i++)
     {
         for (int j = 0; j < map.GetLength(1); j++)
         {
             map[i, j]        = new AstarNode(i, j);
             map[i, j].status = newMap[i, j];
         }
     }
 }
Пример #4
0
        /// <summary>
        /// 获取最小距离花费节点
        /// </summary>
        /// <param name="list">可走节点列表</param>
        public AstarNode MinCost(List <AstarNode> list)
        {
            AstarNode min = null;

            foreach (var node in list)
            {
                if (min == null)
                {
                    min = node; continue;
                }
                if (min > node)
                {
                    min = node;
                }
            }
            return(min);
        }
Пример #5
0
 public void Clear()
 {
     g        = 0;
     h        = 0;
     lastNode = null;
 }
Пример #6
0
 public int Distance(AstarNode other)
 {
     return(Mathf.Abs(other.x - x) + Mathf.Abs(other.y - y));
 }