hch
3 天以前 7222adcc97941b57a61f0f8e8469f0f66b9ee1a1
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
 
//工人是固定的跟随模式,监工是随机分配的,场上的监工数量会比实际更多
//分配监工
// 1.分配都是新的监工 一定从起点开始
// 2.撤回的可以从半路撤回
public class GoldRushTentCell : MonoBehaviour
{
    [SerializeField] Button tentBtn;
    [SerializeField] GameObject funcGo;
    [SerializeField] GameObject lockCntGo;  //淘金次数解锁
    [SerializeField] Text lockCntText;
    [SerializeField] Button unLockBtn;
    [SerializeField] GameObject lockMoneyGo;
    [SerializeField] Button unlockMoneyBtn; //货币解锁
    [SerializeField] Image unlockMoneyIcon;
    [SerializeField] Text unlockMoneyText;
    [SerializeField] GameObject goldRushMissionWaitGo;  //未发布任务
    [SerializeField] GameObject goldRushMissionWorkingGo;  //已发布任务
    [SerializeField] Text goldRushItemText;
    [SerializeField] Image workingProcess;
    [SerializeField] UIEffectPlayer refreshItemEffect;
    [SerializeField] Text workingText; //外出中
 
 
    [SerializeField] UIHeroController[] workerArr; //小兵跟随
    [SerializeField] RectTransform[] workerGoArr;
    [SerializeField] RectTransform[] hidePosArr;   //未解锁时 屏幕外坐标
    [SerializeField] RectTransform[] startPosArr;   //起点 站岗坐标
    [SerializeField] GoldRushPosEvent[] pathPointArr;  //移动点
    [SerializeField] UIAlphaTween[] wordArr;
    [SerializeField] Text[] textArr;
    [SerializeField] GoldRushLeader tmpLeader;  //监工
    [SerializeField] Transform leaderParent;
    [SerializeField] GoldRushPosEvent[] leaderPathPointArr;
    public int campID = 0;
 
    //小兵工人
    Tween[] sequenceArr = new Tween[3];
    Dictionary<int, int> workerPosDic = new Dictionary<int, int>(); //工人已达移动点
 
    List<GoldRushLeader> workingLeaderList = new List<GoldRushLeader>();  //正在工作的监工 和分配列表一致
    List<GoldRushLeader> callBackLeaderList = new List<GoldRushLeader>();  //返程的监工(完工的 和 被召回的)
    Queue<GoldRushLeader> poolLeaderList = new Queue<GoldRushLeader>();  //监工池
 
    int workState = -1; // - 1未解锁 0 站岗 1 前进 2 返程 
    int leaderCount = 0; //当前监工数量
    void Start()
    {
        Init();
        tentBtn.AddListener(ClickTent);
        unlockMoneyBtn.AddListener(ClickUnlockMoney);
        unLockBtn.AddListener(ClickUnlock);
    }
 
 
    void OnEnable()
    {
        GoldRushManager.Instance.GoldRushEvent += GoldRushEvent;
        GoldRushManager.Instance.PathEvent += PathEvent;
        GoldRushManager.Instance.OnGoldRushCampEvent += OnGoldRushCampEvent;
        GoldRushManager.Instance.OnGoldRushInfoEvent += Display;
        GoldRushManager.Instance.OnRefreshItemEvent += OnRefreshItemEvent;
        GlobalTimeEvent.Instance.secondEvent += OnSecondEvent;
        leaderCount = GoldRushManager.Instance.GetCampWorkerCnt(campID);
        Display();
        
    }
 
    void OnDisable()
    {
        GoldRushManager.Instance.GoldRushEvent -= GoldRushEvent;
        GoldRushManager.Instance.PathEvent -= PathEvent;
        GoldRushManager.Instance.OnGoldRushCampEvent -= OnGoldRushCampEvent;
        GoldRushManager.Instance.OnGoldRushInfoEvent -= Display;
        GoldRushManager.Instance.OnRefreshItemEvent -= OnRefreshItemEvent;
        GlobalTimeEvent.Instance.secondEvent -= OnSecondEvent;
    }
 
    void FixFollowWoker(int lockState)
    {
        //跟随小兵如果在外出中,且没有监工,则隐藏
        if (workingLeaderList.Count == 0 && callBackLeaderList.Count == 0 && workState <= 0
        && GoldRushManager.Instance.GetCampEndTime(campID) != 0)
        {
            for (int i = 0; i < workerArr.Length; i++)
            {
                workerGoArr[i].localPosition = hidePosArr[i].localPosition;
            }
            return;
        }
        
        // 初始状态
        if (lockState != 0)
        {
            //未解锁 显示隐藏坐标
            for (int i = 0; i < workerArr.Length; i++)
            {
                workerGoArr[i].localPosition = hidePosArr[i].localPosition;
            }
        }
        else if (workState == -1 && lockState == 0)
        {
            //解锁的时候,如果不在起点则跑步过去
            workState = 0;
            for (int i = 0; i < sequenceArr.Length; i++)
            {
                sequenceArr[i].Kill();
                var worker = workerArr[i];
                worker.PlayAnimation("run", true, false);
                sequenceArr[i] = workerGoArr[i].DOLocalMove(startPosArr[i].localPosition, 1f).SetEase(Ease.Linear);
 
                sequenceArr[i].OnComplete(() =>
                {
                    worker.PlayAnimation("idle", true, false);
                });
            }
        }
 
    }
 
 
    void OnGoldRushCampEvent(int _campID)
    {
        if (_campID != campID)
            return;
 
        var realCount = GoldRushManager.Instance.GetCampWorkerCnt(campID);
        if (realCount > leaderCount)
        {
            //分配监工
            GoldRushManager.Instance.NotifyGoldRushEvent(campID, 0, realCount - leaderCount);
        }
        else if (realCount < leaderCount)
        {
            //召回监工
            GoldRushManager.Instance.NotifyGoldRushEvent(campID, 1, leaderCount - realCount);
        }
        leaderCount = realCount;
        Display();
    }
 
    void OnRefreshItemEvent(int _campID)
    {
        if (_campID != campID)
            return;
        
        refreshItemEffect.Play();
    }
 
    void Display()
    {
        if (!FuncOpen.Instance.IsFuncOpen(GoldRushManager.funcID))
        {
            funcGo.SetActive(false);
            return;
        }
        funcGo.SetActive(true);
 
        var campConfig = GoldRushCampConfig.Get(campID);
        int lockState = GoldRushManager.Instance.GetCampLockState(campID);
        if (lockState == 1)
        {
            if (GoldRushManager.Instance.panningCnt < campConfig.PanningUnlock)
            {
                lockCntGo.SetActive(true);
                unLockBtn.SetActive(false);
                lockCntText.text = Language.Get("GoldRush32", GoldRushManager.Instance.panningCnt, campConfig.PanningUnlock);
            }
            else
            {
                lockCntGo.SetActive(false);
                unLockBtn.SetActive(true);
            }
            lockMoneyGo.SetActive(false);
            goldRushMissionWaitGo.SetActive(false);
            goldRushMissionWorkingGo.SetActive(false);
        }
        else if (lockState == 2)
        {
            lockCntGo.SetActive(false);
            unLockBtn.SetActive(false);
            lockMoneyGo.SetActive(true);
 
            unlockMoneyIcon.SetIconWithMoneyType(campConfig.MoneyUnlock[0]);
            unlockMoneyText.text = campConfig.MoneyUnlock[1].ToString();
            goldRushMissionWaitGo.SetActive(false);
            goldRushMissionWorkingGo.SetActive(false);
        }
        else
        {
            lockCntGo.SetActive(false);
            unLockBtn.SetActive(false);
            lockMoneyGo.SetActive(false);
            var goldID = GoldRushManager.Instance.GetCampGoldID(campID);
            if (goldID == 0)
            {
                goldRushMissionWaitGo.SetActive(true);
                goldRushMissionWorkingGo.SetActive(false);
            }
            else
            {
                goldRushMissionWaitGo.SetActive(false);
                goldRushMissionWorkingGo.SetActive(true);
                var goldConfig = GoldRushItemConfig.Get(goldID);
                goldRushItemText.text = GoldRushManager.Instance.GetCampItemName(goldConfig);
 
                var endTime = GoldRushManager.Instance.GetCampEndTime(campID);
                if (endTime == 0)
                {
                    //未开始
                    workingText.text = "";
                    workingProcess.fillAmount = 0;
                }
                else
                {
                    //按原总时长当进度条
                    workingProcess.fillAmount = (goldConfig.NeedSeconds - (GoldRushManager.Instance.GetCampEndTime(campID) - TimeUtility.AllSeconds)) / (float)goldConfig.NeedSeconds;
                }
 
            }
        }
 
        FixFollowWoker(lockState);
    }
 
    void OnSecondEvent()
    {
        //进度条和 外出中文字
        int lockState = GoldRushManager.Instance.GetCampLockState(campID);
        if (lockState != 0)
        {
            return;
        }
        var goldID = GoldRushManager.Instance.GetCampGoldID(campID);
        if (goldID == 0)
        {
            return;
        }
        var endTime = GoldRushManager.Instance.GetCampEndTime(campID);
        if (endTime == 0)
        {
            return;
        }
 
        var addStr = new string('.', (int)Time.time % 4);
        workingText.text = Language.Get("GoldRush37") + addStr;
        var goldConfig = GoldRushItemConfig.Get(goldID);
        //按原总时长当进度条
        workingProcess.fillAmount = (goldConfig.NeedSeconds - (GoldRushManager.Instance.GetCampEndTime(campID) - TimeUtility.AllSeconds)) / (float)goldConfig.NeedSeconds;
           
    }
 
    void GoldRushEvent(int _campID, int eventType, int leaderCount)
    {
        if (_campID != campID)
        {
            return;
        }
        if (eventType == 0)
        {
            //出发
            AssignLeader(leaderCount);
        }
        else if (eventType == 1)
        {
            //返程
            CallBackLeader(leaderCount);
        }
    }
 
 
 
    void Init()
    {
        for (int i = 0; i < workerArr.Length; i++)
        {
            workerPosDic[i] = -1;   //-1代表起点,站岗坐标
            wordArr[i].SetEndState();
        }
    }
 
    public void StartMove(bool isBack)
    {
        for (int i = 0; i < sequenceArr.Length; i++)
        {
            sequenceArr[i].Kill();
            WorkerMove(isBack, i);
        }
 
    }
 
    void WorkerMove(bool isBack, int workerIndex)
    {
        var worker = workerArr[workerIndex];
        var workerGo = workerGoArr[workerIndex];
        int curIndex = workerPosDic[workerIndex];
        int moveIndex = isBack ? curIndex - 1 : curIndex + 1;
 
 
        GoldRushPosEvent pathPosEvent = isBack ? pathPointArr[curIndex] : pathPointArr[moveIndex];  //判断事件, 当前点或者下一个点
 
        Vector3 nextPos;
        if (moveIndex <= -1)
        {
            moveIndex = -1;
            nextPos = startPosArr[workerIndex].localPosition;
        }
        else
        {
            if (moveIndex >= pathPointArr.Length)
            {
                moveIndex = pathPointArr.Length - 1;
            }
            nextPos = pathPointArr[moveIndex].transform.localPosition;
        }
        worker.PlayAnimation("run", true, false);
 
        workerPosDic[workerIndex] = moveIndex;
        bool isRush = pathPosEvent.m_PosEvent == PosEvent.Rush;
        bool isJump = pathPosEvent.m_PosEvent == PosEvent.Jump;
 
        worker.SetSpeed((isRush ? 2 : 1) * (isBack ? 0.5f : 1));
 
        // 先出发的终点要往前一点
        Vector3 offset = Vector3.zero;
        if (moveIndex == pathPointArr.Length - 1)
        {
            offset = new Vector3((pathPointArr.Length - workerIndex - 1) * 70, 0, 0);
        }
        var endPos = nextPos - offset;
 
        var dis = Vector3.Distance(workerGo.localPosition, endPos);
        var duration = dis / pathPosEvent.m_Speed / (isBack ? pathPosEvent.m_BackSlowSpeedScale : 1);
 
        // Debug.Log("第" + workerIndex + "个工人" +  " duration" + duration + " 移动index " + moveIndex + " Time=" + Time.time);
        if (workerGo.localPosition.x < endPos.x)
        {
            worker.transform.localRotation = Quaternion.Euler(0, 0, 0);
        }
        else
        {
            //转向
            worker.transform.localRotation = Quaternion.Euler(0, 180, 0);
        }
        if (!isJump)
        {
            sequenceArr[workerIndex] = workerGo.DOLocalMove(endPos, duration).SetEase(pathPosEvent.m_EaseType);
        }
        else
        {
            //抛物线跳跃
            // 计算抛物线路径点
            Vector3[] path = new Vector3[3];
            path[0] = workerGo.localPosition; // 起点
            //顶点x 是两者之间 y增加高度
            path[1] = new Vector3(endPos[0] + (workerGo.localPosition.x - endPos[0]) / 2, endPos[1] + pathPosEvent.m_Value1, nextPos.z);
            path[2] = endPos; // 终点
 
            // 使用 DOPath 实现抛物线移动
            sequenceArr[workerIndex] = workerGo.DOLocalPath(path, pathPosEvent.m_Value2, PathType.CatmullRom).SetEase(pathPosEvent.m_EaseType);
        }
        sequenceArr[workerIndex].OnComplete(() =>
        {
            // Debug.Log("Sequence completed for worker " + workerIndex);
            if (moveIndex == (isBack ? -1 : pathPointArr.Length - 1))
            {
                worker.PlayAnimation("idle", true, false);
                worker.transform.localRotation = Quaternion.Euler(0, 0, 0);
                if (moveIndex == -1)
                {
                    workState = 0;
                }
                return;
            }
 
            WorkerMove(isBack, workerIndex);
        });
 
    }
 
 
 
    void PathEvent(PosEvent posEvent, bool isBack, int _tendID, int index, string content)
    {
        if (_tendID != campID)
            return;
 
        if (posEvent == PosEvent.TargetFollow)
        {
            if (isBack)
                return;
            // 已经在外出中不跟随
            if (workState == 1)
                return;
            StartMove(false);
            workState = 1;
        }
 
        else if (posEvent == PosEvent.TargetWord)
        {
            wordArr[index].SetActive(true);
            wordArr[index].Play();
            textArr[index].text = Language.Get(content);
        }
        else if (posEvent == PosEvent.TargetAction)
        {
            for (int i = 0; i < wordArr.Length; i++)
            {
                workerArr[i].PlayAnimation(content);
            }
        }
 
    }
 
 
 
 
 
    void AssignLeader(int addCount)
    {
        //分配的时候加新的监工
        for (int i = 0; i < addCount; i++)
        {
            float waitTime = i * 0.6f;
            var newLeader = RequestLeader();
            workingLeaderList.Add(newLeader);
            newLeader.Init(leaderPathPointArr, waitTime, campID, false, (bool value) =>
            {
                if (!value)
                {
                    workingLeaderList.Remove(newLeader);
                    ReturnLeader(newLeader);
                }
                else
                {
                    //半路返回的监工
                    callBackLeaderList.Add(newLeader);
                    ReturnLeader(newLeader);
                }
            });
        }
    }
 
    GoldRushLeader RequestLeader()
    {
        if (poolLeaderList.Count == 0)
        {
            return Instantiate(tmpLeader, leaderParent);
        }
        return poolLeaderList.Dequeue();
    }
 
    void ReturnLeader(GoldRushLeader leader)
    {
        poolLeaderList.Enqueue(leader);
    }
 
 
    //半路召回的,拉货回来的
    void CallBackLeader(int callBackCount)
    {
        bool followBack = false;
        //从workingLeaderList 中取出最后面调回的监工,不够的新建
        for (int i = 0; i < callBackCount; i++)
        {
            if (workingLeaderList.Count > 0)
            {
                //半路拉回来的
                var leader = workingLeaderList[workingLeaderList.Count - 1];
                //原样显示
                callBackLeaderList.Add(leader);
                workingLeaderList.Remove(leader);
                leader.StartLeaderMove(true);
                if (workingLeaderList.Count == 0 && workState == 1)
                {
                    followBack = true;
                }
            }
            else
            {
                //需要显示货物
                float waitTime = i * 0.6f;
                var newLeader = RequestLeader();
                callBackLeaderList.Add(newLeader);
                newLeader.Init(leaderPathPointArr, waitTime, campID, true, (bool value) =>
                {
                    if (value)
                    {
                        //返回的监工,需要显示货物
                        callBackLeaderList.Add(newLeader);
                        ReturnLeader(newLeader);
                    }
                });
                if (workingLeaderList.Count == 0 && workState == 1)
                {
                    followBack = true;
                }
            }
        }
 
        if (followBack)
        {
            StartMove(true);
            workState = 2;
        }
    }
 
    void ClickTent()
    {
        if (!FuncOpen.Instance.IsFuncOpen(GoldRushManager.funcID))
        {
            return;
        }
        var lockState = GoldRushManager.Instance.GetCampLockState(campID);
        if (lockState != 0)
        {
            return;
        }
        var goldID = GoldRushManager.Instance.GetCampGoldID(campID);
        if (goldID != 0)
        {
            GoldRushManager.Instance.selectCampID = campID;
            UIManager.Instance.OpenWindow<GoldRushRefreshWin>();
            return;
        }
 
        if (!UIHelper.CheckMoneyCount(52, 1, 1))
        {
            return;
        }
 
        GoldRushManager.Instance.SendGoldRushOP(0, campID, 0);
    }
 
    void ClickUnlockMoney()
    {
        int lockState = GoldRushManager.Instance.GetCampLockState(campID);
        if (lockState != 2)
        {
            return;
        }
        var config = GoldRushCampConfig.Get(campID);
 
        ConfirmCancel.MoneyIconToggleConfirmByType(ToggleCheckType.GoldRush, config.MoneyUnlock[1], config.MoneyUnlock[0],
            Language.Get("GoldRush35", UIHelper.GetIconNameWithMoneyType(config.MoneyUnlock[0]), config.MoneyUnlock[1]), () =>
                {
                    if (!UIHelper.CheckMoneyCount(config.MoneyUnlock[0], config.MoneyUnlock[1], 2))
                    {
                        return;
                    }
                    GoldRushManager.Instance.SendGoldRushUnlock(0, campID);
                    refreshItemEffect.Play();
                    SysNotifyMgr.Instance.ShowTip("GoldRush8");
                });
                
 
    }
 
    //达到可解锁条件 需手动解锁
    void ClickUnlock()
    {
        int lockState = GoldRushManager.Instance.GetCampLockState(campID);
        if (lockState != 1)
        {
            return;
        }
        var config = GoldRushCampConfig.Get(campID);
        if (GoldRushManager.Instance.panningCnt < config.PanningUnlock)
            return;
 
        GoldRushManager.Instance.SendGoldRushUnlock(0, campID);
        refreshItemEffect.Play();
        SysNotifyMgr.Instance.ShowTip("GoldRush8");
    }
}