using UnityEngine;

public class Projectile : MonoBehaviour
{
    private Transform _target;
    public float speed = 20f;
    public float damage = 20f;

    public void Seek(Transform target)
    {
        _target = target;
    }

    void Update()
    {
        if (_target == null)
        {
            Destroy(gameObject);
            return;
        }

        // 타겟 방향으로 이동
        Vector3 dir = _target.position - transform.position;
        float distanceThisFrame = speed * Time.deltaTime;

        // 적에게 도달했는지 확인
        if (dir.magnitude <= distanceThisFrame)
        {
            HitTarget();
            return;
        }

        transform.Translate(dir.normalized * distanceThisFrame, Space.World);
        transform.LookAt(_target); // 적을 바라보게 회전
    }

    void HitTarget()
    {
        IDamageable damageable = _target.GetComponentInParent<IDamageable>();
        if (damageable != null)
        {
            damageable.TakeDamage(damage);
        }

        Destroy(gameObject); // 충돌 후 소멸
    }
}