Beispiel #1
0
    private void MitchellBestCandidateSpawner()
    {
        Vector2[] samples = currentChunk.SamplePointsInChunk(currentChunk.Holes.Count * m + 1); //we sample more as there's numerous holes

        Vector2 bestCandidate    = samples[0];
        float   furthestDistance = 0f;

        //Get a list of all trackers holes (spawnarea and extra area)
        IEnumerable <Hole> allHoles;

        if (lastChunk != null)
        {
            allHoles = currentChunk.Holes.Union(lastChunk.Holes);
        }
        else
        {
            allHoles = currentChunk.Holes;
        }
        //List<Hole> allHoles = sensorSpawnArea.SensedHoles.Union(sensorExtraArea.SensedHoles).ToList();

        //Find the best candidate, the furthest away from each point
        foreach (Vector2 point in samples)
        {
            float minDistance = Mathf.Infinity;

            //We get the minimum distance between that point and all the holes.
            foreach (Hole hole in allHoles)
            {
                float dist = Vector2.Distance(point, hole.transform.position) - hole.Radius;

                if (dist <= 0f)
                {
                    dist        = 0f;
                    minDistance = dist;
                    //We got the worst dist possible, no need to compute further
                    break;
                }

                //Min dist for that point
                if (dist < minDistance)
                {
                    minDistance = dist;
                }
            }

            //We keep the maximum minimum distance between all holes
            if (minDistance >= furthestDistance)
            {
                furthestDistance = minDistance;
                bestCandidate    = point;
            }
        }

        //We got our point, time to get the radius
        float maxRadius        = Hole.MAX_RADIUS;
        float maxRadiusForArea = currentChunk.GetMaxRadiusToFillRemainingArea(targetDensity);


        //We calculate the max radius depending on few things
        if (furthestDistance <= Hole.MIN_RADIUS)
        {
            maxRadius = Hole.MIN_RADIUS;
        }
        else
        {
            maxRadius = furthestDistance;
        }

        maxRadius = Mathf.Min(maxRadius, maxRadiusForArea);


        currentChunk.SpawnHole(holePrefab, bestCandidate, Hole.GetRandomRadius(maxRadius));
    }