Example #1
0
    IEnumerator ContinueAttackingTargets(AttackPriority prio, List <enemy> enemies_list)
    {
        while (true)
        {
            //Debug.Log("ContinueAttackingTargets");
            enemy target = null;

            //get list of all targets in radius
            List <enemy> enemies_in_range = new List <enemy>();
            foreach (enemy en in enemies_list)
            {
                if ((transform.position - en.transform.position).sqrMagnitude < attack_radius * attack_radius) //sqrmagnitude cheaper than a costly sqrt operation
                {
                    enemies_in_range.Add(en);
                }
            }

            // Find the target based on the priority param
            // Note that a property of newly spawned enemies guarantees that the lower the index, the earlier it was spawned!
            // Equal values during search will be ignored; in that case first success will always be used
            if (enemies_in_range.Count > 0)
            {
                switch (prio)
                {
                case AttackPriority.hp_highest:
                    target = enemies_in_range[0];
                    foreach (enemy en in enemies_in_range)
                    {
                        if (en.hp > target.hp)
                        {
                            target = en;
                        }
                    }
                    break;

                case AttackPriority.hp_lowest:
                    target = enemies_in_range[0];
                    foreach (enemy en in enemies_in_range)
                    {
                        if (en.hp < target.hp)
                        {
                            target = en;
                        }
                    }
                    break;

                case AttackPriority.speed_fastest:
                    target = enemies_in_range[0];
                    foreach (enemy en in enemies_in_range)
                    {
                        if (en.speed > target.speed)
                        {
                            target = en;
                        }
                    }
                    break;

                case AttackPriority.speed_slowest:
                    target = enemies_in_range[0];
                    foreach (enemy en in enemies_in_range)
                    {
                        if (en.speed < target.speed)
                        {
                            target = en;
                        }
                    }
                    break;

                case AttackPriority.first:      //TODO
                    target = enemies_in_range[0];
                    break;

                case AttackPriority.last:      //TODO
                    target = enemies_in_range[0];
                    break;

                default:
                    target = enemies_in_range[0];     //default to first (only guaranteed) element.
                    break;
                }//end switch
            }
            //attack the targeted enemy
            if (target != null)
            {
                Debug.DrawLine(transform.position, target.transform.position, Color.yellow, .1f); //shows what it is hitting!
                DummyAttack(transform.position, target.transform.position, .1f);


                target.ApplyDamage(attack_damage);
                yield return(new WaitForSeconds(attack_cooldown)); //invoke cooldown if we successfully attacked
            }
            else
            {
                yield return(null); //we did not attack. No cooldown invoked.
            }
        }
    }