using UnityEngine;

public class RockSpawner : MonoBehaviour
{
    [Header("Spawn")]
    public Transform mouthPoint;
    public GameObject[] rockPrefab;
    public float spawnInterval = 0.6f;
    public int burstCount = 1;
    public bool looping = true;

    [Header("Launch Force")]
    public Vector2 minForce = new Vector2(-1f, 6f);
    public Vector2 maxForce = new Vector2(1f, 9f);
    public float angularVelocityRange = 200f;

    [Header("Physics tweaks")]
    public float rockGravityScale = 1.5f;
    public float rockMass = 1f;

    private void Start()
    {
        if (looping) InvokeRepeating(nameof(SpawnBurst), 0.2f, spawnInterval);
    }

    public void SpawnBurst()
    {
        for (int i = 0; i < burstCount; i++)
        {
            SpawnRock();
        }
    }

    void SpawnRock()
    {
        if (rockPrefab == null || mouthPoint == null) return;

        GameObject go = Instantiate(rockPrefab[Random.Range(0, rockPrefab.Length)], mouthPoint.position, Quaternion.identity);
        Rigidbody2D rb = go.GetComponent<Rigidbody2D>();
        if (rb == null) rb = go.AddComponent<Rigidbody2D>();

        // Apply physics settings
        rb.gravityScale = rockGravityScale;
        rb.mass = rockMass;
        rb.constraints = RigidbodyConstraints2D.None;

        // randomize force: x between min and max x, y between min and max y
        float fx = Random.Range(minForce.x, maxForce.x);
        float fy = Random.Range(minForce.y, maxForce.y);
        Vector2 force = new Vector2(fx, fy);

        rb.AddForce(force, ForceMode2D.Impulse);

        // random angular velocity
        rb.angularVelocity = Random.Range(-angularVelocityRange, angularVelocityRange);

        // Optional: destroy after some time to clean up

    }
}




 