Пример #1
0
        [Test] public static void Index2dTo1d()
        {
            RectInt R = new RectInt {
                width = 6, height = 6
            };

            Assert.AreEqual(13, NativeGrid.Index2dTo1d(1, 2, R.width));
        }
 public static int Index2dTo1d(INT x, INT y, INT width) => NativeGrid.Index2dTo1d(x, y, width);
 public static int Index2dTo1d(INT2 i2, INT width) => NativeGrid.Index2dTo1d(i2, width);
        void SolvePath()
        {
            //prepare data:
            NativeArray <float> moveCost;
            {
                int len = _resolution * _resolution;
                moveCost = new NativeArray <float>(len, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                float[] arr = new float[len];                //NativeArray enumeration is slow outside Burst
                for (int i = len - 1; i != -1; i--)
                {
                    arr[i] = _grid[i].style.backgroundColor.value.r;
                }
                moveCost.CopyFrom(arr);
            }

            //calculate:
            NativeList <int2> path;

            float[] debug_F;
            int2[]  visited;
            {
                path = new NativeList <int2>(_resolution, Allocator.TempJob);

                                #if DEBUG
                var watch = System.Diagnostics.Stopwatch.StartNew();
                                #endif

                //run job:
                var job = new NativeGrid.AStarJob(
                    (int2)(_p1 * _resolution), (int2)(_p2 * _resolution),
                    moveCost, _resolution,
                    heuristic_cost,
                    heuristic_search,
                    path
                    );
                job.Run();

                                #if DEBUG
                watch.Stop();
                Debug.Log($"{nameof(NativeGrid.AStarJob)} took {watch.ElapsedMilliseconds} ms");
                                #endif

                // copy debug data:
                debug_F = job._F_.ToArray();
                using (var arr = job.visited.GetKeyArray(Allocator.Temp)) visited = arr.ToArray();

                //dispose unmanaged arrays:
                job.Dispose();
            }

            //visualize:
            foreach (var i2 in path)
            {
                int i    = NativeGrid.Index2dTo1d(i2, _resolution);
                var CELL = _grid[i];

                var   cellStyle = CELL.style;
                Color col       = cellStyle.backgroundColor.value;
                col.r = 1f;
                cellStyle.backgroundColor = col;
            }
            if (labelsExist)
            {
                for (int i = debug_F.Length - 1; i != -1; i--)
                {
                    var   CELL  = _grid[i];
                    Label LABEL = CELL[0] as Label;

                    var f = debug_F[i];
                    if (f != float.MaxValue)
                    {
                        LABEL.text    = $"f:{f:0.000}";
                        LABEL.visible = true;
                    }
                    else
                    {
                        LABEL.visible = false;
                    }
                }
            }
            foreach (var i2 in visited)
            {
                int i    = NativeGrid.Index2dTo1d(i2, _resolution);
                var CELL = _grid[i];

                var   cellStyle = CELL.style;
                Color col       = cellStyle.backgroundColor.value;
                col.b = 1f;
                cellStyle.backgroundColor = col;
            }

            //dispose data:
            moveCost.Dispose();
            path.Dispose();
        }