Example #1
0
    public GameObject Breed(GameObject plant1, GameObject plant2, string outName)
    {
        // breed/crossbreeds 2 plants and returns new plant of name outName


        GameObject outPlant = MakePlant(outName, "Aloe");

        //mix life expectancy of plant
        plant1.GetComponent <Genes>().CrossGenes(plant2.GetComponent <Genes>(), outPlant.GetComponent <Genes>());

        string[] dependencyNames = { "Lighting", "Temperature", "Water", "Fertiliser" };
        foreach (string dep in dependencyNames)
        {
            Genes dep1Gene = plant1.GetComponent <PlantRates>().GetDepComp(dep).gameObject.GetComponent <Genes>();
            Genes dep2Gene = plant2.GetComponent <PlantRates>().GetDepComp(dep).gameObject.GetComponent <Genes>();
            Genes outGene  = outPlant.GetComponent <PlantRates>().GetDepComp(dep).gameObject.GetComponent <Genes>();

            dep1Gene.CrossGenes(dep1Gene, outGene);
        }

        //choose structure passed down
        GameObject plant1Struct = plant1.transform.Find("Structure").gameObject;
        GameObject plant2Struct = plant2.transform.Find("Structure").gameObject;

        DestroyImmediate(outPlant.transform.Find("Structure").gameObject);

        GameObject childStruct;
        GameObject unchosenStruct;
        int        rInt = Random.Range(0, 2);

        if (rInt == 0)
        {
            childStruct    = (GameObject)Instantiate(plant1Struct, outPlant.transform);
            unchosenStruct = plant2Struct;
        }
        else
        {
            childStruct    = (GameObject)Instantiate(plant2Struct, outPlant.transform);
            unchosenStruct = plant1Struct;
        }
        childStruct.name = "Structure";


        //mix unchosen structure into child structure
        //get every obj in chosen structure and mix it with
        //obj of the same type in unchosen structure
        GameObject[] allChildStructObj  = getSubObjects(childStruct);
        GameObject[] allParentStructObj = getSubObjects(unchosenStruct);

        foreach (GameObject obj in allChildStructObj)
        {
            Genes objGenes = obj.GetComponent <Genes>();
            if (objGenes != null)
            {
                foreach (GameObject otherObj in allParentStructObj)
                {
                    if (otherObj.name == obj.name)
                    {
                        objGenes.CrossGenes(otherObj.GetComponent <Genes>(), objGenes);
                    }
                }
            }
        }

        outPlant.GetComponent <Timer>().timeElapsed = 0;
        ResetPlantComp(outPlant);

        return(outPlant);
    }