using System;
using UnityEngine;

public class Enemy: LivingEntity
{   
    private SpriteRenderer _spriteRenderer;
    private GameObject _player;
    private Player _playerScript;

    public float speed = 20f;
    public int attackDamage = 1;

    private void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            _playerScript.Damage(attackDamage);
        }
    }

    private void Start()
    {
        _player = GameObject.FindGameObjectsWithTag("Player")[0];
        _playerScript = _player.GetComponent<Player>();
        _spriteRenderer = GetComponent<SpriteRenderer>();
    }

    private void Update()
    {
        Vector2 direction = transform.position - _player.transform.position;
        var rad2Deg = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0, 0, rad2Deg + 180);
        
        transform.Translate(direction.normalized * (Time.deltaTime * speed * -1));
    }

    protected override void OnDamaged(float damage)
    {
        var spriteRenderer = _spriteRenderer;
        
    }
}