using UnityEngine;

public class AutomaticGeneratorLogic : MonoBehaviour
{
    [Header("Զ")]
    [Tooltip("ģ")]
    public GameObject itemProfab;
    [Tooltip("")]
    public int itemMaximumCount = 10;
    [Tooltip("")]
    public Transform itemBornPointAnchor;

    private System.Random random = new();

    // Start is called before the first frame update
    void Start()
    {
        // ָ
        for (int i = 0; i < itemMaximumCount; i++)
        {
            Invoke(nameof(generateItem), Random.Range(0.01f, 10f));
        }

        InvokeRepeating(nameof(checkItemCount), 3f, 3f);
    }

    private void checkItemCount()
    {
        // жǷټ
        if (transform.childCount < itemMaximumCount)
        {
            // ָ
            Invoke(nameof(generateItem), Random.Range(0.01f, 2f));
        }
    }

    /// <summary>
    /// 
    /// </summary>
    private void generateItem()
    {
        // ʵ
        GameObject obj = Instantiate(itemProfab, transform);
        // ƶλ
        float x = random.Next(60) * 0.01f;
        float y = random.Next(10) * 0.01f;
        float z = random.Next(60) * 0.01f;
        // ͼĵλ
        Vector3 centerPos = transform.localPosition + transform.lossyScale * 0.5f;
        if (itemBornPointAnchor != null)
        {
            centerPos = itemBornPointAnchor.localPosition;
            obj.transform.rotation = itemBornPointAnchor.rotation;
        }
        centerPos.x += x;
        centerPos.y += y;
        centerPos.z += z;
        //egg.transform.Translate(x, y, z, Space.World);
        obj.transform.localPosition = centerPos;
        obj.transform.localScale = new Vector3(0.1f, 2, 0.1f);
    }
}
