コード例 #1
0
 private bool CheckForEnemiesAlive()
 {
     if (Enemy_Manager_V2.GetEnemyList().Count > 0)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
コード例 #2
0
    //TODO: Finish Attack Priority finding
    //  ^ seems to be working
    //TODO: Change DummyAttack() to some real attack animation handler
    IEnumerator ContinueAttackingTargets()
    {
        while (true)
        {
            List <Enemy_V2> enemies_list = Enemy_Manager_V2.GetEnemyList();

            Enemy_V2 target = null;

            //get list of all targets in radius
            List <Enemy_V2> enemies_in_range = new List <Enemy_V2>();
            foreach (Enemy_V2 en in enemies_list)
            {
                if ((transform.position - en.transform.position).sqrMagnitude < attack_radius_current * attack_radius_current) //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 (current_attack_priority)
                {
                case AttackPriority.hp_highest:
                    target = enemies_in_range[0];
                    foreach (Enemy_V2 en in enemies_in_range)
                    {
                        if (en.current_hp > target.current_hp)
                        {
                            target = en;
                        }
                    }
                    break;

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

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

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

                case AttackPriority.first:      //TODO
                    target = enemies_in_range[0];
                    foreach (Enemy_V2 en in enemies_in_range)
                    {
                        if (en.GetCurrentWaypointIndex() > target.GetCurrentWaypointIndex())
                        {
                            target = en;
                        }
                        else if (en.GetCurrentWaypointIndex() == target.GetCurrentWaypointIndex() && en.GetSquareDistanceToNextWaypoint() < target.GetSquareDistanceToNextWaypoint())
                        {
                            target = en;
                        }
                    }
                    break;

                case AttackPriority.last:      //TODO
                    target = enemies_in_range[0];
                    foreach (Enemy_V2 en in enemies_in_range)
                    {
                        if (en.GetCurrentWaypointIndex() < target.GetCurrentWaypointIndex())
                        {
                            target = en;
                        }
                        else if (en.GetCurrentWaypointIndex() == target.GetCurrentWaypointIndex() && en.GetSquareDistanceToNextWaypoint() > target.GetSquareDistanceToNextWaypoint())
                        {
                            target = en;
                        }
                    }
                    break;

                default:
                    target = enemies_in_range[0];     //default to first (only guaranteed) element.
                    break;
                }//end switch
            }
            //Check to see if a target was found. Attack it
            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);                  //TODO: change to a handler of some kind

                time_since_last_attack = Time.time;
                target.ApplyDamage(attack_damage_current);
                yield return(new WaitForSeconds(attack_cooldown_current)); //invoke cooldown if we successfully attacked
            }
            else
            {
                yield return(null); //we did not attack. No cooldown invoked.
            }
        }
    }