using System.Collections.Generic; 
 | 
using UnityEngine; 
 | 
using UnityEngine.UI; 
 | 
using DG.Tweening; 
 | 
using Cysharp.Threading.Tasks; 
 | 
using System; 
 | 
  
 | 
public class GoldRushLeader : MonoBehaviour 
 | 
{ 
 | 
    [SerializeField] UIHeroController leader;   //监工 
 | 
    // [SerializeField] GameObject goods; 
 | 
    [SerializeField] RectTransform leaderGo; 
 | 
    [SerializeField] UIAlphaTween leaderWord; 
 | 
    [SerializeField] Text leaderText; 
 | 
    public float leaderScale = 0.8f; 
 | 
  
 | 
    public Tween leaderSequence; 
 | 
    private int leaderPosIndex = -1; //监工已达移动点 
 | 
  
 | 
    int tendID; 
 | 
    GoldRushPosEvent[] leaderPathPointArr;  //监工移动点 
 | 
  
 | 
    public Action<bool> OnComplete; 
 | 
    public bool forcrCallBack; 
 | 
  
 | 
    public void Init(GoldRushPosEvent[] _leaderPathPointArr, float waitTime, int _tendID, int index, bool isBack, 
 | 
        Action<bool> _OnComplete) 
 | 
    { 
 | 
        tendID = _tendID; 
 | 
        leaderPathPointArr = _leaderPathPointArr; 
 | 
        leaderPosIndex = !isBack ? -1 : leaderPathPointArr.Length - 1; 
 | 
        leaderWord.Stop(); 
 | 
        leaderWord.SetEndState(); 
 | 
        // goods.SetActive(isBack); 
 | 
        leaderGo.localPosition = !isBack ? leaderPathPointArr[0].transform.localPosition : 
 | 
                                leaderPathPointArr[leaderPathPointArr.Length - 1].transform.localPosition; 
 | 
        OnComplete = _OnComplete; 
 | 
        this.SetActive(true); 
 | 
        leader.Create(GoldRushManager.Instance.GetWorkerSkinID(tendID, index), leaderScale); 
 | 
        Go(waitTime, isBack).Forget(); 
 | 
    } 
 | 
  
 | 
  
 | 
  
 | 
    async UniTask Go(float waitTime, bool isBack) 
 | 
    { 
 | 
        int delayTime = Math.Max(1, (int)(waitTime * 1000)); 
 | 
        await UniTask.Delay(delayTime); 
 | 
        StartLeaderMove(isBack); 
 | 
    } 
 | 
  
 | 
    public void StartLeaderMove(bool isBack) 
 | 
    { 
 | 
        leaderSequence.Kill(); 
 | 
  
 | 
        leaderWord.Stop(); 
 | 
        leaderWord.SetEndState(); 
 | 
        LeaderMove(isBack); 
 | 
  
 | 
    } 
 | 
  
 | 
    void LeaderMove(bool isBack) 
 | 
    { 
 | 
        int moveIndex = isBack ? leaderPosIndex - 1 : leaderPosIndex + 1; 
 | 
        //判断事件, 返程用当前点事件,出发用下一个点的事件 
 | 
        GoldRushPosEvent pathPosEvent = isBack ? leaderPathPointArr[leaderPosIndex] : leaderPathPointArr[moveIndex];   
 | 
  
 | 
        if (moveIndex <= 0) 
 | 
        { 
 | 
            moveIndex = 0; 
 | 
        } 
 | 
        else if (moveIndex >= leaderPathPointArr.Length) 
 | 
        { 
 | 
            moveIndex = leaderPathPointArr.Length - 1; 
 | 
        } 
 | 
  
 | 
        Vector3 nextPos = leaderPathPointArr[moveIndex].transform.localPosition; 
 | 
        if (isBack) 
 | 
        { 
 | 
            if (forcrCallBack) 
 | 
            { 
 | 
                leader.PlayAnimation("run", true, false); 
 | 
            } 
 | 
            else 
 | 
            { 
 | 
                leader.PlayAnimation("run_xiangzi", true, false); 
 | 
            } 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            leader.PlayAnimation("run", true, false); 
 | 
        }    
 | 
  
 | 
        leaderPosIndex = moveIndex; 
 | 
        bool isRush = pathPosEvent.m_PosEvent == PosEvent.Rush; 
 | 
        bool isJump = pathPosEvent.m_PosEvent == PosEvent.Jump; 
 | 
  
 | 
        leader.SetSpeed((isRush ? 2 : 1) * (isBack ? 0.5f : 1)); 
 | 
  
 | 
  
 | 
        var dis = Vector3.Distance(leaderGo.localPosition, nextPos); 
 | 
        var duration = dis / pathPosEvent.m_Speed / (isBack ? pathPosEvent.m_BackSlowSpeedScale : 1); 
 | 
  
 | 
        // Debug.Log("第" + workerIndex + "个工人" +  " duration" + duration + " 移动index " + moveIndex + " Time=" + Time.time); 
 | 
        if (leaderGo.localPosition.x < nextPos.x) 
 | 
        { 
 | 
            leader.transform.localRotation = Quaternion.Euler(0, 0, 0); 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            //转向 
 | 
            leader.transform.localRotation = Quaternion.Euler(0, 180, 0); 
 | 
        } 
 | 
  
 | 
        PathEvent(pathPosEvent, isBack); 
 | 
  
 | 
        if (!isJump) 
 | 
        { 
 | 
            leaderSequence = leaderGo.DOLocalMove(nextPos, duration).SetEase(pathPosEvent.m_EaseType); 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            //抛物线跳跃 
 | 
            // 计算抛物线路径点 
 | 
            Vector3[] path = new Vector3[3]; 
 | 
            path[0] = leaderGo.localPosition; // 起点 
 | 
            //顶点x 是两者之间 y增加高度 
 | 
            path[1] = new Vector3(nextPos[0] + (leaderGo.localPosition.x - nextPos[0]) / 2, nextPos[1] + pathPosEvent.m_Value1, nextPos.z); 
 | 
            path[2] = nextPos; // 终点 
 | 
  
 | 
            // 使用 DOPath 实现抛物线移动 
 | 
            leaderSequence = leaderGo.DOLocalPath(path, pathPosEvent.m_Value2, PathType.CatmullRom).SetEase(pathPosEvent.m_EaseType); 
 | 
        } 
 | 
        leaderSequence.OnComplete(() => 
 | 
        { 
 | 
            // Debug.Log("Sequence completed for worker " + workerIndex); 
 | 
            if (moveIndex == (isBack ? 0 : leaderPathPointArr.Length - 1)) 
 | 
            { 
 | 
                leader.PlayAnimation("idle", true, false); 
 | 
                leader.transform.localRotation = Quaternion.Euler(0, 0, 0); 
 | 
  
 | 
                this.SetActive(false); 
 | 
                OnComplete(isBack); 
 | 
                return; 
 | 
            } 
 | 
            LeaderMove(isBack); 
 | 
        }); 
 | 
  
 | 
    } 
 | 
  
 | 
    void PathEvent(GoldRushPosEvent pathPosEvent, bool isBack) 
 | 
    { 
 | 
        if (isBack) 
 | 
        { 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        if (pathPosEvent.m_PosEvent == PosEvent.TargetFollow) 
 | 
        { 
 | 
            // StartMove(isBack); 
 | 
            GoldRushManager.Instance.NotifyPathEvent(pathPosEvent.m_PosEvent, isBack, tendID, 0, ""); 
 | 
        } 
 | 
        else if (pathPosEvent.m_PosEvent == PosEvent.Word) 
 | 
        { 
 | 
            leaderWord.SetActive(true); 
 | 
            leaderWord.Play(); 
 | 
            leaderText.text = Language.Get(pathPosEvent.m_Text1); 
 | 
        } 
 | 
        else if (pathPosEvent.m_PosEvent == PosEvent.TargetWord) 
 | 
        { 
 | 
  
 | 
            if (!pathPosEvent.m_IsRandom) 
 | 
            { 
 | 
                //指定语言 
 | 
                if (pathPosEvent.m_Value1 <= 0) 
 | 
                { 
 | 
                    //全体说话 
 | 
                    for (int i = 0; i < GoldRushManager.followWorkerCount; i++) 
 | 
                    { 
 | 
                        GoldRushManager.Instance.NotifyPathEvent(pathPosEvent.m_PosEvent, isBack, tendID, i, pathPosEvent.m_Text1); 
 | 
                    } 
 | 
                } 
 | 
                else 
 | 
                { 
 | 
                    //单个说话 
 | 
                    GoldRushManager.Instance.NotifyPathEvent(pathPosEvent.m_PosEvent, isBack, tendID, (int)pathPosEvent.m_Value1 - 1, pathPosEvent.m_Text1); 
 | 
                } 
 | 
            } 
 | 
            else 
 | 
            { 
 | 
                //随机全体说话 
 | 
                for (int i = 0; i < GoldRushManager.followWorkerCount; i++) 
 | 
                { 
 | 
                    GoldRushManager.Instance.NotifyPathEvent(pathPosEvent.m_PosEvent, isBack, tendID, i, 
 | 
                    pathPosEvent.m_Text1 + UnityEngine.Random.Range((int)pathPosEvent.m_Value1, (int)pathPosEvent.m_Value2)); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        else if (pathPosEvent.m_PosEvent == PosEvent.TargetAction) 
 | 
        { 
 | 
            for (int i = 0; i < GoldRushManager.followWorkerCount; i++) 
 | 
            { 
 | 
                GoldRushManager.Instance.NotifyPathEvent(pathPosEvent.m_PosEvent, isBack, tendID, i, pathPosEvent.m_Text1); 
 | 
            } 
 | 
        } 
 | 
        else if (pathPosEvent.m_PosEvent == PosEvent.Action) 
 | 
        { 
 | 
            leader.PlayAnimation(pathPosEvent.m_Text1); 
 | 
        } 
 | 
  
 | 
    } 
 | 
  
 | 
  
 | 
} 
 |