Beispiel #1
0
        /// <summary>
        /// 调整位置后开始战斗
        /// </summary>
        /// <returns>The position and start fight.</returns>
        /// <param name="battlePlayerCtr">Battle player ctr.</param>
        private IEnumerator ResetPositionAndStartFight(BattlePlayerController battlePlayerCtr)
        {
            // 等待准备好
            yield return(new WaitUntil(() => isReadyToFight));

            // 头山闪红色叹号
            AlertTintSpark();
            // 播放idle动画
            baCtr.PlayRoleAnim(CommonData.roleIdleAnimName, 0, null);
            // 判断玩家角色是否需要重新开始攻击动作
            bool playerNeedResetAttack = battlePlayerCtr.NeedResetAttack();

            yield return(new WaitForSeconds(0.4f));

            HideAllAlertAreas();
            DisableAllDetect();

            Vector3 playerOriPos  = battlePlayerCtr.transform.position;
            Vector3 monsterOriPos = transform.position;

            int playerPosX  = Mathf.RoundToInt(playerOriPos.x);
            int playerPosY  = Mathf.RoundToInt(playerOriPos.y);
            int monsterPosX = Mathf.RoundToInt(monsterOriPos.x);
            int monsterPosY = Mathf.RoundToInt(monsterOriPos.y);

            int monsterLayerOrder = -monsterPosY;

            int posOffsetX = playerPosX - monsterPosX;
            int posOffsetY = playerPosY - monsterPosY;

            Vector3 monsterRunPos   = Vector3.zero;
            Vector3 monsterFightPos = Vector3.zero;
            Vector3 playerFightPos  = new Vector3(playerPosX, playerPosY, 0);

            int minX = 0;
            int maxX = ExploreManager.Instance.newMapGenerator.columns - 1;

            HLHRoleAnimInfo playerCurrentAnimInfo = battlePlayerCtr.GetCurrentRoleAnimInfo();



            // 根据玩家角色和怪物的位置,战斗点附近的可行走情况决定人物和怪物的战斗位置
            //人物在怪物右边
            if (posOffsetX > 0)
            {
                // 人物角色先朝左
                battlePlayerCtr.TowardsLeft(!battlePlayerCtr.isInFight);
                // 如果怪物在人物水平向左一格的附近位置,并且该位置没有超出地图左侧界限
                if (playerPosX - 1 >= minX && playerPosX - 1 == monsterPosX && playerPosY == monsterPosY)
                {
                    // 怪物跑向的位置【人物左边一格】
                    monsterRunPos = new Vector3(playerPosX - 1f, playerPosY, 0);
                    // 怪物战斗位置【人物左边一格】
                    monsterFightPos = new Vector3(playerPosX - 1, playerPosY, 0);
                    // 怪物层级调整到人物层级
                    monsterLayerOrder = -playerPosY;
                }
                // 人物左侧没有超过地图左侧界限,且地图上人物左侧位置上没有东西【可行走信息为1】
                else if (playerPosX - 1 >= minX && ExploreManager.Instance.newMapGenerator.mapWalkableInfoArray[playerPosX - 1, playerPosY] == 1)
                {
                    monsterRunPos     = new Vector3(playerPosX - 0.5f, playerPosY, 0);
                    monsterFightPos   = new Vector3(playerPosX - 1, playerPosY, 0);
                    monsterLayerOrder = -playerPosY;
                }
                // 人物左侧没有超过地图左侧界限,地图上人物左侧位置没有东西,但是可行走信息不为1,但是是当前怪物的单步行走目标位置【每个怪物在行走之前都会把目标位置可行走信息设为5,防止其他怪物也走上去】
                else if (playerPosX - 1 >= minX && playerPosX - 1 == Mathf.RoundToInt(moveDestination.x) && playerPosY == Mathf.RoundToInt(moveDestination.y))
                {
                    monsterRunPos     = new Vector3(playerPosX - 1f, playerPosY, 0);
                    monsterFightPos   = new Vector3(playerPosX - 1, playerPosY, 0);
                    monsterLayerOrder = -playerPosY;
                }
                // 其他情况都要让怪物和人物的战斗位置尽量放进同一个格子里了
                else
                {
                    // 如果人物靠上
                    if (posOffsetY > 0)
                    {
                        //怪物跑到的位置
                        monsterRunPos = new Vector3(playerPosX - 0.25f, playerPosY - 0.15f, 0);
                        // 怪物战斗位置
                        monsterFightPos = new Vector3(playerPosX - 0.25f, playerPosY - 0.15f, 0);
                        // 怪物的层级比人物高1级
                        monsterLayerOrder = -playerPosY + 1;
                        // 人物的战斗位置
                        playerFightPos = new Vector3(playerPosX + 0.25f, playerPosY + 0.15f, 0);

                        baCtr.SetSortingOrder(monsterLayerOrder);
                    }
                    else                    // 如果人物靠下
                    {
                        monsterRunPos     = new Vector3(playerPosX - 0.25f, playerPosY + 0.15f, 0);
                        monsterFightPos   = new Vector3(playerPosX - 0.25f, playerPosY + 0.15f, 0);
                        monsterLayerOrder = -playerPosY - 1;

                        playerFightPos = new Vector3(playerPosX + 0.25f, playerPosY - 0.15f, 0);

                        baCtr.SetSortingOrder(-playerPosY - 1);
                    }
                }
            }
            else if (posOffsetX == 0)
            {
                if (playerPosX + 1 <= maxX && ExploreManager.Instance.newMapGenerator.mapWalkableInfoArray [playerPosX + 1, playerPosY] == 1)
                {
                    battlePlayerCtr.TowardsRight(!battlePlayerCtr.isInFight);

                    monsterRunPos = new Vector3(playerPosX + 0.5f, playerPosY, 0);

                    monsterFightPos = new Vector3(playerPosX + 1, playerPosY, 0);

                    monsterLayerOrder = -playerPosY;
                }
                else if (playerPosX + 1 <= maxX && playerPosX + 1 == Mathf.RoundToInt(moveDestination.x) && playerPosY == Mathf.RoundToInt(moveDestination.y))
                {
                    battlePlayerCtr.TowardsRight(!battlePlayerCtr.isInFight);

                    monsterRunPos = new Vector3(playerPosX + 0.5f, playerPosY, 0);

                    monsterFightPos = new Vector3(playerPosX + 1, playerPosY, 0);

                    monsterLayerOrder = -playerPosY;
                }
                else if (playerPosX - 1 >= minX && ExploreManager.Instance.newMapGenerator.mapWalkableInfoArray [playerPosX - 1, playerPosY] == 1)
                {
                    battlePlayerCtr.TowardsLeft(!battlePlayerCtr.isInFight);

                    monsterRunPos = new Vector3(playerPosX - 0.5f, playerPosY, 0);

                    monsterFightPos = new Vector3(playerPosX - 1, playerPosY, 0);

                    monsterLayerOrder = -playerPosY;
                }
                else if (playerPosX - 1 >= minX && playerPosX - 1 == Mathf.RoundToInt(moveDestination.x) && playerPosY == Mathf.RoundToInt(moveDestination.y))
                {
                    battlePlayerCtr.TowardsLeft(!battlePlayerCtr.isInFight);

                    monsterRunPos = new Vector3(playerPosX - 0.5f, playerPosY, 0);

                    monsterFightPos = new Vector3(playerPosX - 1, playerPosY, 0);

                    monsterLayerOrder = -playerPosY;
                }
                else
                {
                    if (posOffsetY > 0)
                    {
                        monsterRunPos = new Vector3(playerPosX + 0.25f, playerPosY - 0.15f, 0);

                        monsterFightPos = new Vector3(playerPosX + 0.25f, playerPosY - 0.15f, 0);

                        monsterLayerOrder = -playerPosY + 1;

                        playerFightPos = new Vector3(playerPosX - 0.25f, playerPosY + 0.15f, 0);

                        baCtr.SetSortingOrder(-playerPosY + 1);

                        battlePlayerCtr.TowardsRight(!battlePlayerCtr.isInFight);
                    }
                    else
                    {
                        monsterRunPos = new Vector3(playerPosX + 0.25f, playerPosY + 0.15f, 0);

                        monsterFightPos = new Vector3(playerPosX + 0.25f, playerPosY + 0.15f, 0);

                        monsterLayerOrder = -playerPosY - 1;

                        playerFightPos = new Vector3(playerPosX - 0.25f, playerPosY - 0.15f, 0);

                        baCtr.SetSortingOrder(-playerPosY - 1);

                        battlePlayerCtr.TowardsRight(!battlePlayerCtr.isInFight);
                    }
                }
            }
            else if (posOffsetX < 0)
            {
                battlePlayerCtr.TowardsRight(!battlePlayerCtr.isInFight);

                if (playerPosX + 1 <= maxX && playerPosX + 1 == monsterPosX && playerPosY == monsterPosY)
                {
                    monsterRunPos     = new Vector3(playerPosX + 1f, playerPosY, 0);
                    monsterFightPos   = new Vector3(playerPosX + 1, playerPosY, 0);
                    monsterLayerOrder = -playerPosY;
                }
                else if (playerPosX + 1 <= maxX && ExploreManager.Instance.newMapGenerator.mapWalkableInfoArray[playerPosX + 1, playerPosY] == 1)
                {
                    monsterRunPos     = new Vector3(playerPosX + 0.5f, playerPosY, 0);
                    monsterFightPos   = new Vector3(playerPosX + 1, playerPosY, 0);
                    monsterLayerOrder = -playerPosY;
                }
                else if (playerPosX + 1 <= maxX && playerPosX + 1 == Mathf.RoundToInt(moveDestination.x) && playerPosY == Mathf.RoundToInt(moveDestination.y))
                {
                    monsterRunPos     = new Vector3(playerPosX + 1f, playerPosY, 0);
                    monsterFightPos   = new Vector3(playerPosX + 1, playerPosY, 0);
                    monsterLayerOrder = -playerPosY;
                }
                else
                {
                    if (posOffsetY > 0)
                    {
                        monsterRunPos   = new Vector3(playerPosX + 0.25f, playerPosY - 0.15f, 0);
                        monsterFightPos = new Vector3(playerPosX + 0.25f, playerPosY - 0.15f, 0);

                        monsterLayerOrder = -playerPosY + 1;

                        playerFightPos = new Vector3(playerPosX - 0.25f, playerPosY + 0.15f, 0);

                        baCtr.SetSortingOrder(-playerPosY + 1);
                    }
                    else
                    {
                        monsterRunPos   = new Vector3(playerPosX + 0.25f, playerPosY + 0.15f, 0);
                        monsterFightPos = new Vector3(playerPosX + 0.25f, playerPosY + 0.15f, 0);

                        monsterLayerOrder = -playerPosY - 1;

                        playerFightPos = new Vector3(playerPosX - 0.25f, playerPosY - 0.15f, 0);

                        baCtr.SetSortingOrder(-playerPosY - 1);
                    }
                }
            }

            // 如果玩家需要重置战斗动作
            if (playerNeedResetAttack)
            {
                battlePlayerCtr.ResetAttack(playerCurrentAnimInfo);
                ExploreManager.Instance.expUICtr.UpdateActiveSkillButtons();
            }

            // 玩家位置修正
            battlePlayerCtr.FixPosTo(playerFightPos, null);

            // 怪物跑到位置
            RunToPosition(monsterRunPos, delegate {
                // 如果跑到的时候已经死了,直接返回
                if (baCtr.isDead)
                {
                    return;
                }

                // 调整怪物位置到战斗位置
                baCtr.FixPosTo(monsterFightPos, null);

                // 如果玩家没有逃离战斗
                if (!battlePlayerCtr.escapeFromFight)
                {
                    // 根据双方位置关系重新调整朝向
                    if (transform.position.x <= ExploreManager.Instance.battlePlayerCtr.transform.position.x)
                    {
                        baCtr.TowardsRight();
                        boneTowards = MyTowards.Right;
                    }
                    else
                    {
                        baCtr.TowardsLeft();
                        boneTowards = MyTowards.Left;
                    }

                    // 如果玩家没有逃离战斗,并且玩家还没有进入战斗,则玩家和怪物都进入战斗
                    if (!battlePlayerCtr.isInEscaping && !battlePlayerCtr.isInFight)
                    {
                        ExploreManager.Instance.PlayerAndMonsterStartFight();
                    }
                    else
                    {
                        // 其他情况下只有怪物进入战斗
                        ExploreManager.Instance.MonsterStartFight();
                    }
                }
                // 玩家在怪物跑的过程中脱离了战斗【这个现在从动画播放时长和跑动的时长上看是不会实现的,但是逻辑先做着】
                else
                {
                    bool monsterDie = baCtr.agent.health <= 0;
                    RefreshWalkableInfoWhenQuit(monsterDie);
                    QuitFightAndDelayMove(5);
                    battlePlayerCtr.escapeFromFight = false;
                }
            }, monsterLayerOrder);
        }