public void GraphGridNeighborService_ShouldFindAllValidInCorner(int x, int y)
        {
            // Arrange
            var node      = new Vector2Int(x, y);
            var graphMock = new Mock <IGraph>();

            graphMock.Setup(g => g.Width).Returns(3);
            graphMock.Setup(g => g.Height).Returns(3);
            graphMock.Setup(g => g.GetTransition(node, It.IsAny <Vector2Int>())).Returns(0);

            var neighborService = new GraphGridNeighborsService(1);

            // Act
            var result = new List <Vector2Int>();

            neighborService.GetNeighbors(graphMock.Object, node, result);

            // Assert
            Assert.True(result.Distinct().Count(n => n != node) == 3);
            var distances = result.Select(n => Vector2Int.Distance(n, node)).Distinct().ToArray();

            Assert.True(distances.Count() == 2);
            Assert.True(Math.Abs(distances.Min() - 1) < 0.00001);
            Assert.True(Math.Abs(distances.Max() - Math.Sqrt(2)) < 0.00001);
        }
        public void GraphGridNeighborService_ShouldFindWithDropUnreachable()
        {
            // Arrange
            var node      = new Vector2Int(1, 1);
            var graphMock = new Mock <IGraph>();

            graphMock.Setup(g => g.Width).Returns(3);
            graphMock.Setup(g => g.Height).Returns(3);
            graphMock.SetupSequence(g => g.GetTransition(node, It.IsAny <Vector2Int>()))
            .Returns(0)
            .Returns(2)
            .Returns(0)
            .Returns(2)
            .Returns(0)
            .Returns(2)
            .Returns(0)
            .Returns(2);

            var neighborService = new GraphGridNeighborsService(1);

            // Act
            var result = new List <Vector2Int>();

            neighborService.GetNeighbors(graphMock.Object, node, result);

            // Assert
            Assert.True(result.Distinct().Count(n => n != node) == 4);
            var distances = result.Select(n => Vector2Int.Distance(n, node)).Distinct().ToArray();

            Assert.True(distances.Count() == 1);
            Assert.True(Math.Abs(distances.First() - 1) < 0.00001);
        }
        public void AStarPathFindServiceUnitTests_ShouldFindOptimalPath_From00To04()
        {
            // Arrange
            var start   = new Vector2Int(0, 0);
            var end     = new Vector2Int(0, 4);
            var options = new PathFindingOptions()
            {
                Start = start,
                End   = end
            };

            var graph           = new MatrixFullGraph(5, 5, 0);
            var neighborService = new GraphGridNeighborsService(1);
            var astar_finder    = new AStarPathFindService(neighborService);

            // Act
            var result = astar_finder.Find(graph, options);

            // Assert
            var expected = new GraphPath()
            {
                Path = new List <Vector2Int>()
                {
                    start,
                    new Vector2Int(0, 1),
                    new Vector2Int(0, 2),
                    new Vector2Int(0, 3),
                    end
                }
            };

            Assert.True(Enumerable.SequenceEqual(expected.Path, result.Path));
        }
        public void GraphGridNeighborService_ShouldThrowIfGraphIsNull()
        {
            // Arrange
            var neighborService = new GraphGridNeighborsService(1);

            // Act/Assert
            Assert.Throws <ArgumentNullException>(() =>
            {
                neighborService.GetNeighbors(null, new Vector2Int(), new List <Vector2Int>());
            });
        }
        public void GraphGridNeighborService_ShouldThrowIfCurrentNotInBounds(int x, int y)
        {
            // Arrange
            var node      = new Vector2Int(x, y);
            var graphMock = new Mock <IGraph>();

            graphMock.Setup(g => g.Width).Returns(1);
            graphMock.Setup(g => g.Height).Returns(1);

            var neighborService = new GraphGridNeighborsService(1);

            // Act/Assert
            Assert.Throws <ArgumentException>(() =>
            {
                neighborService.GetNeighbors(graphMock.Object, node, new List <Vector2Int>());
            });
        }
Exemple #6
0
        void Update()
        {
            if (!(Application.isEditor && !Application.isPlaying))
            {
                return;
            }

            if (isInit)
            {
                return;
            }

            var graph    = new MatrixFullGraph(options.GraphWidth, options.GraphHeight);
            var neighbor = new GraphGridNeighborsService(options.MaxNotWalkableCost);

            var navGrid = GetComponent <NavigationActionGrid>();

            navGrid.Graph            = graph;
            navGrid.NeighborsService = neighbor;

            isInit = true;
        }