Ejemplo n.º 1
0
        private void RunSearch()
        {
            var grid = new PathfindingGrid(GridWidth, GridHeight, Allocator.Persistent);

            for (var i = 0; i < Cells.Length; ++i)
            {
                var cell = Cells[i];
                grid.SetClearance(cell.GridX, cell.GridY, cell.Clearance);
                grid.SetFlag(cell.GridX, cell.GridY, cell.Capabilities);
            }

            NativeList <int> path = new NativeList <int>(Allocator.Persistent);
            var pathFound         = AStarSearch.TryGetPath(grid,
                                                           SelectedCell1.GridX, SelectedCell1.GridY,
                                                           SelectedCell2.GridX, SelectedCell2.GridY,
                                                           UnitSize, UnitCapabilities,
                                                           Allocator.Persistent, path);

            foreach (var index in path)
            {
                Cells[index].SetCellColor(SelectedColor);
            }

            if (path.IsCreated)
            {
                path.Dispose();
            }

            grid.Dispose();
        }
Ejemplo n.º 2
0
        [Test] //Partial vertical block
        public void PathFoundIsTheShortest_WhenPathIsPartiallyBlocked()
        {
            var grid = new PathfindingGrid(6, 5, Allocator.Persistent);

            SetArea(grid, 6, 5, 1, 1);
            grid.SetClearance(3, 0, 0);
            grid.SetClearance(3, 1, 0);
            grid.SetClearance(3, 2, 0);
            grid.SetClearance(3, 3, 0);

            NativeList <int> resultPath = new NativeList <int>(Allocator.Persistent);
            var pathFound = AStarSearch.TryGetPath(grid,
                                                   2, 0,
                                                   5, 4,
                                                   1, 1, Allocator.Persistent, resultPath);

            Assert.AreEqual(true, pathFound);
            Assert.AreEqual(2, resultPath[0]);
            Assert.AreEqual(8, resultPath[1]);
            Assert.AreEqual(14, resultPath[2]);
            Assert.AreEqual(20, resultPath[3]);
            Assert.AreEqual(27, resultPath[4]);
            Assert.AreEqual(28, resultPath[5]);

            grid.Dispose();
            resultPath.Dispose();
        }