示例#1
0
    // Update is called once per frame
    void Update()
    {
        if (WarSceneController.isGamePause)
        {
            return;
        }

        if (WarSceneController.state == WarSceneController.State.Beginning ||
            WarSceneController.state == WarSceneController.State.Menu ||
            WarSceneController.state == WarSceneController.State.End ||
            WarSceneController.state == WarSceneController.State.Assault)
        {
            return;
        }

        if (state == State.Dead)
        {
            OnDyingHandler();
            return;
        }

        if (isMagicLock)
        {
            timeCoolDownTick -= Time.deltaTime;
            if (timeCoolDownTick <= 0)
            {
                isMagicLock      = false;
                timeCoolDownTick = 0;
                anim.Resume();
            }
            return;
        }

        if (isParrying)
        {
            OnParryHandler();
        }

        if (isFighting)
        {
            OnFightingAct();
            return;
        }

        if (timeCoolDownTick > 0)
        {
            timeCoolDownTick -= Time.deltaTime;
            if (timeCoolDownTick <= 0)
            {
                timeCoolDownTick = 0;
            }
            else
            {
                return;
            }
        }

        if (isFighting || isParrying)
        {
            return;
        }

        switch (state)
        {
        case State.Idle:
        {
            if (!anim.IsPlaying("Idle"))
            {
                anim.Play("Idle");
            }

            if (CheckCanFight())
            {
                return;
            }

            RangeSolidersFightCheck();
        }
        break;

        case State.Running:
        {
            if (!anim.IsPlaying("Run"))
            {
                anim.Play("Run");
            }

            if (WarSceneController.state == WarSceneController.State.Dead)
            {
                SetRandomRun(transform.localPosition, false);
                return;
            }

            if (targetEnemy != null &&
                ((side == WarSceneController.WhichSide.Left && transform.localPosition.x - targetEnemy.localPosition.x > -100) ||
                 side == WarSceneController.WhichSide.Right && transform.localPosition.x - targetEnemy.localPosition.x < 100))
            {
                Vector3 posBK     = transform.localPosition;
                Vector3 targetPos = Vector3.zero;

                if (transform.localPosition.x > targetEnemy.localPosition.x)
                {
                    targetPos = new Vector3(targetEnemy.localPosition.x + hitDistance - locationStepX,
                                            targetEnemy.localPosition.y, targetEnemy.localPosition.z);
                }
                else
                {
                    targetPos = new Vector3(targetEnemy.localPosition.x - hitDistance + locationStepX,
                                            targetEnemy.localPosition.y, targetEnemy.localPosition.z);
                }

                int x = (int)((targetPos.x + manPosMaxX) / locationStepX);
                int y = (int)((targetPos.y + manPosMaxY) / locationStepY);
                if (warCtrl.GetLocationFlag(x, y) != 0 && warCtrl.GetLocationFlag(x, y) != index)
                {
                    SetRandomRun(targetPos, false);
                    return;
                }

                transform.localPosition = Vector3.MoveTowards(transform.localPosition,
                                                              targetPos, runSpeed * Time.deltaTime);

                if ((transform.localPosition.x > posBK.x && transform.localScale.x > 0) ||
                    (transform.localPosition.x < posBK.x && transform.localScale.x < 0))
                {
                    transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
                }

                if (transform.localPosition == targetPos)
                {
                    if (isEnemyGeneral == false ||
                        (side == WarSceneController.WhichSide.Left && (transform.localPosition.x > targetEnemy.localPosition.x ||
                                                                       (transform.localPosition.x < targetEnemy.localPosition.x &&
                                                                        Vector3.Distance(warCtrl.leftGeneral.transform.localPosition, warCtrl.rightGeneral.transform.localPosition) > 200))) ||
                        (side == WarSceneController.WhichSide.Right && (transform.localPosition.x < targetEnemy.localPosition.x ||
                                                                        (transform.localPosition.x > targetEnemy.localPosition.x &&
                                                                         Vector3.Distance(warCtrl.leftGeneral.transform.localPosition, warCtrl.rightGeneral.transform.localPosition) > 200))))
                    {
                        CheckCanFight();
                    }
                    else
                    {
                        SetRandomRun(targetPos, false);
                    }
                }
            }
            else
            {
                Vector3 pos = transform.localPosition;

                if (side == WarSceneController.WhichSide.Left)
                {
                    pos = new Vector3(pos.x + runSpeed * Time.deltaTime, pos.y, pos.z);
                    if (transform.localScale.x > 0)
                    {
                        transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
                    }
                }
                else
                {
                    pos = new Vector3(pos.x - runSpeed * Time.deltaTime, pos.y, pos.z);
                    if (transform.localScale.x < 0)
                    {
                        transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
                    }
                }

                pos.x = Mathf.Clamp(pos.x, -WarSceneController.manPosMaxX + 100, WarSceneController.manPosMaxX - 100);
                transform.localPosition = pos;
            }

            RangeSolidersFightCheck();
        }
        break;

        case State.RandomRun:
        {
            if (!anim.IsPlaying("Run"))
            {
                anim.Play("Run");
            }

            transform.localPosition = Vector3.MoveTowards(transform.localPosition,
                                                          targetPosition, runSpeed * Time.deltaTime);
            if (transform.localPosition == targetPosition)
            {
                if (!isOnRandomRun)
                {
                    SetRun();
                }
                else
                {
                    SetRandomRun(randomRunPos, true);
                }
            }
        }
        break;

        case State.MovingBack:
        {
            if (!anim.IsPlaying("Run"))
            {
                anim.Play("Run");
            }

            Vector3 pos = transform.localPosition;
            if (side == WarSceneController.WhichSide.Left)
            {
                pos = new Vector3(pos.x - runSpeed * Time.deltaTime, pos.y, pos.z);
            }
            else
            {
                pos = new Vector3(pos.x + runSpeed * Time.deltaTime, pos.y, pos.z);
            }

            pos.x = Mathf.Clamp(pos.x, -WarSceneController.manPosMaxX + 100, WarSceneController.manPosMaxX - 100);
            transform.localPosition = pos;
        }
        break;

        case State.Escape:
            if (!anim.IsPlaying("Run"))
            {
                anim.Play("Run");
            }

            if (side == WarSceneController.WhichSide.Left)
            {
                transform.localPosition = new Vector3(transform.localPosition.x - runSpeed * Time.deltaTime,
                                                      transform.localPosition.y, transform.localPosition.z);

                if (transform.localPosition.x <= -WarSceneController.manPosMaxX + WarSceneController.locationStepX)
                {
                    Destroy(gameObject);
                    Destroy(mapPoint.gameObject);
                }
                else if (transform.localPosition.x <= -WarSceneController.manPosMaxX + 100)
                {
                    float transparent = (transform.localPosition.x + WarSceneController.manPosMaxX) / 100f;
                    anim.GetComponent <exSprite>().color = new Color(1, 1, 1, transparent);
                }
            }
            else
            {
                transform.localPosition = new Vector3(transform.localPosition.x + runSpeed * Time.deltaTime,
                                                      transform.localPosition.y, transform.localPosition.z);

                if (transform.localPosition.x >= WarSceneController.manPosMaxX - WarSceneController.locationStepX)
                {
                    Destroy(gameObject);
                    Destroy(mapPoint.gameObject);
                }
                else if (transform.localPosition.x >= WarSceneController.manPosMaxX - 100)
                {
                    float transparent = (WarSceneController.manPosMaxX - transform.localPosition.x) / 100f;
                    anim.GetComponent <exSprite>().color = new Color(1, 1, 1, transparent);
                }
            }
            break;
        }

        CheckLocationState();
    }
示例#2
0
    void CheckLocationState()
    {
        locationX = warCtrl.GetLocationPositionX(transform.localPosition.x);
        locationY = warCtrl.GetLocationPositionY(transform.localPosition.y);

        SetMapLocation();

        if (state == State.Magic ||
            WarSceneController.state == WarSceneController.State.Dead ||
            WarSceneController.state == WarSceneController.State.End)
        {
            return;
        }

        isStopped = false;
        bool flag = false;

        if (Random.Range(0, 100) > 50)
        {
            flag = true;
            for (int i = locationX + 1; i <= locationX + (int)(hitDistance / locationStepX); i++)
            {
                if (i >= manPosMaxX * 2 / locationStepX)
                {
                    continue;
                }
                if (warCtrl.GetLocationFlag(i, locationY) != 0)
                {
                    if (side == WarSceneController.WhichSide.Left)
                    {
                        if (warCtrl.GetLocationSide(i, locationY) == side)
                        {
                            if (state != State.Escape && i == locationX + 1)
                            {
                                isStopped = true;
                            }
                        }
                        else
                        {
                            if (state != State.Escape)
                            {
                                SetAttackFront();
                            }
                            break;
                        }
                    }
                    else
                    {
                        if (warCtrl.GetLocationSide(i, locationY) != side)
                        {
                            if (state != State.Escape)
                            {
                                SetAttackBack();
                            }
                            else
                            {
                                SetAttackFront();
                            }
                        }
                        else
                        {
                            if (state == State.Escape && i == locationX + 1)
                            {
                                isStopped = true;
                            }
                        }
                    }
                }
            }
        }

        if (isFighting)
        {
            return;
        }

        for (int i = locationX - (int)(hitDistance / locationStepX); i < locationX; i++)
        {
            if (i <= 0)
            {
                continue;
            }
            if (warCtrl.GetLocationFlag(i, locationY) != 0)
            {
                if (side == WarSceneController.WhichSide.Right)
                {
                    if (warCtrl.GetLocationSide(i, locationY) == side)
                    {
                        if (state != State.Escape && i == locationX - 1)
                        {
                            isStopped = true;
                        }
                    }
                    else
                    {
                        if (state != State.Escape)
                        {
                            SetAttackFront();
                        }
                        break;
                    }
                }
                else
                {
                    if (warCtrl.GetLocationSide(i, locationY) != side)
                    {
                        if (state != State.Escape)
                        {
                            SetAttackBack();
                        }
                        else
                        {
                            SetAttackFront();
                        }
                    }
                    else
                    {
                        if (state == State.Escape && i == locationX - 1)
                        {
                            isStopped = true;
                        }
                    }
                }
            }
        }

        if (isFighting)
        {
            return;
        }

        if (!flag)
        {
            for (int i = locationX + 1; i <= locationX + (int)(hitDistance / locationStepX); i++)
            {
                if (i >= manPosMaxX * 2 / locationStepX)
                {
                    continue;
                }
                if (warCtrl.GetLocationFlag(i, locationY) != 0)
                {
                    if (side == WarSceneController.WhichSide.Left)
                    {
                        if (warCtrl.GetLocationSide(i, locationY) == side)
                        {
                            if (state != State.Escape && i == locationX + 1)
                            {
                                isStopped = true;
                            }
                        }
                        else
                        {
                            if (state != State.Escape)
                            {
                                SetAttackFront();
                            }
                            break;
                        }
                    }
                    else
                    {
                        if (warCtrl.GetLocationSide(i, locationY) != side)
                        {
                            if (state != State.Escape)
                            {
                                SetAttackBack();
                            }
                            else
                            {
                                SetAttackFront();
                            }
                        }
                        else
                        {
                            if (state == State.Escape && i == locationX + 1)
                            {
                                isStopped = true;
                            }
                        }
                    }
                }
            }
        }
    }