hch
4 天以前 f2c78eecd49edc328225b77492e7f45e157bad02
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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; //监工已达移动点
    private int state = 0; //0 站岗(不显示) 1 前进 2 空手返程 3 货物返程(不中断)
 
    int tendID;
    GoldRushPosEvent[] leaderPathPointArr;  //监工移动点
 
    Action<bool> OnComplete;
 
    public void Init(GoldRushPosEvent[] _leaderPathPointArr, float waitTime, int _tendID, bool isBack, Action<bool> _OnComplete)
    {
        tendID = _tendID;
        leaderPathPointArr = _leaderPathPointArr;
        leaderPosIndex = !isBack ? -1: leaderPathPointArr.Length - 1;
        state = 0;
        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.GetRandommSkinID(), 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)
        { 
            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);
        }
 
    }
 
 
}