Esempio n. 1
0
        public static Action Test_GetLinePointList()
        {
            Vector2Int        a_point    = new Vector2Int(-4, -4);
            Vector2Int        b_point    = new Vector2Int(3, 2);
            List <Vector2Int> point_list = AStarUtil.GetLinePointList(a_point, b_point);

            return(() => { AStarUtil.GUIShowPointList(-5, -5, 5, 5, point_list); });
        }
Esempio n. 2
0
        //对角线寻路
        public static List <Vector2Int> DiagonallyFindPath(AStarMapPath astarMapPath, Vector2Int pointA,
                                                           Vector2Int pointB,
                                                           int[] canPassObstacleTypes,
                                                           int[] canPassTerrainTypes)
        {
            if (!AStarUtil.IsInRange(astarMapPath.GetFinalGrids(), pointA) ||
                !AStarUtil.IsInRange(astarMapPath.GetFinalGrids(), pointB))
            {
                return(null);
            }
            List <Vector2Int> list = new List <Vector2Int>();

            int dx = pointB.x - pointA.x;
            int dy = pointB.y - pointA.y;

            if (Math.Abs(dx) > Math.Abs(dy))
            {
                int x1;
                if (dx > 0)
                {
                    x1 = pointA.x + Math.Abs(dy);
                }
                else
                {
                    x1 = pointA.x - Math.Abs(dy);
                }
                Vector2Int p = new Vector2Int(x1, pointB.y);
                if (!AStarUtil.CanPass(astarMapPath, p.x, p.y, canPassObstacleTypes, canPassTerrainTypes))
                {
                    return(null);
                }
                List <Vector2Int> list1 = AStarUtil.GetLinePointList(pointA, p);
                if (!AStarUtil.CanPass(astarMapPath, list1, canPassObstacleTypes, canPassTerrainTypes))
                {
                    return(null);
                }
                List <Vector2Int> list2 = AStarUtil.GetLinePointList(p, pointB);
                if (!AStarUtil.CanPass(astarMapPath, list2, canPassObstacleTypes, canPassTerrainTypes))
                {
                    return(null);
                }
                list.AddRange(list1);
                list.RemoveLast();                 //删掉p
                list.AddRange(list2);
            }
            else
            {
                int y1;
                if (dy > 0)
                {
                    y1 = pointA.y + Math.Abs(dx);
                }
                else
                {
                    y1 = pointA.y - Math.Abs(dx);
                }
                Vector2Int p = new Vector2Int(pointB.x, y1);
                if (!AStarUtil.CanPass(astarMapPath, p.x, p.y, canPassObstacleTypes, canPassTerrainTypes))
                {
                    return(null);
                }
                List <Vector2Int> list1 = AStarUtil.GetLinePointList(pointA, p);
                if (!AStarUtil.CanPass(astarMapPath, list1, canPassObstacleTypes, canPassTerrainTypes))
                {
                    return(null);
                }
                List <Vector2Int> list2 = AStarUtil.GetLinePointList(p, pointB);
                if (!AStarUtil.CanPass(astarMapPath, list2, canPassObstacleTypes, canPassTerrainTypes))
                {
                    return(null);
                }
                list.AddRange(list1);
                list.RemoveLast();                 //删掉p
                list.AddRange(list2);
            }

            return(list);
        }