예제 #1
0
    public List <GameObject> GetSeenBoids()
    {
        List <GameObject> seenBoids = new List <GameObject>();

        /*
         * Collider[] boids = Physics.OverlapSphere(rb.transform.position, overlapSphereRadius, LAYER_BOID);
         * int n = (maxSeenBoidsToStore <= 0) ? boids.Length : Mathf.Min(boids.Length, maxSeenBoidsToStore);
         * for (int i = 0; i < n; i++)
         * {
         *  if (boids[i].gameObject != this.gameObject) seenBoids.Add(boids[i].gameObject);
         * }
         */

        //System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();

        List <GameObject> boids = new List <GameObject>();

        if (useFastHashCheck)
        {
            boids = hash.Get(transform.position);
        }
        else
        {
            boids = hash.GetByRadius(transform.position, overlapSphereRadius);
        }

        int n = (maxSeenBoidsToStore <= 0) ? boids.Count : Mathf.Min(boids.Count, maxSeenBoidsToStore);

        for (int i = 0; i < n; i++)
        {
            if (boids[i] != this.gameObject)
            {
                seenBoids.Add(boids[i]);
            }
        }

        //watch.Stop();
        //if(Random.Range(0f, 1f) >= 0.9f) Debug.Log("time to for hash.GetByRadius(): " + watch.ElapsedTicks + " ticks");

        //ADAPTIVE OVERLAP SPHERE: if current pass didn't find enough boids, increase overlap sphere size; if it did, reduce it
        if (useAdaptiveOverlapSphere)
        {
            if (seenBoids.Count < maxSeenBoidsToStore && overlapSphereRadius < maxAdaptiveOverlapRadius)
            {
                overlapSphereRadius += adaptiveOverlapSphereInc;
            }
            else if (overlapSphereRadius > minAdaptiveOverlapRadius)
            {
                overlapSphereRadius -= adaptiveOverlapSphereInc;
            }
        }

        return(seenBoids);
    }