Exemplo n.º 1
0
    void Breed()
    {
        // New babies List
        List <Bunny> tempList = new List <Bunny>();

        for (int i = 1; i < population.Count; i += 2)
        {
            int breeder1Index = i - 1;
            int breeder2Index = i;

            float howGenesAreSplit = UnityEngine.Random.Range(0.0f, 1.0f);

            Bounds bounds = environment.GetComponent <Renderer>().bounds;

            Bunny childBunny1 = CreateBunny(bounds);
            Bunny childBunny2 = CreateBunny(bounds);

            tempList.Add(childBunny1);
            tempList.Add(childBunny2);

            if (howGenesAreSplit < 0.16f)
            {
                Color tempColor = new Color(population[breeder1Index].color.r,
                                            population[breeder1Index].color.g,
                                            population[breeder2Index].color.b);

                tempColor = EvaluateMutation(tempColor);

                childBunny1.SetColor(tempColor);

                tempColor = new Color(population[breeder2Index].color.r,
                                      population[breeder2Index].color.g,
                                      population[breeder1Index].color.b);

                tempColor = EvaluateMutation(tempColor);

                childBunny2.SetColor(tempColor);
            }
            else if (howGenesAreSplit < 0.32f)
            {
                Color tempColor = new Color(population[breeder1Index].color.r,
                                            population[breeder2Index].color.g,
                                            population[breeder1Index].color.b);

                tempColor = EvaluateMutation(tempColor);

                childBunny1.SetColor(tempColor);

                tempColor = new Color(population[breeder2Index].color.r,
                                      population[breeder1Index].color.g,
                                      population[breeder2Index].color.b);

                tempColor = EvaluateMutation(tempColor);

                childBunny2.SetColor(tempColor);
            }
            else if (howGenesAreSplit < 0.48f)
            {
                Color tempColor = new Color(population[breeder1Index].color.r,
                                            population[breeder2Index].color.g,
                                            population[breeder2Index].color.b);

                tempColor = EvaluateMutation(tempColor);

                childBunny1.SetColor(tempColor);

                tempColor = new Color(population[breeder2Index].color.r,
                                      population[breeder1Index].color.g,
                                      population[breeder1Index].color.b);

                tempColor = EvaluateMutation(tempColor);

                childBunny2.SetColor(tempColor);
            }
            else if (howGenesAreSplit < 0.64f)
            {
                Color tempColor = new Color(population[breeder2Index].color.r,
                                            population[breeder1Index].color.g,
                                            population[breeder1Index].color.b);

                tempColor = EvaluateMutation(tempColor);

                childBunny1.SetColor(tempColor);

                tempColor = new Color(population[breeder1Index].color.r,
                                      population[breeder2Index].color.g,
                                      population[breeder2Index].color.b);

                tempColor = EvaluateMutation(tempColor);

                childBunny2.SetColor(tempColor);
            }
            else if (howGenesAreSplit < 0.80f)
            {
                Color tempColor = new Color(population[breeder2Index].color.r,
                                            population[breeder2Index].color.g,
                                            population[breeder1Index].color.b);

                tempColor = EvaluateMutation(tempColor);

                childBunny1.SetColor(tempColor);

                tempColor = new Color(population[breeder1Index].color.r,
                                      population[breeder1Index].color.g,
                                      population[breeder2Index].color.b);

                tempColor = EvaluateMutation(tempColor);

                childBunny2.SetColor(tempColor);
            }
            else
            {
                Color tempColor = new Color(population[breeder2Index].color.r,
                                            population[breeder1Index].color.g,
                                            population[breeder2Index].color.b);

                tempColor = EvaluateMutation(tempColor);

                childBunny1.SetColor(tempColor);

                tempColor = new Color(population[breeder1Index].color.r,
                                      population[breeder2Index].color.g,
                                      population[breeder1Index].color.b);

                tempColor = EvaluateMutation(tempColor);

                childBunny2.SetColor(tempColor);
            }
        }

        population.AddRange(tempList);
    }