Skip to content
Snippets Groups Projects
EnemyAttack.cs 3.85 KiB
using UnityEngine;
using System.Collections;

public class EnemyAttack : MonoBehaviour
{
    public float attackOffset = 0.5f;
    public int attackDamage = 10;

    Animator anim;
    GameObject player;
    GameObject pet;
    PlayerHealth playerHealth;
    EnemyHealth enemyHealth;
    EnemyMovement enemyMovement;
    PetHealth petHealth;
    bool playerInRange;
    bool petInRange;
    float timer;
    bool animating;


    void Awake ()
    {
        player = GameObject.FindGameObjectWithTag ("Player");
        playerHealth = player.GetComponent <PlayerHealth> ();

        pet = GameObject.FindGameObjectWithTag ("Pet");
        if(pet != null)
        {
            petHealth = pet.GetComponent <PetHealth> ();
        }

        enemyHealth = GetComponent<EnemyHealth>();
        enemyMovement = GetComponent<EnemyMovement>();

        anim = GetComponent <Animator> ();

        animating = false;
    }

    void OnTriggerEnter (Collider other)
    {
        // If the entering collider is the player...
        if(other.gameObject == player && other.isTrigger == false)
        {
            // ... the player is in range.
            playerInRange = true;
            anim.SetBool("IsNearPlayer", true);
        }
        if(enemyMovement.petExist)
        {
            if(other.gameObject == pet && other.isTrigger == false)
            {
                petInRange = true;
                anim.SetBool("IsNearPlayer", true);
            }
        }
    }

    void OnTriggerExit (Collider other)
    {
        // If the exiting collider is the player...
        if(other.gameObject == player)
        {
            // ... the player is no longer in range.
            playerInRange = false;
            timer = 0f;
            animating = false;
            anim.SetBool("IsNearPlayer", false);
        }
        if(enemyMovement.petExist)
        {
            if(other.gameObject == pet)
            {
                petInRange = false;
                timer = 0f;
                animating = false;
                anim.SetBool("IsNearPlayer", false);
            }
        }
    }


    void Update ()
    {
        if (playerInRange || petInRange) {
            timer += Time.deltaTime;
        }
        // If the timer exceeds the time between attacks, the player is in range and this enemy is alive...
        if(animating == false && timer >= attackOffset && (playerInRange || petInRange) && enemyHealth.currentHealth > 0)
        {
            // ... attack.
            // Debug.Log("Initiate attack"); // ! Debug
            Attack ();
        }
        if (animating == true) {
            if (timer >= anim.GetCurrentAnimatorStateInfo(0).length) {
                animating = false;
            }
        }

        // If the player has zero or less health...
        if (playerHealth.currentHealth <= 0)
        {
            // ... tell the animator the player is dead.
            anim.SetBool("PlayerDead", true);
        }
    }


    void Attack ()
    {
        Transform nearestAttackable = enemyMovement.getNearestAttackable();
        // Debug.Log("Enemy try to attack"); // ! Debug
        if(nearestAttackable.gameObject == player)
        {
            // Debug.Log("Enemy try to attack player"); // ! Debug
            // If the player has health to lose...
            if (playerHealth.currentHealth > 0)
            {
                // ... damage the player.
                playerHealth.TakeDamage(attackDamage);
            }
            animating = true;
            timer = 0f;
        }
        else if(nearestAttackable.gameObject == pet) {
            // Debug.Log("Enemy try to attack pet"); // ! Debug
            // If the pet has health to lose...
            if (petHealth.currentHealth > 0)
            {
                // ... damage the pet.
                petHealth.TakeDamage(attackDamage);
            }
            animating = true;
            timer = 0f;
        }
    }
}