Пример #1
0
        //开始战斗
        private void Fight()
        {
            battleState = BattleState.Fighting;

            Debug.Log(string.Format("<color=#ff0000> {0} battle fight. </color>", this.ToString()));

            List <BattleAction> actions = new List <BattleAction>();

            BattleUnit actionUnit = null;

            do
            {
                //没有连接渲染器,则一步一更新
                //连接了渲染器,则一直更新直到结束
                actionUnit = actionQueue.Dequeue();

                if (actionUnit == null)
                {
                    battleState = BattleState.End;
                    break;
                }

                if (actionUnit.CanAction)
                {
                    HeroActionState state = actionUnit.BattleAction(actions);

                    switch (state)
                    {
                    case HeroActionState.BattleEnd:
                        battleState = BattleState.End;
                        break;

                    case HeroActionState.Error:
                        battleState = BattleState.Exception;
                        break;

                    case HeroActionState.Warn:
                        UtilityHelper.LogWarning(string.Format("Warning: battle action state warning -> {0}", actionUnit.ToString()));
                        break;

                    case HeroActionState.WaitForPlayerChoose:
                        battleState = BattleState.WaitForPlayer;
                        break;

                    default:
                        break;
                    }

                    //追加动作
                    AppendBattleActions(actions);
                    //考虑是否在放回队列
                    CalculateNextAction(actionUnit);
                }

                ++actionCount;

                if (actionCount >= EGameConstL.BattleFieldMaxActions)
                {
                    battleState = BattleState.Exception;
                }
            } while (battleFieldRenderer == null &&
                     battleState != BattleState.End &&
                     battleState != BattleState.Exception);

            //连接了渲染器,一步一更新
            if (battleFieldRenderer != null)
            {
                Debug.Log("Play actions");

                if (battleState == BattleState.End || battleState == BattleState.Exception)
                {
                    battleFieldRenderer.PlayBattle(BattleEnd);
                }

                else if (battleState == BattleState.WaitForPlayer)
                {
                    battleFieldRenderer.PlayBattle(null);
                }

                else
                {
                    battleFieldRenderer.PlayBattle(Fight);
                }
            }
            else
            {
                //没有连接渲染器,自动战斗
                BattleEnd();
            }
        }
Пример #2
0
        //一次完成战斗计算,不一定需要记录过程
        public int Run(bool recordProcess)
        {
            BattleUnit          actionUnit  = null;
            List <BattleAction> heroActions = null;

            if (recordProcess)
            {
                heroActions = new List <BattleAction>();
            }

            //先进入战场
            EnterBattleField(recordProcess);

            //开始战斗
            int  maxRound  = 999;
            int  round     = 0;
            bool battleEnd = false;

            while (!battleEnd && round++ < maxRound)
            {
                //按照先后顺序
                for (int i = 0; i < teams.Count; ++i)
                {
                    //战斗结束
                    if (battleEnd)
                    {
                        break;
                    }

                    for (int j = 0; j < teams[i].battleUnits.Count; ++j)
                    {
                        //战斗结束
                        if (battleEnd)
                        {
                            break;
                        }

                        actionUnit = teams[i].battleUnits[j];
                        if (actionUnit.CanAction)
                        {
                            HeroActionState state = actionUnit.BattleAction(heroActions);
                            AppendBattleActions(heroActions.ToArray());
                            switch (state)
                            {
                            case HeroActionState.BattleEnd:
                                battleEnd = true;
                                break;

                            case HeroActionState.Warn:
                                break;

                            case HeroActionState.Error:
                                UtilityHelper.LogError("Aciont error.");
                                break;

                            default:
                                break;
                            }
                        }
                    }
                }
            }

            //生成战斗结果
            GenerateBattleResult();

            //判断是否记录过程
            if (!recordProcess)
            {
                CleanBattleAction();
            }

            return(BattleState);
        }
Пример #3
0
        //开始战斗
        private void Fight()
        {
            battleState = BattleState.Fighting;

            BattleUnit actionUnit = null;

            do
            {
                //连接渲染器,则一步一更新
                //没有连接渲染器,则一直计算直到结束
                actionUnit = actionQueue.Dequeue();

                if (actionUnit == null)
                {
                    battleState = BattleState.End;
                    break;
                }

                if (actionUnit.CanAction)
                {
                    BattleUnitAction battleUnitAction = BattleUnitAction.Create(actionUnit);

                    HeroActionState state = actionUnit.BattleAction();

                    switch (state)
                    {
                    case HeroActionState.Normal:
                        battleState = BattleState.Fighting;
                        break;

                    case HeroActionState.WaitForPlayerChoose:
                        battleState = BattleState.WaitForPlayer;
                        break;

                    case HeroActionState.Error:
                        battleState = BattleState.Exception;
                        break;

                    case HeroActionState.Warn:
                        battleState = BattleState.Exception;
                        UtilityHelper.LogWarning(string.Format("Warning: battle action state warning -> {0}", actionUnit.ToString()));
                        break;

                    default:
                        break;
                    }
                }

                //考虑是否在放回队列
                CalculateNextAction(actionUnit);

                if (battleActions.Count > EGameConstL.BattleFieldMaxActions)
                {
                    UtilityHelper.LogError("Battle actions overflow max limit.");
                    battleState = BattleState.Exception;
                }
                else
                {
                    //只在这种情况下做战斗结束的判断
                    if (!actionUnit.CanAction ||
                        actionUnit.targetBattleUnit == null ||
                        !actionUnit.targetBattleUnit.CanAction)
                    {
                        CheckBattleEnd();
                    }
                }
            } while (battleFieldRenderer == null &&
                     battleState != BattleState.End &&
                     battleState != BattleState.Exception);

            //连接了渲染器,一步一表现
            if (battleFieldRenderer != null)
            {
                if (battleState == BattleState.WaitForPlayer)
                {
                    battleFieldRenderer.PlayBattle(null);
                }

                else
                {
                    battleFieldRenderer.PlayBattle(Run);
                }
            }
            else
            {
                Run();
            }
        }
Пример #4
0
        //协同计算战斗,必记录过程
        public IEnumerator Run()
        {
            BattleUnit          actionUnit  = null;
            List <BattleAction> heroActions = new List <BattleAction>();

            //先进入战场
            EnterBattleField(true);

            //开始战斗
            int  maxRound  = 999;
            int  round     = 0;
            bool battleEnd = false;

            while (!battleEnd && round++ < maxRound)
            {
                //按照先后顺序
                for (int i = 0; i < teams.Count; ++i)
                {
                    //战斗结束
                    if (battleEnd)
                    {
                        break;
                    }

                    for (int j = 0; j < teams[i].battleUnits.Count; ++j)
                    {
                        //战斗结束
                        if (battleEnd)
                        {
                            break;
                        }

                        actionUnit = teams[i].battleUnits[j];
                        if (actionUnit.CanAction)
                        {
                            HeroActionState state = actionUnit.BattleAction(heroActions);
                            AppendBattleActions(heroActions.ToArray());
                            switch (state)
                            {
                            case HeroActionState.Normal:
                                //正常
                                break;

                            case HeroActionState.WaitForPlayerChoose:
                                yield return(EGameConstL.WaitForTouchScreen);

                                break;

                            case HeroActionState.BattleEnd:
                                battleEnd = true;
                                break;

                            case HeroActionState.Warn:
                                break;

                            case HeroActionState.Error:
                                UtilityHelper.LogError("Aciont error.");
                                break;

                            default:
                                UtilityHelper.LogError("Unknow hero action state..." + state);
                                break;
                            }
                        }
                    }
                }
            }

            if (round >= maxRound)
            {
                UtilityHelper.LogError(string.Format("Battle round greater than {0} -> {1}", maxRound, battleID));
            }

            //生成战斗结果
            GenerateBattleResult();
        }