Beispiel #1
0
    public void GetClosest(float2 pos, float range, NativeList <Result> items, int maxResults)
    {
        var min     = math.max(0, (int2)((pos - range) / _cellSize));
        var max     = math.min(_gridSize - 1, (int2)((pos + range) / _cellSize));
        var rangeSq = Math.Square(range);

        for (int y = min.y; y <= max.y; y++)
        {
            for (int x = min.x; x <= max.x; x++)
            {
                if (Get(new int2(x, y), out var l))
                {
                    for (int i = 0; i < l.Length; i++)
                    {
                        var item = new Result {
                            Item = l[i], Dist = math.lengthsq(l[i].Position - pos)
                        };

                        if (item.Dist < rangeSq)
                        {
                            if (items.Length < maxResults)
                            {
                                items.Add(item);
                            }

                            var i1 = items.Length - 1;

                            while (i1 != 0 && item.Dist < items[i1 - 1].Dist)
                            {
                                items[i1] = items[i1 - 1];
                                --i1;
                            }

                            items[i1] = item;

                            if (items.Length == maxResults)
                            {
                                rangeSq = items[items.Length - 1].Dist;
                            }
                        }
                    }
                }
            }
        }
    }