using System;
using UnityEngine;

public class Projectile : MonoBehaviour
{
    [SerializeField] private float moveSpeed;
    
    public int projectileDamage = 10;
    public Vector2 Direction = Vector2.zero;
    private Rigidbody2D _rb;
    private void Awake()
    {
        _rb = GetComponent<Rigidbody2D>();
    }

    private void FixedUpdate()
    {
        if(!Direction.Equals(Vector2.zero)) MoveProjectile();
    }

    private void MoveProjectile()
    {
        Vector2 pos = _rb.position;
        Vector2 direction = Direction;
        _rb.MovePosition(pos + direction*(moveSpeed * Time.fixedDeltaTime));
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            Health playerHealth = other.gameObject.GetComponent<Health>();
            playerHealth.TakeDamage(projectileDamage);
            Destroy(gameObject);
        }
    }
}
