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

public class Plant : MonoBehaviour
{
    public GameObject PlantPrefab;
    public float Interval;
    private float plantTimer;
    public float Range;
    public int MaxSeeds;
    private int counter;

    public ParticleSystem PlantFX;

    // Start is called before the first frame update
    void OnEnable()
    {
        PlantFX.Play(true);
    }

    // Update is called once per frame
    void Update()
    {
        plantTimer += Time.deltaTime;
        if(plantTimer > Interval)
        {
            Vector2 randomPoint = Random.insideUnitCircle;
            Vector3 tracePoint = new Vector3(randomPoint.x, 0, randomPoint.y) * Range + Vector3.up * 50 + transform.position;
            RaycastHit hit;
            Debug.DrawRay(tracePoint, Vector3.down * 100, Color.red, 1);
            if(Physics.Raycast(tracePoint, Vector3.down, out hit, 100, 1<<9))
            {
                Instantiate(PlantPrefab, hit.point, Quaternion.AngleAxis(Random.Range(0.0f, 360.0f), Vector3.up));
                counter++;
                if(counter >= MaxSeeds)
                    enabled = false;
            }
                
            plantTimer = 0;
            

        }
    }
}
