using UnityEngine;

public class Bullet : MonoBehaviour
{
    public float speed = 10f;
    public float lifeTime = 15f; // "X time" to exist

    void Start()
    {
        // Automatically destroy the bullet after X seconds
        Destroy(gameObject, lifeTime);
    }

    void Update()
    {
        // Move the bullet forward based on its own rotation
        transform.Translate(Vector2.right * speed * Time.deltaTime);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        // Check if it hits the wall/floor (Tilemap) or Player
        if (collision.CompareTag("Player") || collision.gameObject.layer == LayerMask.NameToLayer("Ground"))
        {
            if (collision.CompareTag("Player"))
            {
                // Handle player damage here
                collision.gameObject.GetComponent<PlayerMovement>().KillPlayer();
            }

            Destroy(gameObject); // End bullet on impact
        }
    }
}