using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LeafSpawner : MonoBehaviour {
    public float Radius = 3;
    public float Rate = 5;
    public LeafPhysics[] Leafs;
    // Use this for initialization
    private LeafHandler leafHandler;
	void Start () {
        leafHandler = GameObject.FindObjectOfType<LeafHandler>();
	}

    private List<LeafPhysics> leaves = new List<LeafPhysics>();

    private float time = 0;
	
	// Update is called once per frame
	void Update () {
        float interval = 1 / Rate;
        time += Time.deltaTime;
        while (time > interval)
        {
            time -= interval;
            if (leafHandler != null && leafHandler.NumLeaves < leafHandler.MaxLeaves)
            {                
                float angle = UnityEngine.Random.Range(0, Mathf.PI * 2);
                float r = UnityEngine.Random.Range(0, Radius);

                Vector3 point = transform.position + new Vector3(Mathf.Sin(angle) * r, 0, Mathf.Cos(angle) * r);
                LeafPhysics leaf = GameObject.Instantiate<LeafPhysics>(Leafs[Random.Range(0, Leafs.Length)]);
                leaves.Add(leaf);
                leaf.transform.position = point;
                leaf.transform.rotation = Quaternion.Euler(new Vector3((Random.value - 0.5f) * 25, Random.value * 360, (Random.value - 0.5f) * 25));

                float scale = Random.Range(0.9f, 1.3f);
                leaf.transform.localScale = new Vector3(scale, scale, scale);
            }
        }
	}    

}



