Пример #1
0
    /**
     * Remove one exist field for one exist behavior.
     */
    private void RemoveBehavior(CompositeBehavior cb)
    {
        // Get exist behavior array's length.
        var oldCount = cb.behaviors.Length;

        // Check there is only one exist behavior.
        if (oldCount == 1)
        {
            // Set behavior array and weight array to null.
            cb.behaviors        = null;
            cb.behaviorsWeights = null;
        }
        else
        {
            // Create a new behavior array(one less length).
            var newBehaviors = new FlockBehavior[oldCount - 1];
            // Create a new float array(one less length).
            var newWeights = new float[oldCount - 1];

            // Assign previous behaviors and weights unless the last behavior and weight.
            for (var i = 0; i < oldCount - 1; i++)
            {
                newBehaviors[i] = cb.behaviors[i];
                newWeights[i]   = cb.behaviorsWeights[i];
            }
            // Assign new behavior array and weight array back.
            cb.behaviors        = newBehaviors;
            cb.behaviorsWeights = newWeights;
        }
    }
Пример #2
0
 public void Initialize(SpeciesBehavior species)
 {
     m_species    = species;
     m_FlockAgent = species.m_prefab;
     behavior     = species.m_behavior;
     m_prey       = species.m_prey;
 }
    public void GUI_AddBehavior(CompositeBehavior compositeBehavior) //adicionar um comportamento
    {
        int newArraySize = 0;                                        //incializar valores

        if (compositeBehavior.flockBehaviors == null)                //se nao existir o array, criar
        {
            compositeBehavior.flockBehaviors   = new FlockBehavior[0];
            compositeBehavior.behaviorsWeights = new float[0];
        }

        newArraySize = compositeBehavior.flockBehaviors.Length + 1;            //pegar tamanho + 1

        FlockBehavior[] newFlockBehaviors   = new FlockBehavior[newArraySize]; //incializar valores
        float[]         newBehaviorsWeights = new float[newArraySize];

        for (int i = 0; i < newArraySize - 1; i++) //para cada elemento antigo, re adicionar no novo array
        {
            newFlockBehaviors[i]   = compositeBehavior.flockBehaviors[i];
            newBehaviorsWeights[i] = compositeBehavior.behaviorsWeights[i];
        }

        newBehaviorsWeights[newArraySize - 1] = 1f;             //adicionar valor default ao novo elemento

        compositeBehavior.flockBehaviors   = newFlockBehaviors; //setar novos arrays no lguar dos antigos
        compositeBehavior.behaviorsWeights = newBehaviorsWeights;
    }
Пример #4
0
    void ApplyTheseRules()
    {
        GameObject[] gos;
        gos = FlockPets.allPets;

        Vector3 vcent  = Vector3.zero;
        Vector3 vavoid = Vector3.zero;
        float   gSpeed = 0.5f;

        Vector3 goalPos = FlockPets.goalPos;

        float dist;

        int groupSize = 0;

        foreach (GameObject go in gos)
        {
            if (go != this.gameObject)
            {
                dist = Vector3.Distance(go.transform.position, this.transform.position);
                if (dist <= neighborDist)
                {
                    vcent += go.transform.position;
                    groupSize++;

                    //Avoids objects if they come too close
                    if (dist < 1.0f)
                    {
                        vavoid = vavoid + (this.transform.position - go.transform.position);
                    }

                    FlockBehavior notherFlock = go.GetComponent <FlockBehavior>();
                    gSpeed = gSpeed + notherFlock.speed;
                }
            }
        }

        //Calculates avg center in the group & calculates avg speed if in a group
        if (groupSize > 0)
        {
            vcent = vcent / groupSize + (goalPos - this.transform.position);
            speed = gSpeed / groupSize;

            //Use center vector towards the center and add away from anything we might hit...
            //...then subtract that from current position
            Vector3 dir = (vcent + vavoid) - transform.position;
            //If the direction is zero, we rotate from one direction to another...
            //...without hitting another object
            if (dir != Vector3.zero)
            {
                transform.rotation = Quaternion.Slerp(transform.rotation,
                                                      Quaternion.LookRotation(dir),
                                                      rotateSpeed * Time.deltaTime);
            }
        }
    }
        protected virtual void Start()
        {
            InitOffset();

            HP = MaxHP;

            PlayerGroup = Owner.Group;

            steerBh     = Singleton.SteerBehavior;
            flockBh     = Singleton.FlockBehavior;
            avoidanceBh = Singleton.ObstacleAvoidance;
        }
    void AddBehavior(CompositeBehavior cb)
    {
        int n = ((cb.behaviors != null) ? cb.behaviors.Length : 0) + 1;

        FlockBehavior[] newBehaviors = new FlockBehavior[n];
        float[]         newWeights   = new float[n];
        for (int i = 0; i < n - 1; i++)
        {
            newBehaviors[i] = cb.behaviors[i];
            newWeights[i]   = cb.weights[i];
        }
        newWeights[n - 1] = 1f;
        cb.behaviors      = newBehaviors;
        cb.weights        = newWeights;
    }
Пример #7
0
    public void AddBehavior()
    {
        int oldCount = (this.behaviors != null) ? this.behaviors.Length : 0;

        FlockBehavior[] newBehaviors = new FlockBehavior[oldCount + 1];
        float[]         newWeights   = new float[oldCount + 1];
        for (int i = 0; i < oldCount; i++)
        {
            newBehaviors[i] = this.behaviors[i];
            newWeights[i]   = this.weights[i];
        }
        newWeights[oldCount] = 1f;
        this.behaviors       = newBehaviors;
        this.weights         = newWeights;
    }
Пример #8
0
    void AddBehavior(CompositeBehavior cb)
    {
        int oldCount = (cb.behaviors != null) ? cb.behaviors.Length : 0;

        FlockBehavior[] newBehaviors = new FlockBehavior[oldCount + 1];
        float[]         newWeights   = new float[oldCount + 1];
        for (int i = 0; i < oldCount; i++)
        {
            newBehaviors[i] = cb.behaviors[i];
            newWeights[i]   = cb.weights[i];
        }
        newWeights[oldCount] = 1f;
        cb.behaviors         = newBehaviors;
        cb.weights           = newWeights;
    }
Пример #9
0
    private FlockBehavior[] Remove(int index, FlockBehavior[] old)
    {
        // Remove this behaviour
        var current = new FlockBehavior[old.Length - 1];

        for (int y = 0, x = 0; y < old.Length; y++)
        {
            if (y != index)
            {
                current[x] = old[y];
                x++;
            }
        }

        return(current);
    }
Пример #10
0
    public void RemoveBehavior(int index)
    {
        FlockBehavior[] newBehaviors = new FlockBehavior[behaviors.Length - 1];
        float[]         newWeights   = new float[weights.Length - 1];
        int             j            = 0;

        for (int i = 0; i < behaviors.Length; i++)
        {
            if (i != index)
            {
                newBehaviors[j] = behaviors[i];
                newWeights[j]   = weights[i];
                j++;
            }
        }
        behaviors = newBehaviors;
        weights   = newWeights;
    }
Пример #11
0
    // Add and remove behavior methods
    void AddBehavior(CompositeBehavior cb)
    {
        // get the original size of the array
        int oldCount = (cb.behaviors != null ? cb.behaviors.Length : 0);

        FlockBehavior[] newBehaviors = new FlockBehavior[oldCount + 1];
        float[]         newWeights   = new float[oldCount + 1];

        for (int i = 0; i < oldCount; i++)
        {
            newBehaviors[i] = cb.behaviors[i];
            newWeights[i]   = cb.weights[i];
        }

        newWeights[oldCount] = 1f;
        cb.behaviors         = newBehaviors;
        cb.weights           = newWeights;
    }
    void RemoveBehavior(CompositeBehavior cb)
    {
        int n = cb.behaviors.Length - 1;

        if (n == 0)
        {
            cb.behaviors = null;
            cb.weights   = null;
            return;
        }
        FlockBehavior[] newBehaviors = new FlockBehavior[n];
        float[]         newWeights   = new float[n];
        for (int i = 0; i < n; i++)
        {
            newBehaviors[i] = cb.behaviors[i];
            newWeights[i]   = cb.weights[i];
        }
        cb.behaviors = newBehaviors;
        cb.weights   = newWeights;
    }
Пример #13
0
    void RemoveBehavior(CompositeBehavior cb)
    {
        int oldCount = cb.behaviors.Length;

        if (oldCount == 1)
        {
            cb.behaviors = null;
            cb.weights   = null;
            return;
        }
        FlockBehavior[] newBehaviors = new FlockBehavior[oldCount - 1];
        float[]         newWeights   = new float[oldCount - 1];
        for (int i = 0; i < oldCount - 1; i++)
        {
            newBehaviors[i] = cb.behaviors[i];
            newWeights[i]   = cb.weights[i];
        }
        cb.behaviors = newBehaviors;
        cb.weights   = newWeights;
    }
Пример #14
0
    private void LoadScriptableObjectByName(PreferencesData data)
    {
        Debug.Log("NewSession, load from name");
        FlockBehavior[] behaviors = new FlockBehavior[data.behaviorsName.Count];
        for (int i = 0; i < data.behaviorsName.Count; i++)
        {
            behaviors[i] = Resources.Load <FlockBehavior>("UIPrefab" + Path.DirectorySeparatorChar
                                                          + "BoidsBehaviors" + Path.DirectorySeparatorChar + data.behaviorsName[i]);
        }
        data.CompositeBehavior.behaviors = behaviors;
        data.CompositeBehavior.weights   = data.weigth.ToArray();

        GeneticBehavior[] genes = new GeneticBehavior[data.genesName.Count];
        for (int i = 0; i < data.genesName.Count; i++)
        {
            genes[i] = Resources.Load <GeneticBehavior>("UIPrefab" + Path.DirectorySeparatorChar
                                                        + "Genetic" + Path.DirectorySeparatorChar + data.genesName[i]);
        }
        data.compositeGene.genes = genes;
    }
Пример #15
0
    /**
     * Add one new field for one new behavior.
     */
    private void AddBehavior(CompositeBehavior cb)
    {
        // First check if new behavior array is not null and have at least one behavior.
        var oldCount = cb.behaviors?.Length ?? 0;
        // Create a new behavior array(one more length).
        var newBehaviors = new FlockBehavior[oldCount + 1];
        // Create a new float array(one more length).
        var newWeights = new float[oldCount + 1];

        // Assign previous behaviors and weights.
        for (var i = 0; i < oldCount; i++)
        {
            newBehaviors[i] = cb.behaviors[i];
            newWeights[i]   = cb.behaviorsWeights[i];
        }

        // Set the new behavior's weight to 1f.
        newWeights[oldCount] = 1f;
        // Assign new behavior array and weight array back.
        cb.behaviors        = newBehaviors;
        cb.behaviorsWeights = newWeights;
    }
    private void RemoveBehavior(CompositeBehavior cb)
    {
        int oldCount = cb.BehaviorList.Length;

        if (oldCount == 1)
        {
            cb.BehaviorList = null;
            cb.Weights      = null;
            return;
        }
        else
        {
            FlockBehavior[] newBehaviors = new FlockBehavior[oldCount - 1];
            float[]         newWeights   = new float[oldCount - 1];
            for (int i = 0; i < oldCount; i++)
            {
                newBehaviors[i] = cb.BehaviorList[i];
                newWeights[i]   = cb.Weights[i];
            }
            cb.BehaviorList = newBehaviors;
            cb.Weights      = newWeights;
        }//3060 4176
    }
    public void GUI_RemoveBehavior(CompositeBehavior compositeBehavior, int position) //remover um comportamento
    {
        position = Mathf.Clamp(position, 0, compositeBehavior.flockBehaviors.Length); //ajustar posicao para nao passar o array

        int newArraySize = 0;                                                         //incializar valores

        if (compositeBehavior.flockBehaviors != null)                                 //se existir o array, pegar tamanho + 1
        {
            newArraySize = compositeBehavior.flockBehaviors.Length - 1;
        }

        FlockBehavior[] newFlockBehaviors   = new FlockBehavior[newArraySize]; //incializar valores
        float[]         newBehaviorsWeights = new float[newArraySize];

        for (int i = 0; i < newArraySize; i++) //para cada elemento antigo, re adicionar no novo array
        {
            newFlockBehaviors[i]   = compositeBehavior.flockBehaviors[i >= position ? i + 1 : i];
            newBehaviorsWeights[i] = compositeBehavior.behaviorsWeights[i >= position ? i + 1 : i];
        }

        compositeBehavior.flockBehaviors   = newArraySize == 0 ? null : newFlockBehaviors; //setar novos arrays no lguar dos antigos
        compositeBehavior.behaviorsWeights = newArraySize == 0 ? null : newBehaviorsWeights;
    }
    public override void OnInspectorGUI()
    {
        CompositeBehavior compBehavior = (CompositeBehavior)target;

        // Check for behaviours
        if (compBehavior.behaviors == null || compBehavior.behaviors.Length == 0)
        {
            EditorGUILayout.HelpBox("No behaviours in array.", MessageType.Warning);
        }
        else
        {
            EditorGUILayout.BeginHorizontal();
            GUILayout.Space(20f);
            //EditorGUILayout.LabelField("Number", GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
            EditorGUILayout.LabelField("Behaviors", GUILayout.MinWidth(60f));
            EditorGUILayout.LabelField("Weights", GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
            EditorGUILayout.EndHorizontal();

            EditorGUI.BeginChangeCheck();
            for (int i = 0; i < compBehavior.behaviors.Length; ++i)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField(i.ToString(), GUILayout.MaxWidth(15f));
                compBehavior.behaviors[i] = (FlockBehavior)EditorGUILayout.ObjectField(compBehavior.behaviors[i], typeof(FlockBehavior), false, GUILayout.MinWidth(60f));
                compBehavior.weights[i]   = EditorGUILayout.FloatField(compBehavior.weights[i], GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
                EditorGUILayout.EndHorizontal();
            }
            if (EditorGUI.EndChangeCheck())
            {
                EditorUtility.SetDirty(compBehavior);
            }
        }

        GUILayout.Space(10f);
        // Buttons to add and remove behaviours in our containers
        EditorGUILayout.BeginVertical();
        if (GUILayout.Button("Add"))
        {
            AddBehavior(compBehavior);
            EditorUtility.SetDirty(compBehavior);
        }
        if (GUILayout.Button("Remove"))
        {
            RemoveBehavior(compBehavior);
            EditorUtility.SetDirty(compBehavior);
        }
        EditorGUILayout.EndVertical();

        void AddBehavior(CompositeBehavior cb)
        {
            int oldCount = (cb.behaviors != null) ? cb.behaviors.Length : 0;

            FlockBehavior[] newBehaviors = new FlockBehavior[oldCount + 1];
            float[]         newWeights   = new float[oldCount + 1];
            for (int i = 0; i < oldCount; i++)
            {
                newBehaviors[i] = cb.behaviors[i];
                newWeights[i]   = cb.weights[i];
            }
            newWeights[oldCount] = 1f;
            cb.behaviors         = newBehaviors;
            cb.weights           = newWeights;
        }

        void RemoveBehavior(CompositeBehavior cb)
        {
            int oldCount = cb.behaviors.Length;

            if (oldCount == 1)
            {
                cb.behaviors = null;
                cb.weights   = null;
                return;
            }
            FlockBehavior[] newBehaviors = new FlockBehavior[oldCount - 1];
            float[]         newWeights   = new float[oldCount - 1];
            for (int i = 0; i < oldCount - 1; i++)
            {
                newBehaviors[i] = cb.behaviors[i];
                newWeights[i]   = cb.weights[i];
            }
            cb.behaviors = newBehaviors;
            cb.weights   = newWeights;
        }
    }
Пример #19
0
    public override void OnInspectorGUI()
    {
        // Setup
        var current = (CompositeBehavior)target;

        EditorGUILayout.BeginHorizontal();

        // Draw
        if (current.behaviors == null || current.behaviors.Length == 0)
        {
            EditorGUILayout.HelpBox("No behaviours attached.", MessageType.Warning);
            EditorGUILayout.EndHorizontal();
        }
        else
        {
            EditorGUILayout.LabelField("Behaviours");
            EditorGUILayout.LabelField("Weights");

            EditorGUILayout.EndHorizontal();

            for (int i = 0; i < current.behaviors.Length; i++)
            {
                // Draw index
                EditorGUILayout.BeginHorizontal();

                if (GUILayout.Button("Remove") || current.behaviors[i] == null)
                {
                    // Remove this behaviour
                    current.behaviors = Remove(i, current.behaviors);
                    break;
                }

                current.behaviors[i] = (FlockBehavior)EditorGUILayout.ObjectField(current.behaviors[i], typeof(FlockBehavior), false);
                EditorGUILayout.Space(30);
                current.weights[i] = EditorGUILayout.Slider(current.weights[i], 0, 10);

                EditorGUILayout.EndHorizontal();
            }
        }

        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Add behaviour...");
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.BeginHorizontal();

        adding = (FlockBehavior)EditorGUILayout.ObjectField(adding, typeof(FlockBehavior), false);

        if (adding != null)
        {
            // add this item to the array
            var oldBehaviours = current.behaviors;
            current.behaviors = new FlockBehavior[oldBehaviours.Length + 1];
            var oldWeights = current.weights;
            current.weights = new float[oldWeights.Length + 1];

            for (int i = 0; i < oldBehaviours.Length; i++)
            {
                current.behaviors[i] = oldBehaviours[i];
                current.weights[i]   = oldWeights[i];
            }

            current.behaviors[oldBehaviours.Length] = adding;
            current.weights[oldWeights.Length]      = 1;

            adding = null;
        }
    }
Пример #20
0
 private void Start()
 {
     steerBh = AIUtils.steerBehaviorInstance;
     flockBh = AIUtils.flockBehaviorInstance;
 }