using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyLife : MonoBehaviour {

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            PlayerMovement playerMovement = collision.GetComponent<PlayerMovement>();
            if (playerMovement.Attacking)
            {
                DestroyEnemy();
            }
            else
            {
                if (playerMovement.enabled) {
                    playerMovement.ApplyDamage();
                }
            }
        }
    }

    public void DestroyEnemy ()
    {
        if (GameObject.FindGameObjectsWithTag("Enemy") == null)
        {
            GetComponent<AudioSource>().Stop();
        }

        GameObject deathAnim = ObjectPooler.Instance.GetPooledObject(PoolObjectTags.EnemyDeath.ToString());
        deathAnim.transform.position = transform.position;
        deathAnim.transform.localScale = transform.localScale;
        deathAnim.SetActive(true);
        gameObject.SetActive(false);
    }
}
