コード例 #1
0
    //TRIES TO GET CLOSER TO OTHER BIRDS. Calculates the cohesion vector to maintain the flock members close
    Vector3 Cohesion()
    {
        Vector3 cohesionVector = new Vector3();
        int     countMembers   = 0;
        var     neighbours     = flockManager.GetNeighbours(this, conf.cohesionRadius);

        //No neighbours close to the member
        if (neighbours.Count == 0)
        {
            return(cohesionVector);
        }
        //Basically gets the position of all the birds that are withn the range defined, computes the averge position of all of them and goes towards it.
        foreach (var member in neighbours)
        {
            if (isInFOV(member.position))
            {
                cohesionVector += member.position;
                countMembers++;
            }
        }

        if (countMembers == 0)
        {
            return(cohesionVector);
        }

        //We calculate the cohesion vector with the member position and other members in FOV position and we normalize it
        cohesionVector /= countMembers;
        cohesionVector  = cohesionVector - this.position;
        cohesionVector  = Vector3.Normalize(cohesionVector);
        return(cohesionVector);
    }