void MovetoTarget(MeteorUnit target)
 {
     //if (pathIdx == -1 && !FreeCache.ContainsKey(followTarget) && Vector3.Distance(owner.transform.position, followTarget.mPos) > 40.0f)
     //{
     //    int FreeSlot = -1;
     //    targetPath = GameBattleEx.Instance.FindPath(owner.transform.position, followTarget, out FreeSlot);
     //    pathIdx = 0;
     //    targetPos = followTarget.transform.position;
     //}
     //tick = 0.0f;
     owner.FaceToTarget(target);
     if (Vector3.Distance(new Vector3(owner.mPos.x, 0, owner.mPos.z), new Vector3(target.mPos.x, 0, target.mPos.z)) <= 35)
     {
         owner.controller.Input.AIMove(0, 0);
         if (Status == EAIStatus.Kill)
         {
             SubStatus = EAISubStatus.KillGetTarget;
         }
         else if (Status == EAIStatus.Follow)
         {
             Status    = EAIStatus.Idle;
             SubStatus = EAISubStatus.Think;
         }
         return;
     }
     else
     {
         if (SubStatus == EAISubStatus.KillGetTarget)
         {
             SubStatus = EAISubStatus.KillGotoTarget;
         }
     }
     owner.controller.Input.AIMove(0, 1);
 }
    IEnumerator PatrolRotateToTarget(Vector3 vec)
    {
        WsGlobal.AddDebugLine(vec, vec - Vector3.up * 10, Color.red, "PatrolPoint", 20.0f);
        Vector3 diff = (vec - owner.mPos);

        diff.y = 0;
        float dot         = Vector3.Dot(new Vector3(-owner.transform.forward.x, 0, -owner.transform.forward.z).normalized, diff.normalized);
        float dot2        = Vector3.Dot(new Vector3(-owner.transform.right.x, 0, -owner.transform.right.z).normalized, diff.normalized);
        float angle       = Mathf.Abs(Mathf.Acos(dot) * Mathf.Rad2Deg);
        bool  rightRotate = dot2 < 0;
        float offset      = 0.0f;
        float offsetmax   = GetAngleBetween(vec);
        float timeTotal   = offsetmax / 75.0f;
        float timeTick    = 0.0f;

        while (true)
        {
            timeTick += Time.deltaTime;
            float yOffset = Mathf.Lerp(0, offsetmax, timeTick / timeTotal);
            owner.SetOrientation((rightRotate ? -1 : 1) * (yOffset - offset));
            offset = yOffset;
            if (timeTick > timeTotal)
            {
                owner.FaceToTarget(vec);
                if (owner.posMng.mActiveAction.Idx == CommonAction.WalkRight || owner.posMng.mActiveAction.Idx == CommonAction.WalkLeft)
                {
                    owner.posMng.ChangeAction(0, 0.1f);
                }
                break;
            }
            yield return(0);
        }
        PatrolRotateToTargetCoroutine = null;
        SubStatus = EAISubStatus.PatrolSubGotoTarget;
    }
 public void SetPatrolPath(List <int> path)
 {
     PatrolPath.Clear();
     for (int i = 0; i < path.Count; i++)
     {
         PatrolPath.Add(Global.GLevelItem.wayPoint[path[i]]);
     }
     //-1代表在当前角色所在位置
     curPatrolIndex = -1;
     SubStatus      = EAISubStatus.Patrol;
 }
    public void OnDamaged()
    {
        //模拟出招被其他敌方角色攻击打断
        if (PlayWeaponPoseCorout != null)
        {
            owner.StopCoroutine(PlayWeaponPoseCorout);
            PlayWeaponPoseCorout = null;
        }

        Status    = EAIStatus.Kill;
        SubStatus = EAISubStatus.KillOnHurt;
    }
 void OnHurt()
 {
     owner.controller.Input.ResetInput();
     if (owner.posMng.mActiveAction.Idx == CommonAction.Struggle || owner.posMng.mActiveAction.Idx == CommonAction.Struggle0)
     {
         if (struggleCoroutine == null)
         {
             struggleCoroutine = owner.StartCoroutine(ProcessStruggle());
         }
     }
     SubStatus = EAISubStatus.KillGotoTarget;
 }
    public void ChangeState(EAIStatus type, float t)
    {
        EAIStatusChange newState = new EAIStatusChange(type, t);

        StateStack.Clear();
        StateStack.Add(newState);
        if (type == EAIStatus.Kill)
        {
            SubStatus = EAISubStatus.KillGotoTarget;
            Debug.LogError("kill");
            killTarget = owner.GetLockedTarget();
        }
        ResetAIKey();
    }
    void OnPatrol()
    {
        switch (SubStatus)
        {
        case EAISubStatus.Patrol:
            //Debug.LogError("进入巡逻子状态-EAISubStatus.Patrol");
        {
            if (curPatrolIndex == PatrolPath.Count - 1)
            {
                targetPatrolIndex = 0;
            }
            else
            {
                targetPatrolIndex = (curPatrolIndex + 1) % PatrolPath.Count;
            }
            if (targetPatrolIndex != curPatrolIndex)
            {
                if (PatrolPath.Count <= targetPatrolIndex)
                {
                    OnIdle();
                    return;
                }

                if (Vector3.Distance(new Vector3(owner.mPos.x, 0, owner.mPos.z), new Vector3(PatrolPath[targetPatrolIndex].pos.x, 0, PatrolPath[targetPatrolIndex].pos.z)) <= 50)
                {
                    owner.controller.Input.AIMove(0, 0);
                    RotateRound    = Random.Range(1, 3);
                    SubStatus      = EAISubStatus.PatrolSubRotateInPlace;   //到底指定地点后旋转
                    curPatrolIndex = targetPatrolIndex;
                    //Debug.LogError("进入巡逻子状态-到底指定地点后原地旋转.PatrolSubRotateInPlace");
                    return;
                }
                //Debug.LogError("进入巡逻子状态-朝目标旋转");
                SubStatus = EAISubStatus.PatrolSubRotateToTarget;        //准备先对准目标
            }
            else
            {
                RotateRound = Random.Range(1, 3);
                SubStatus   = EAISubStatus.PatrolSubRotateInPlace;
            }
        }
        break;

        case EAISubStatus.PatrolSubRotateInPlace:
            if (RotateRound > 0)
            {
                if (PatrolRotateCoroutine == null)
                {
                    //Debug.LogError("进入巡逻子状态-到底指定地点后旋转.启动协程");
                    PatrolRotateCoroutine = owner.StartCoroutine(PatrolRotate());
                }
            }
            else
            {
                //旋转轮次使用完毕,下一次巡逻
                SubStatus = EAISubStatus.Patrol;
            }
            break;

        case EAISubStatus.PatrolSubRotateToTarget:
            if (PatrolRotateToTargetCoroutine == null)
            {
                //Debug.LogError("进入巡逻子状态-朝目标旋转.启动协程");
                PatrolRotateToTargetCoroutine = owner.StartCoroutine(PatrolRotateToTarget(PatrolPath[targetPatrolIndex].pos));
            }
            break;

        case EAISubStatus.PatrolSubGotoTarget:
            //Debug.LogError("进入巡逻子状态-朝目标输入移动");
            if (Vector3.Distance(new Vector3(owner.mPos.x, 0, owner.mPos.z), new Vector3(PatrolPath[targetPatrolIndex].pos.x, 0, PatrolPath[targetPatrolIndex].pos.z)) <= 50)
            {
                RotateRound    = Random.Range(1, 3);
                SubStatus      = EAISubStatus.PatrolSubRotateInPlace;//到底指定地点后旋转
                curPatrolIndex = targetPatrolIndex;
                //Debug.LogError("进入巡逻子状态-到底指定地点后原地旋转.PatrolSubRotateInPlace");
                owner.controller.Input.AIMove(0, 0);
                return;
            }
            owner.controller.Input.AIMove(0, 1);
            break;
        }
    }
    List <WayPoint> targetPath = new List <WayPoint>();//固定点位置.当只有单点的时候,表示可以直接走过去.也就是
    // Update is called once per frame
    public void Update()
    {
        //是否暂停AI。
        if (stoped)
        {
            return;
        }
        //return;
        if (paused)
        {
            pause_tick -= Time.deltaTime;
            if (pause_tick <= 0.0f)
            {
                paused = false;
            }
            return;
        }

        if (StateStack.Count != 0)
        {
            StateStack[StateStack.Count - 1].last -= Time.deltaTime;
            if (StateStack[StateStack.Count - 1].last <= 0.0f)
            {
                StateStack.RemoveAt(StateStack.Count - 1);
                if (StateStack.Count == 0)
                {
                    ResetAIKey();
                    if (owner.GetLockedTarget() != null)
                    {
                        killTarget = owner.GetLockedTarget();
                        Status     = EAIStatus.Kill;
                        SubStatus  = EAISubStatus.KillGotoTarget;
                    }
                    else if (PatrolPath.Count != 0)
                    {
                        Status    = EAIStatus.Patrol;
                        SubStatus = EAISubStatus.Patrol;
                    }
                    else
                    {
                        Status = EAIStatus.Wait;
                    }
                }
            }
            else
            {
                Status = StateStack[StateStack.Count - 1].type;
            }
        }

        switch (Status)
        {
        case EAIStatus.Idle:
            OnIdle();
            break;

        case EAIStatus.Guard:
            owner.controller.Input.OnKeyDown(EKeyList.KL_Defence, true);    //防御
            break;

        case EAIStatus.Wait:
            break;

        case EAIStatus.Patrol:
            OnPatrol();
            break;

        case EAIStatus.Follow:
            MovetoTarget(followTarget);
            break;

        case EAIStatus.Kill:
            //Debug.LogError("killstatus");
            switch (SubStatus)
            {
            case EAISubStatus.KillGotoTarget:
                //Debug.LogError("KillGotoTarget");
                if (killTarget == null)
                {
                    killTarget = owner.GetLockedTarget();
                }
                if (killTarget != null)
                {
                    MovetoTarget(killTarget);
                }
                else
                {
                    Status    = EAIStatus.Idle;
                    SubStatus = EAISubStatus.Think;
                }
                break;

            case EAISubStatus.KillGetTarget:
                //Debug.LogError("KillGetTarget");
                OnIdle();
                break;

            case EAISubStatus.KillOnHurt:
                OnHurt();
                break;
            }
            break;
        }
    }