Beispiel #1
0
        //获取某个点周围可以到达的点
        public List <Point> SurroundPoints(Point point, bool IsIgnoreCorner)
        {
            List <Point> surroundPoints = new List <Point>(9);

            //允许斜着走
            //for (int x = point.X - 1; x <= point.X + 1; x++)
            //    for (int y = point.Y - 1; y <= point.Y + 1; y++)
            //    {
            //        if (CanReach(point, x, y, IsIgnoreCorner))
            //            ListHelp.Add(surroundPoints, x, y);
            //    }

            //不允许斜着走
            {
                for (int y = point.Y - 1; y <= point.Y + 1; y++)
                {
                    if (CanReach(point, point.X, y, IsIgnoreCorner))
                    {
                        ListHelp.Add(surroundPoints, point.X, y);
                    }
                }
                for (int x = point.X - 1; x <= point.X + 1; x += 2)
                {
                    if (CanReach(point, x, point.Y, IsIgnoreCorner))
                    {
                        ListHelp.Add(surroundPoints, x, point.Y);
                    }
                }
            }

            return(surroundPoints);
        }
Beispiel #2
0
 private bool CanReach(Point start, int x, int y, bool IsIgnoreCorner)
 {
     if (!CanReach(x, y) || ListHelp.Exists(CloseList, x, y))
     {
         return(false);
     }
     else
     {
         //判断是否是上下左右走
         if (System.Math.Abs(x - start.X) + System.Math.Abs(y - start.Y) == 1)
         {
             return(true);
         }
         //如果是斜方向移动, 判断是否 "拌脚"
         else
         {
             if (CanReach(start.X, y) || CanReach(x, start.Y))
             {
                 return(true);
             }
             else
             {
                 return(IsIgnoreCorner);
             }
         }
     }
 }
Beispiel #3
0
 //一次寻路的函数,因为路径找的时候是从end到start,所以已经把函数中所有的end的地方和start的地方互换了
 public void FindOncePath(Point start, Point end, bool IsIgnoreCorner)
 {
     OpenList.Add(end);
     while (OpenList.Count != 0)
     {
         //找出F值最小的点
         Point tempStart = ListHelp.MinPoint(ref OpenList);//ref为引用调用
         OpenList.RemoveAt(0);
         CloseList.Add(tempStart);
         //找出它相邻的点
         List <Point> surroundPoints = SurroundPoints(tempStart, IsIgnoreCorner);
         foreach (Point point in surroundPoints)
         {
             if (ListHelp.Exists(OpenList, point))
             {
                 //如果已经在开始列表里了,计算G值, 如果比原来的大, 就什么都不做, 否则设置它的父节点为当前点,并更新G和F
                 FoundPoint(tempStart, point);
             }
             else
             {
                 //如果它们不在开始列表里, 就加入, 并设置父节点,并计算GHF
                 NotFoundPoint(tempStart, start, point);
             }
         }
         if (ListHelp.Get(OpenList, start) != null)
         {
             parent = ListHelp.Get(OpenList, start);
             return;
         }
     }
     parent = ListHelp.Get(OpenList, start);
 }