示例#1
0
    // Handles the rat attacking an enemy
    IEnumerator Attack()
    {
        // Play animation
        //Debug.Log("Attack!Rat@" + Time.time);

        if (canAttack)
        {
            canAttack = false;
            if (anims)
            {
                // Put attack animation here
                anims.SetTrigger(ATK_ANIMATION_TRIGGER);
            }
            // For rodents
            Rodent      _EnemyRodent = currentTarget.GetComponent <Rodent>();
            PlayerStats king         = currentTarget.GetComponent <PlayerStats>();

            if (_EnemyRodent)
            {
                if (!_EnemyRodent.isDead())
                {// Reduce enemy health
                    _EnemyRodent.Damage(attackDamage);
                    SoundManager.Instance.PlayCombat();
                }
                else
                {
                    _inRange.Remove(currentTarget);
                    // print("Called from Dead Rodent found");
                    FindNextTargetInRange();
                }
            }
            else if (king)
            {
                if (!king.isDead())
                {
                    king.Damage(attackDamage);
                    SoundManager.Instance.PlayCombat();
                }
                else
                {
                    _inRange.Remove(currentTarget);
                    FindNextTargetInRange();
                }
            }
            else
            {
                BuildableObject _enemyBuilding = currentTarget.GetComponent <BuildableObject>();
                if (_enemyBuilding.getHP() > 0 && _enemyBuilding.getType() != BuildableObject.BuildingType.TownCenter)
                {
                    _enemyBuilding.Damage(attackDamage);
                    SoundManager.Instance.PlayCombat();
                }
                else
                {
                    _inRange.Remove(currentTarget);
                    // print("Called from Dead Building found");
                    FindNextTargetInRange();
                }
            }


            yield return(new WaitForSeconds(1.16f)); //Length of attack animation

            canAttack = true;
        }
    }