using UnityEngine;

public class Enemy : MonoBehaviour
{
    public float speed = 1.2f;
    public float stepDown = 0.35f;
    public float leftBound = -7.2f;
    public float rightBound = 7.2f;

    private int direction = 1;

    private void Update()
    {
        transform.Translate(Vector3.right * direction * speed * Time.deltaTime);

        if (transform.position.x >= rightBound)
        {
            direction = -1;
            transform.position = new Vector3(rightBound, transform.position.y - stepDown, 0f);
        }
        else if (transform.position.x <= leftBound)
        {
            direction = 1;
            transform.position = new Vector3(leftBound, transform.position.y - stepDown, 0f);
        }

        if (transform.position.y <= -4.5f)
        {
            GameManager.Instance?.PlayerHit();
            Destroy(gameObject);
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (!other.CompareTag("PlayerBullet")) return;

        GameManager.Instance?.NotifyEnemyDestroyed(gameObject);
        Destroy(other.gameObject);
        Destroy(gameObject);
    }
}
