using UnityEngine;

public class GrenadeLauncher : MonoBehaviour
{
    public GameObject grenadePrefab;
    public Transform grenadeSpawnPoint;

    public float launchForce = 20f;
    public float upwardForce = 5f;
    public float fireRate = 1.0f;

    private float nextFireTime = 0f;

    public void FireGrenade()
    {
        if (Time.time < nextFireTime)
        {
            return;
        }

        if (!grenadePrefab || !grenadeSpawnPoint)
        {
            Debug.LogError("Grenade Prefab or Spawn Point is not assigned in the Grenade Launcher.");
            return;
        }

        nextFireTime = Time.time + fireRate;

        var grenadeInstance = Instantiate(grenadePrefab, grenadeSpawnPoint.position, grenadeSpawnPoint.rotation);
        var grenadeRigidbody = grenadeInstance.GetComponent<Rigidbody>();

        if (grenadeRigidbody)
        {
            var forceDirection = grenadeSpawnPoint.forward * launchForce + grenadeSpawnPoint.up * upwardForce;
            grenadeRigidbody.AddForce(forceDirection, ForceMode.Impulse);
        }
        else
        {
            Debug.LogError("The Grenade Prefab is missing a Rigidbody component.");
        }
    }
}