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

public class LeafGenerator : MonoBehaviour {
    public float Radius = 6;
    public float Num = 500;
    public LeafPhysics[] Leafs;
	// Use this for initialization
	void Start () {
        int spawned = 0;

        Ray ray = new Ray();
        ray.direction = Vector3.down;

        RaycastHit hitInfo = new RaycastHit();
        int layer = LayerMask.GetMask("iso_piece");


        int tries = 0;
        while (spawned < Num && tries < 1000)
        {
            Vector2 offset = Random.insideUnitCircle;
            Vector3 point = transform.position + new Vector3(offset.x * Radius, 20, offset.y * Radius);

            ray.origin = point;
            bool hit = Physics.Raycast(ray, out hitInfo, 40, layer);

            if (hit && hitInfo.normal.y > 0.6f)
            {
                LeafPhysics leaf = GameObject.Instantiate<LeafPhysics>(Leafs[Random.Range(0, Leafs.Length)]);
                leaf.transform.position = hitInfo.point + Vector3.up * 0.07f;
                leaf.transform.rotation = Quaternion.Euler(new Vector3(0, Random.value * 360, 0));

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

                spawned++;
            }

            tries++;
        }
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}
