Example #1
0
    public void Init(int seed)
    {
        Random.InitState(seed);

        treeSpecies = gameObject.GetComponent <TreeSpecies>();
        // Load variables from species creator
        if (treeSpecies == null)
        {
            Debug.LogError("ERROR: No tree species attached!");
        }
        GetVariablesFromSpecies();
        treeSpecies.GenerateMaterials();

        //LODManager lodManager = new LODManager();

        //trunkObject = new GameObject("Trunk");
        //trunkObject.transform.parent = transform;
        trunkObject = gameObject.EnsureChildGameObject("Trunk");
        trunkObject.transform.localPosition = Vector3.zero;                                // Remove offset from parenting
        PGTreeTrunkSimple trunkScript = trunkObject.EnsureComponent <PGTreeTrunkSimple>(); //trunkObject.AddComponent<PGTreeTrunkSimple>();

        trunkObject.EnsureComponent <MeshRenderer>();                                      //trunkObject.AddComponent<MeshRenderer>();
        trunkObject.GetComponent <Renderer>().material = treeSpecies.m_trunk_mat;

        trunkScript.CreateObject(false);

        // Add collider
        collider        = trunkObject.EnsureComponent <CapsuleCollider>();
        collider.radius = (m_start_radius + m_end_radius) / 2.0f;
        collider.height = m_height;
        collider.center = new Vector3(0.0f, m_height / 2.0f - collider.radius, 0.0f);

        if (m_hasLeaves)
        {
            leafObject = gameObject.EnsureChildGameObject("Leaves");
            //leafObject.transform.parent = transform;
            leafObject.transform.localPosition = Vector3.zero;             // Remove offset from parenting
            PGTreeLeaf leafScript = leafObject.EnsureComponent <PGTreeLeaf>();
            leafObject.EnsureComponent <MeshRenderer>();
            leafObject.GetComponent <Renderer>().material = treeSpecies.m_leaf_mat;

            leafScript.CreateObject(false);
        }

        // Add fruit
        if (m_has_fruit)
        {
            int   fruitNum        = Random.Range(treeSpecies.m_fruit_num_min, treeSpecies.m_fruit_num_max + 1);
            int   leafPositionNum = trunkScript.Leaves.Count;
            int[] usedIndices     = new int[fruitNum];

            for (int i = 0; i < fruitNum && i < leafPositionNum; i++)
            {
                GameObject newFruit           = GameObject.Instantiate(treeSpecies.m_fruit) as GameObject;
                int        fruitPositionIndex = Random.Range(0, leafPositionNum - 1);
                bool       spareIndex         = false;
                int        loopSafetyCount    = 0;
                while (spareIndex && loopSafetyCount < leafPositionNum)
                {
                    fruitPositionIndex = Random.Range(0, leafPositionNum - 1);
                    // Check if position index already has a fruit
                    bool matchFound = false;
                    for (int j = 0; j < fruitNum; j++)
                    {
                        if (fruitPositionIndex == usedIndices[j])
                        {
                            matchFound = true;
                        }
                    }
                    loopSafetyCount++;
                    // If no match is found, no fruit is at that index
                    spareIndex = !matchFound;
                }
                usedIndices[i]                   = fruitPositionIndex;
                newFruit.transform.parent        = transform;
                newFruit.transform.localPosition = trunkScript.Leaves[fruitPositionIndex].position;
                newFruit.transform.rotation      = trunkScript.Leaves[fruitPositionIndex].rotation * Quaternion.Euler(180.0f, 0.0f, 0.0f);
            }
        }
    }