IEnumerator InCombat(AIwarriorController enemy)
    {
        if (firstHit == false)
        {
            GetComponent <AudioSource>().PlayOneShot(swordClashFX, 0.5f);
        }

        if (firstHit == true && health > 0)
        {
            GetComponent <AudioSource>().PlayOneShot(swordClashFX, 0.5f);                       ////play the on-hit sound effect at relatively low volume
            yield return(new WaitForSeconds(attackSpeed));
        }
        firstHit = true;
        if (health <= 0)
        {
            Die();
        }
        else if (enemy.health > 0 && animator.GetBool("inRange") == true)
        {
            enemy.health = enemy.health - attackDamage;
            StartCoroutine(InCombat(enemy));
        }
        else
        {
            firstHit = false;
            resetWarrior();
        }
    }
 void OnCollisionStay2D(Collision2D col)
 {
     if (col.transform.tag == "warrior")
     {
         colAnimator = col.transform.GetComponent <Animator> ();
         Vector2 relativePosition = transform.InverseTransformPoint(col.transform.position);
         if (relativePosition.x < 0)
         {
             colAnimator.SetBool("isIdle", true);
         }
     }
     if (col.transform.tag == "Archer")
     {
         colAnimator = col.transform.GetComponent <Animator> ();
         Vector2 relativePosition = transform.InverseTransformPoint(col.transform.position);
         if (relativePosition.x < 0)
         {
             colAnimator.SetBool("isIdle", true);
         }
     }
     if (col.transform.tag == "AIwarrior" && animator.GetBool("inRange") == false)
     {
         AIwarriorController someObject = col.gameObject.GetComponent <AIwarriorController>();
         animator.SetBool("inRange", true);
         animator.SetBool("isIdle", false);
         StartCoroutine(InCombat(someObject));
     }
     if (col.transform.tag == "AIcastle" && animator.GetBool("inRange") == false)
     {
         AIHealthBar castleObject = col.gameObject.GetComponent <AIHealthBar>();
         animator.SetBool("inRange", true);
         animator.SetBool("isIdle", false);
         StartCoroutine(InCombat(castleObject));                             //get the reference of the enemy castle healthbar and pass it to the combat method
     }
     if (col.transform.tag == "AIworker" && animator.GetBool("inRange") == false)
     {
         AIworkerController workerObject = col.gameObject.GetComponent <AIworkerController>();
         animator.SetBool("inRange", true);
         animator.SetBool("isIdle", false);
         StartCoroutine(InCombat(workerObject));                             //get the reference of the enemy warrior and pass it to the combat method
     }
     if (col.transform.tag == "AIarcher" && animator.GetBool("inRange") == false)
     {
         AIarcherController someObject = col.gameObject.GetComponent <AIarcherController>();
         animator.SetBool("inRange", true);
         animator.SetBool("isIdle", false);
         StartCoroutine(InCombat(someObject));
     }
 }
Exemple #3
0
 void OnCollisionEnter2D(Collision2D col)
 {
     if (col.transform.tag == "AIwarrior")
     {
         Rigidbody2D arrowBody = thisArrow.GetComponent <Rigidbody2D>();
         arrowBody.isKinematic = true;
         AIwarriorController warriorObject = col.gameObject.GetComponent <AIwarriorController>();
         warriorObject.health = warriorObject.health - PlayerArcherController.attackDamage;
         Destroy(thisArrow);
     }
     else
     {
         Destroy(thisArrow);
     }
 }
Exemple #4
0
 //determine whether a target is in range to start attacking
 void GetTarget()
 {
     target = GameObject.FindGameObjectWithTag("AIwarrior");
     if (target == null)
     {
         target = GameObject.FindGameObjectWithTag("AIarcher");
     }
     if (target == null)
     {
         target = GameObject.FindGameObjectWithTag("AIworker");
     }
     if (target == null)
     {
         target = GameObject.FindGameObjectWithTag("AIcastle");
     }
     if (target != null)
     {
         distance    = Vector3.Distance(transform.position, target.transform.position);
         targetFound = distance <= maxRange;
     }
     if (targetFound == true)
     {
         if (target.transform.tag == "AIwarrior")
         {
             AIwarriorController AIwarriorObject = target.GetComponent <AIwarriorController>();
             StartCoroutine(Shoot(AIwarriorObject));
         }
         if (target.transform.tag == "AIworker")
         {
             AIworkerController AIworkerObject = target.GetComponent <AIworkerController>();
             StartCoroutine(Shoot(AIworkerObject));
         }
         if (target.transform.tag == "AIarcher")
         {
             AIarcherController AIarcherObject = target.GetComponent <AIarcherController>();
             StartCoroutine(Shoot(AIarcherObject));
         }
         if (target.transform.tag == "AIcastle")
         {
             AIHealthBar AIcastleObject = target.GetComponent <AIHealthBar>();
             StartCoroutine(Shoot(AIcastleObject));
         }
     }
 }
Exemple #5
0
/*------------------Overloaded Shoot methods for each different attackable unit-----------------------------*/
    IEnumerator Shoot(AIwarriorController enemyWarrior)
    {
        animator.SetBool("inRange", true);
        //keep track of first hit so the unit starts attacking as soon as it collides instead of waiting and then begining to attack
        if (firstHit == false)
        {
            GetComponent <AudioSource>().PlayOneShot(bowReleaseSFX, 1);
        }

        /*if (firstHit == true && health > 0) {
         *      GetComponent<AudioSource>().PlayOneShot (bowReleaseSFX, 1f);
         *      yield return new WaitForSeconds (attackSpeed);
         * }*/
        firstHit = true;

        //if this warrior dies during combat then call the DIE method
        if (health <= 0)
        {
            Die();
        }

        else if (enemyWarrior.health > 0 && animator.GetBool("inRange") == true)
        {
            GetComponent <AudioSource>().PlayOneShot(bowReleaseSFX, 1);
            Instantiate(_arrow, thisArcher.transform.position, Quaternion.identity);
            yield return(new WaitForSeconds(attackSpeed));

            StartCoroutine(Shoot(enemyWarrior));
        }
        //else the enemy has been killed thus, reset the warrior
        else
        {
            firstHit = false;
            animator.SetBool("inRange", false);
        }
    }