コード例 #1
0
        private void Awake()
        {
            InitBattleActions();

            unit0 = new CombatUnit(0);
            unit1 = new CombatUnit(1);
            steps = new List <CombatStep>();
        }
コード例 #2
0
        /// <summary>
        /// 运行动画,并返回长度
        /// </summary>
        /// <param name="combat"></param>
        /// <param name="actor"></param>
        /// <param name="other"></param>
        /// <param name="dirs"></param>
        /// <returns></returns>
        protected virtual float RunAniamAndGetLengthInMap(CombatVariable actor, CombatVariable other, Direction[] dirs)
        {
            CombatUnit actorUnit = combat.GetCombatUnit(actor.position);

            if (actorUnit == null || actorUnit.mapClass == null)
            {
                return(0f);
            }

            ClassAnimatorController actorAnima = actorUnit.mapClass.animatorController;
            Direction dir    = dirs[actor.position];
            float     length = 0.5f;

            switch (actor.animaType)
            {
            case CombatAnimaType.Prepare:
                actorAnima.PlayPrepareAttack(dir, actorUnit.weaponType);
                break;

            case CombatAnimaType.Attack:
            case CombatAnimaType.Heal:
                actorAnima.PlayAttack();
                length = actorAnima.GetAttackAnimationLength(dir, actorUnit.weaponType);
                break;

            case CombatAnimaType.Evade:
                actorAnima.PlayEvade();
                length = actorAnima.GetEvadeAnimationLength(dir);
                break;

            case CombatAnimaType.Damage:
                actorAnima.PlayDamage();
                length = actorAnima.GetDamageAnimationLength(dir);

                // TODO 受到爆击的额外动画,假定是晃动
                // if (other.crit)
                // {
                //     CommonAnima.PlayShake(actorUnit.mapClass.gameObject);
                //     length = Mathf.Max(length, CommonAnima.shakeLength);
                // }

                break;

            case CombatAnimaType.Dead:
                // TODO 播放死亡动画,我把死亡忘记了
                break;

            default:
                break;
            }
            return(length);
        }
コード例 #3
0
        public override CombatStep CalcBattle(Combat combat, CombatVariable atkVal, CombatVariable defVal)
        {
            CombatUnit atker = combat.GetCombatUnit(0);
            CombatUnit defer = combat.GetCombatUnit(1);

            // 防守者是否可反击
            bool canDeferAtk = false;

            if (defer.role.equipedWeapon != null)
            {
                Vector3Int       offset  = defer.mapClass.cellPosition - atker.mapClass.cellPosition;
                int              dist    = Mathf.Abs(offset.x) + Mathf.Abs(offset.y);
                WeaponUniqueInfo defInfo = defer.role.equipedWeapon.uniqueInfo;

                //if (defInfo.weaponType != WeaponType.Staff)
                {
                    // 如果在反击范围内
                    if (dist >= defInfo.minRange && dist <= defInfo.maxRange)
                    {
                        canDeferAtk = true;
                    }
                }
            }

            // 根据速度初始化攻击者与防守者
            if (canDeferAtk)
            {
                if (atker.speed < defer.speed)
                {
                    CombatUnit tmp = atker;
                    atker = defer;
                    defer = tmp;
                }
            }

            // 更新信息
            this.message = "战斗开始";

            atkVal = new CombatVariable(atker.position, atker.hp, atker.mp, true, atker.durability, CombatAnimaType.Prepare);
            defVal = new CombatVariable(defer.position, defer.hp, defer.mp, canDeferAtk, defer.durability, CombatAnimaType.Prepare);

            // 准备阶段
            CombatStep firstStep = new CombatStep(atkVal, defVal);

            return(firstStep);
        }
コード例 #4
0
        /// <summary>
        /// 计算战斗数据
        /// </summary>
        private void CalcBattle(CombatVariable atkVal, CombatVariable defVal)
        {
            CombatUnit       atker      = GetCombatUnit(atkVal.position);
            BattleActionType actionType = GetBattleActionType(atker.weaponType);
            BattleAction     action;

            if (!m_BattleActionDict.TryGetValue(actionType, out action))
            {
                Debug.LogErrorFormat(
                    "Combat -> StartBattle: BattleActionType.{0} is not found, check the code.",
                    actionType.ToString());
                return;
            }


            CombatStep step = action.CalcBattle(this, atkVal, defVal);

            // step.message = action.message;
            steps.Add(step);

            // 如果战斗没有结束,交换攻击者与防守者
            if (!action.IsBattleEnd(this, step.atkVal, step.defVal))
            {
                if (step.defVal.canAtk)
                {
                    CalcBattle(step.defVal, step.atkVal);
                }
                else
                {
                    // 如果防守方不可反击
                    defVal        = step.defVal;
                    defVal.action = true;
                    if (!action.IsBattleEnd(this, defVal, step.atkVal))
                    {
                        CalcBattle(step.atkVal, defVal);
                    }
                }
            }
            else
            {
                // TODO 如果死亡,播放死亡动画(我把死亡动画忘记了)
                // if (step.defVal.isDead) 播放死亡动画
            }
        }
コード例 #5
0
        private IEnumerator RunningAnimasInMap()
        {
            CombatUnit        unit0 = combat.GetCombatUnit(0);
            CombatUnit        unit1 = combat.GetCombatUnit(1);
            List <CombatStep> steps = combat.steps;

            Direction[] dirs = new Direction[2];
            dirs[0] = GetAnimaDirectionInMap(unit0.mapClass.cellPosition, unit1.mapClass.cellPosition);
            dirs[1] = GetAnimaDirectionInMap(unit1.mapClass.cellPosition, unit0.mapClass.cellPosition);

            yield return(null);

            int        curIndex = 0;
            CombatStep step;

            while (curIndex < steps.Count)
            {
                step = steps[curIndex];

                // 根据动画不同,播放时间应该是不同的
                // 这需要一些参数或者算法来控制
                // (例如一些魔法,在配置表中加上一个特效的变量,
                // 人物施法动画是这个,特效还要另算,需要计算在内)
                // 这里我只是简单的定义为同时播放

                float len0 = RunAniamAndGetLengthInMap(step.atkVal, step.defVal, dirs);
                float len1 = RunAniamAndGetLengthInMap(step.defVal, step.atkVal, dirs);
                float wait = Mathf.Max(len0, len1);

                onStep.Invoke(this, curIndex, wait, false);
                yield return(new WaitForSeconds(wait));

                onStep.Invoke(this, curIndex, animationInterval, true);
                yield return(new WaitForSeconds(animationInterval));

                curIndex++;
            }
        }
コード例 #6
0
        public override CombatStep CalcBattle(Combat combat, CombatVariable atkVal, CombatVariable defVal)
        {
            CombatUnit atker = combat.GetCombatUnit(atkVal.position);
            CombatUnit defer = combat.GetCombatUnit(defVal.position);

            atkVal.animaType = CombatAnimaType.Attack;

            // 真实命中率 = 攻击者命中 - 防守者回避
            int realHit = atker.hit - defer.avoidance;

            // 概率是否击中
            int  hitRate = UnityEngine.Random.Range(0, 100);
            bool isHit   = hitRate <= realHit;

            if (isHit)
            {
                bool crit    = false; // TODO 是否爆击
                int  realAtk = atker.atk;

                ///////////////////
                // TODO 触发伤害技能
                // 这里写触发技能后伤害变化(比如武器特效等),
                // 或者触发某些状态(比如中毒等)
                //////////////////

                if (crit)
                {
                    realAtk *= 2; // 假定爆击造成双倍伤害
                }

                // 掉血 = 攻击者攻击力 - 防守者防御力
                // 最少掉一滴血
                int damageHp = Mathf.Max(1, realAtk - defer.def);
                if (damageHp > defVal.hp)
                {
                    damageHp = defVal.hp;
                }
                defVal.hp = defVal.hp - damageHp;

                atkVal.crit      = crit;
                defVal.animaType = CombatAnimaType.Damage;

                // 更新此次攻击信息
                this.message = string.Format(
                    "{0} 对 {1} 的攻击造成了 {2} 点伤害{3}。",
                    atker.role.character.info.name,
                    defer.role.character.info.name,
                    damageHp,
                    crit ? "(爆击)" : string.Empty);

                if (defVal.isDead)
                {
                    this.message += string.Format(" {0}被击败了。", defer.role.character.info.name);
                }
            }
            else
            {
                defVal.animaType = CombatAnimaType.Evade;

                // 更新此次躲闪信息
                this.message = string.Format(
                    "{1} 躲闪了 {0} 的攻击。",
                    atker.role.character.info.name,
                    defer.role.character.info.name);
            }

            // 只有玩家才会减低耐久度
            if (atker.role.attitudeTowards == AttitudeTowards.Player)
            {
                // 攻击者武器耐久度-1
                atkVal.durability = Mathf.Max(0, atkVal.durability - 1);
            }

            // 攻击者行动过了
            atkVal.action = true;

            CombatStep step = new CombatStep(atkVal, defVal);

            return(step);
        }