コード例 #1
0
ファイル: AIManager.cs プロジェクト: phelow/FallOfTheTzar
    //AI routine for faction based turn mode, AI will move all available unit for the faction
    //called by UnitControl in OnNextTurn
    IEnumerator _AIRoutine(int factionID)
    {
        while (GameControlTB.IsActionInProgress())
        {
            yield return(null);
        }

        yield return(new WaitForSeconds(1f));

        Faction       faction = UnitControl.GetFactionInTurn(factionID);
        List <UnitTB> allUnit = faction.allUnitList;

        int counter = 0;

        if (GameControlTB.GetMoveOrder() == _MoveOrder.Free)
        {
            int count = allUnit.Count;

            //create a list (1, 2, 3..... ) to the size of the unit, this is for unit shuffling
            List <int> IDList = new List <int>();
            for (int i = 0; i < count; i++)
            {
                IDList.Add(i);
            }

            for (int i = 0; i < count; i++)
            {
                //randomly select a unit to move
                int rand = UnityEngine.Random.Range(0, IDList.Count);

                /*
                 * //if(counter%2==0) rand=0;
                 * //else rand=IDList.Count-1;
                 *
                 * string label="rand: "+rand+" ("+IDList.Count+")";
                 * for(int nn=0; nn<IDList.Count; nn++){
                 *      label+="     "+IDList[nn];
                 * }
                 * Debug.Log(label);
                 */

                int    ID   = IDList[rand];
                UnitTB unit = allUnit[ID];
                //remove the unit from allUnit list so that it wont be selected again
                IDList.RemoveAt(rand);

                counter += 1;

                //move the unit
                unitInAction = true;
                StartCoroutine(_MoveUnit(unit));

                //wait a frame for the unit to start moving, set the inAction flag, etc..
                yield return(null);

                //wait while the unit is in action (making its move)
                //and while the game event is in progress (counter attack, death effect/animation, etc.)
                //and while the unit localUnitInactionFlag hasn't been cleared
                while (unit.InAction() || GameControlTB.IsActionInProgress() || unitInAction)
                {
                    yield return(null);
                }

                //for counter-attack enabled, in case the unit is destroyed in after being countered
                if (unit.IsDestroyed())
                {
                    //label="";
                    for (int n = rand; n < IDList.Count; n++)
                    {
                        if (IDList[n] >= ID)
                        {
                            //label+="   "+IDList[n];
                            IDList[n] -= 1;
                        }
                    }
                    //Debug.Log("unit destroyed    "+label);
                }

                /*
                 * label="rand: "+rand+" ("+IDList.Count+")";
                 * for(int nn=0; nn<IDList.Count; nn++){
                 *      label+="     "+IDList[nn];
                 * }
                 * Debug.Log(label);
                 */

                if (GameControlTB.battleEnded)
                {
                    yield break;
                }
            }
        }
        else if (GameControlTB.GetMoveOrder() == _MoveOrder.FixedRandom || GameControlTB.GetMoveOrder() == _MoveOrder.FixedStatsBased)
        {
            for (int i = 0; i < allUnit.Count; i++)
            {
                UnitTB unit = allUnit[i];

                //move the unit
                unitInAction = true;
                StartCoroutine(_MoveUnit(unit));

                yield return(null);

                //wait while the unit is in action (making its move)
                //and while the game event is in progress (counter attack, death effect/animation, etc.)
                while (unit.InAction() || GameControlTB.IsActionInProgress() || unitInAction)
                {
                    yield return(null);
                }

                if (GameControlTB.battleEnded)
                {
                    yield break;
                }
            }
        }

        faction.allUnitMoved = true;

        yield return(new WaitForSeconds(0.5f));

        EndTurn();
    }