Beispiel #1
0
        public void AddTest()
        {
            var position = GetRandomVector3();
            var entity   = new Entity(1, position, 0, 1);

            grid2.Add(entity);
            using (var enumerator = grid2.Find(position, 0).GetEnumerator())
            {
                Assert.True(enumerator.MoveNext());
                Assert.AreEqual(entity, enumerator.Current);
            }
        }
    public void CreatePoints()
    {
        Debug.Log("Create Points");
        Random.InitState((int)DateTime.Now.Ticks);
        processList  = new List <Vector2>();
        samplePoints = new List <Vector2>();

        radius  = minRadius;
        radius2 = radius * radius;

        // build a grid to work with
        if (image != null)
        {
            size.x = image.width;
            size.y = image.height;
            grid   = new Grid2(size.x, size.y, maxRadius / Mathf.Sqrt(2));
        }
        else
        {
            grid = new Grid2(size.x, size.y, radius / Mathf.Sqrt(2));
        }

        // Start with an initial random point
        var initialPoint = new Vector2(Random.Range(0, size.x), Random.Range(0, size.y));

        Debug.Log($"Initial Point {initialPoint.x},{initialPoint.y}");
        processList.Add(initialPoint);
        samplePoints.Add(initialPoint);
        grid.Add(initialPoint);

        // Generate more points
        while (processList.Count > 0 && processList.Count < maxPoints)
        {
            // pick a random point to start from
            Vector2 point = RandomPointFromProcessList();

            // generate new points around that one, based on density
            for (int i = 0; i < density; i++)
            {
                Vector2 newPoint = GenerateRandomPointAroundPoint(point);
                // Check if that point is not too close to another (and is inside out bounds)
                if (grid.InsideBounds(newPoint) && IsFarEnoughFromAllPoints(newPoint))
                {
                    // This is a good point, so add it to our grid and lists
                    grid.Add(newPoint);
                    processList.Add(newPoint);
                    samplePoints.Add(newPoint);
                }
            }

            processList.Remove(point);
        }
        Debug.Log($"count {processList.Count}");
        // samplePoints now contains our final points
        if (prefab != null)
        {
            foreach (Vector2 samplePoint in samplePoints)
            {
                GameObject go = Instantiate(prefab, samplePoint, Quaternion.identity, gameObject.transform);
                go.name = $"Point {(int)samplePoint.x},{(int)samplePoint.y}";
            }
        }
    }