Example #1
0
    public void RemoveObstacle_OutOfMaterix()
    {
        Vector2 obstaclePosition = new Vector2(5, 3);

        AStarBase aStar = new AStarBase(10, 10);

        aStar.SetObstacle(obstaclePosition);

        aStar.RemoveObstacle(new Vector2(100, 5));
        aStar.RemoveObstacle(new Vector2(5, 100));
        aStar.RemoveObstacle(new Vector2(-10, 5));
        aStar.RemoveObstacle(new Vector2(5, -10));

        for (int x = 0; x < aStar.matrix.GetLength(0); x++)
        {
            for (int y = 0; y < aStar.matrix.GetLength(1); y++)
            {
                if (x != (int)obstaclePosition.x || y != (int)obstaclePosition.y)
                {
                    Assert.IsTrue(aStar.matrix[x, y].canThrough);
                }
                else
                {
                    Assert.IsFalse(aStar.matrix[x, y].canThrough);
                }
            }
        }
    }
Example #2
0
    float GetAStarBaseFindpathTime()
    {
        AStarBase aStar = new AStarBase(_map.width, _map.height);

        float averageTime = GetFunctionMillisecondTime(aStar.FindPath, _map.startPosition, _map.endPosition);

        return(averageTime);
    }
Example #3
0
    public void Pathfind_CantFind()
    {
        AStarBase aStar = new AStarBase(7, 7);

        Vector2 startPosition = new Vector2(0, 0);
        Vector2 endPosition   = new Vector2(6, 6);

        aStar.SetObstacle(new Vector2(0, 1));
        aStar.SetObstacle(new Vector2(1, 1));
        aStar.SetObstacle(new Vector2(2, 1));
        aStar.SetObstacle(new Vector2(4, 1));
        aStar.SetObstacle(new Vector2(4, 2));
        aStar.SetObstacle(new Vector2(4, 3));
        aStar.SetObstacle(new Vector2(5, 3));
        aStar.SetObstacle(new Vector2(6, 3));
        aStar.SetObstacle(new Vector2(1, 4));
        aStar.SetObstacle(new Vector2(2, 4));
        aStar.SetObstacle(new Vector2(3, 4));
        aStar.SetObstacle(new Vector2(2, 5));
        aStar.SetObstacle(new Vector2(0, 6));
        aStar.SetObstacle(new Vector2(1, 6));

        aStar.FindPath(startPosition, endPosition);

        /*
         *  N   N   0   0   0   0   E
         *
         *  ↓   ←   N   0   0   0   0
         *
         *  ↓   N   N   N   0   0   0
         *
         *  →   →   →   ↓   N   N   N
         *
         *  →   →   →   ↓   N   ↓   ←
         *
         *  N   N   N   ↓   N   ↓   ←
         *
         *  S   ←   ←   ←   ←   ←   ←
         */
        AstarBaseNode[,] matrix    = aStar.matrix;
        AstarBaseNode[,] nextNodes = new AstarBaseNode[7, 7]
        {
            { null, matrix[0, 0], matrix[1, 0], matrix[2, 0], matrix[3, 0], matrix[4, 0], matrix[5, 0] },
            { null, null, null, matrix[3, 0], null, matrix[5, 0], matrix[5, 1] },
            { matrix[1, 2], matrix[2, 2], matrix[3, 2], matrix[3, 1], null, matrix[5, 1], matrix[5, 2] },
            { matrix[1, 3], matrix[2, 3], matrix[3, 3], matrix[3, 2], null, null, null },
            { matrix[0, 3], null, null, null, null, null, null },
            { matrix[0, 4], matrix[0, 5], null, null, null, null, null },
            { null, null, null, null, null, null, null }
        };
        for (int x = 0; x < 7; x++)
        {
            for (int y = 0; y < 7; y++)
            {
                Assert.AreEqual(nextNodes[y, x], aStar.matrix[x, y].nextNode);      //横y竖x,何等反人类,而且竟然严格符合了数组下标顺序:前面是第一层,后面是第二层
            }
        }
    }
Example #4
0
    public void Creat_Normal()
    {
        int width  = 5;
        int height = 10;

        AStarBase aStar = new AStarBase(width, height);

        Assert.AreEqual(aStar.matrix.GetLength(0), width);
        Assert.AreEqual(aStar.matrix.GetLength(1), height);
    }
Example #5
0
    public void Creat_Negative()
    {
        int width  = -10;
        int height = -5;

        AStarBase aStar = new AStarBase(width, height);

        Assert.AreEqual(aStar.matrix.GetLength(0), 0);
        Assert.AreEqual(aStar.matrix.GetLength(1), 0);
    }
Example #6
0
    public void RemoveObstacle_Normal()
    {
        Vector2 obstaclePosition = new Vector2(5, 3);

        AStarBase aStar = new AStarBase(10, 10);

        aStar.SetObstacle(obstaclePosition);
        aStar.RemoveObstacle(obstaclePosition);

        foreach (AstarBaseNode node in aStar.matrix)
        {
            Assert.IsTrue(node.canThrough);
        }
    }
Example #7
0
    public void SetObstacle_OutOfMaterix()
    {
        AStarBase aStar = new AStarBase(10, 10);

        aStar.SetObstacle(new Vector2(-1, 3));
        aStar.SetObstacle(new Vector2(1, -3));
        aStar.SetObstacle(new Vector2(5, 10));
        aStar.SetObstacle(new Vector2(25, 3));

        foreach (AstarBaseNode node in aStar.matrix)
        {
            Assert.IsTrue(node.canThrough);
        }
    }
Example #8
0
    public void Pathfind_StartAndEndOutOfMatrix()
    {
        AStarBase aStar = new AStarBase(7, 7);

        Vector2 startPosition = new Vector2(0, 7);
        Vector2 endPosition   = new Vector2(-1, 6);

        aStar.FindPath(startPosition, endPosition);

        foreach (AstarBaseNode node in aStar.matrix)
        {
            Assert.AreEqual(null, node.nextNode);
        }
    }
Example #9
0
    public void Pathfind_EndIsObstacle()
    {
        AStarBase aStar = new AStarBase(7, 7);

        Vector2 startPosition = new Vector2(0, 0);
        Vector2 endPosition   = new Vector2(6, 6);

        aStar.SetObstacle(new Vector2(6, 6));

        aStar.FindPath(startPosition, endPosition);

        foreach (AstarBaseNode node in aStar.matrix)
        {
            Assert.AreEqual(null, node.nextNode);
        }
    }
Example #10
0
    public void Pathfind_CanFind_NoObstacle()
    {
        AStarBase aStar = new AStarBase(7, 7);

        Vector2 startPosition = new Vector2(0, 0);
        Vector2 endPosition   = new Vector2(6, 6);

        aStar.FindPath(startPosition, endPosition);

        /*
         *  ↓   ←   ←   ←   ←   ←   E
         *
         *  ↓   ←   ↑   ↑   ↑   ↑   0
         *
         *  ↓   ←   0   0   0   0   0
         *
         *  ↓   ←   0   0   0   0   0
         *
         *  ↓   ←   0   0   0   0   0
         *
         *  ↓   ←   0   0   0   0   0
         *
         *  S   ←   0   0   0   0   0
         */
        AstarBaseNode[,] matrix    = aStar.matrix;
        AstarBaseNode[,] nextNodes = new AstarBaseNode[7, 7]
        {
            { null, matrix[0, 0], null, null, null, null, null },
            { matrix[0, 0], matrix[0, 1], null, null, null, null, null },
            { matrix[0, 1], matrix[0, 2], null, null, null, null, null },
            { matrix[0, 2], matrix[0, 3], null, null, null, null, null },
            { matrix[0, 3], matrix[0, 4], null, null, null, null, null },
            { matrix[0, 4], matrix[0, 5], matrix[2, 6], matrix[3, 6], matrix[4, 6], matrix[5, 6], null },
            { matrix[0, 5], matrix[0, 6], matrix[1, 6], matrix[2, 6], matrix[3, 6], matrix[4, 6], matrix[5, 6] }
        };
        for (int x = 0; x < 7; x++)
        {
            for (int y = 0; y < 7; y++)
            {
                Assert.AreEqual(nextNodes[y, x], aStar.matrix[x, y].nextNode);      //二维数组赋值是按照横竖x的方式进行的,也就是说上面的矩阵写错了,要转置,这就是一个用 [y, x] 一个用 [x, y] 的原因
            }
        }
    }
Example #11
0
 void CreatAStar()
 {
     _aStar = new AStarBase(_width, _height);
 }