示例#1
0
    /// <summary>
    ///This function is called to find a tower to send the troops
    /// </summary>
    public TowerBehavior FindTowerToSendTroops(AIBehavior attackingAI)
    {
        //gives me a dictionary with numUnits associated with their towers lowest num being at the 0 element
        List <List <TowerBehavior> > UnitList = attackingAI.GetUnitPriority();
        //gives me a dictionary with distance associated with the tower from the origin. smallest distance is the zero element
        List <List <TowerBehavior> > DistanceList = attackingAI.GetDistancePriority();

        //create a final list that holds a tower and it's total score from both dictionaries
        Dictionary <TowerBehavior, int> finalDict = new Dictionary <TowerBehavior, int>();


        //this variable is used for the value that we will give each tower
        //we put it outside the foreach loop so that towers in the same
        //list are given the same value
        int counter = 0;

        //go through each list in the unit dictionary
        foreach (List <TowerBehavior> list in UnitList)
        {
            //go through each tower through each list
            foreach (TowerBehavior tower in list)
            {
                //add that tower the the final dictionary with its value
                finalDict.Add(tower, counter);
            }
            //increment counter after going through the list
            counter++;
        }

        //reset the counter
        counter = 0;
        //go through each list in the distance dictionary
        foreach (List <TowerBehavior> list in DistanceList)
        {
            //go through each tower in each list
            foreach (TowerBehavior tower in list)
            {
                //if the final dictionary already contains the tower
                if (finalDict.ContainsKey(tower))
                {
                    //add the new counter to the final value
                    finalDict[tower] += (counter);
                }

                //if not, then something is wrong
                else
                {
                     #if Unity_Editor
                    Debug.LogError("DICTIONARY DOESN'T CONTAIN TOWER ALREADY,RETURNING NULL");
                     #endif
                    return(null);
                }
            }
            //increment the counter
            counter++;
        }

        //send the dictionary to the function so that the tower can actually be chosen
        return(FindTowerToAttack(finalDict));
    }