wangguan
2020-12-04 52aa4f486f772f9aaf52eb6e7729eed8bbc298aa
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
using System.Collections.Generic;
using System.Collections;
using UnityEngine.UI;
using UnityEngine;
using EnhancedUI;
using EnhancedUI.EnhancedScroller;
using TMPro;
using UnityEngine.SceneManagement;
using KTGMGemClient;
using DG.Tweening;
using System;
using System.Linq;
 
public class FinalPanel : MonoBehaviour, IEnhancedScrollerDelegate
{
    //无限滚动相关
    private EnhancedScroller scroller;
    private SmallList<FinalPanelItemData> _data;
    public EnhancedScrollerCellView cellViewPrefab;
 
    private string endlessGameScene = "Endless2D";
    private string loadingScene = "LoadingScene";
    // Start is called before the first frame update
    void Start()
    {
        closeBtn = transform.Find("BackGround").GetComponent<Button>();
        closeBtn.onClick.AddListener(() =>
        {
            gameObject.SetActive(false);
            GameConfig.NextSceneName = endlessGameScene;
            SceneManager.LoadScene(loadingScene);
        });
        closeBtn.interactable = false;
    }
    Button closeBtn;
    Text waveText;
    GameObject image_HScore;
    Text scoreText;
    GameObject image_NewRecord;
    bool isNewH = false;
    int hScore;//最高分
    int tmpRank;//当前排名
    int tmpRankUp;//排名上升了多少
    //string hwaveInfo;
 
    CanvasGroup scrollerCanvasGroup;
    CanvasGroup itemCanvasGroup;
 
    ParticleSystem endPs;//最终特效
 
    string playerPrefs_HScore = "GemBattleHScore";
    //string playerPrefs_HWaveInfo = "GemBattleHWaveInfo";
    string playerPrefs_HRank = "GemBattleHRank";
 
    string playerPrefs_NickName = "GemBattlePlayerNickName";
 
    string _waveInfo;
    int _myScore;//本次
    FinalHttp finalHttp;
    string playerNickName;
    int _limit;
    int _limitUp;
 
    FinalPanelItem itemFirst;//第一
    FinalPanelItem itemSecond;//第二
    FinalPanelItem itemThird;//第三
    Text txt_Name, txt_Wave;
    TextMeshProUGUI txt_HScore, txt_Index;
 
    TextMeshProUGUI rankUpbj;
    /// <summary>
    /// 入口函数
    /// </summary>
    /// <param name="waveInfo">25关 3波</param>
    /// <param name="myScore">789220</param>
    public void SetData(string waveInfo, int myScore)
    {
        _waveInfo = waveInfo;
        _myScore = myScore;
        _data = new SmallList<FinalPanelItemData>();
        Debug.Log($"_myScore:{_myScore}  _waveInfo:{_waveInfo}");
 
        hScore = PlayerPrefs.GetInt(playerPrefs_HScore);
        tmpRank = PlayerPrefs.GetInt(playerPrefs_HRank);
        tmpRankUp = 0;
        //hwaveInfo = PlayerPrefs.GetString(playerPrefs_HWaveInfo);
        scrollerCanvasGroup = transform.Find("Scroller").GetComponent<CanvasGroup>();
        scrollerCanvasGroup.alpha = 0;
        itemCanvasGroup = transform.Find("ItemPanel").GetComponent<CanvasGroup>();
        itemCanvasGroup.alpha = 0;
 
        if (myScore > hScore)//得分比记录的高
        {
            PlayerPrefs.SetInt(playerPrefs_HScore, myScore);
            //PlayerPrefs.SetString(playerPrefs_HWaveInfo, waveInfo);
            hScore = myScore;
            //hwaveInfo = waveInfo;
            isNewH = true;
        }
 
        txt_Name = transform.Find("Image_HScore/Text_Name").GetComponent<Text>();
        txt_Wave = transform.Find("Image_HScore/Text_Wave").GetComponent<Text>();
        txt_HScore = transform.Find("Image_HScore/Text_HScore").GetComponent<TextMeshProUGUI>();
        txt_Index = transform.Find("Image_HScore/Text_Index").GetComponent<TextMeshProUGUI>();
 
 
        transform.Find("Image_HScore/Text_HScore").GetComponent<TextMeshProUGUI>().text = hScore.ToString();
        image_HScore = transform.Find("Image_HScore").gameObject;
        image_HScore.SetActive(false);
 
 
        waveText = transform.Find("Text_Wave").GetComponent<Text>();
        scoreText = transform.Find("Text_Score").GetComponent<Text>();
        scoreText.text = "";
        waveText.text = "";
        waveText.gameObject.SetActive(false);
        //scoreText.gameObject.SetActive(false);
 
        image_NewRecord = transform.Find("Image_NewRecord").gameObject;
        image_NewRecord.SetActive(false);
 
        finalHttp = GetComponent<FinalHttp>();
        _limit = 10;
        _limitUp = 10;
        finalHttp.Init(GetHttpData, _limit);//重置状态,设置回调和拉取的所有数据
 
        itemFirst = transform.Find("ItemPanel/FinalPanelItemFirst").GetComponent<FinalPanelItem>();
        itemSecond = transform.Find("ItemPanel/FinalPanelItemSecond").GetComponent<FinalPanelItem>();
        itemThird = transform.Find("ItemPanel/FinalPanelItemThird").GetComponent<FinalPanelItem>();
 
        httpCountDownPanel = transform.Find("HttpCountDownPanel").gameObject;
        countDownReplayBtn = transform.Find("HttpCountDownPanel/ReplayBtn").GetComponent<Button>();
        countDownTxt = transform.Find("HttpCountDownPanel/WarningTxt").GetComponent<Text>();
        httpCountDownPanel.SetActive(false);
 
        rankUpbj = transform.Find("Image_HScore/RankUp").GetComponent<TextMeshProUGUI>();
        rankUpbj.gameObject.SetActive(false);
 
        endPs = transform.Find("Effect_UI_JieSuanShengLi/03").GetComponent<ParticleSystem>();
 
        //PlayerPrefs.DeleteKey("GemBattlePlayerNickName");
 
        playerNickName = PlayerPrefs.GetString(playerPrefs_NickName);
        if (playerNickName == "")
        {
            GameObject go = Instantiate(Resources.Load<GameObject>("UI/Final/InputNamePanel"), transform);
            go.GetComponent<InputNamePanel>().Init(LoadName);
        }
        else
        {
            StartHttp();
        }
 
    }
 
    private void LoadName(string nickName)
    {
        playerNickName = nickName;
        PlayerPrefs.SetString(playerPrefs_NickName, playerNickName);
        StartHttp();
    }
 
    private void StartHttp()
    {
        //OpenHttpCountDown(true);
        Debug.Log($"_myScore:{_myScore}  _waveInfo:{_waveInfo}");
        finalHttp.SendPost(playerNickName, _myScore, 1, _waveInfo);
    }
 
    #region 联网倒计时判断
    bool isSending;
    int sendCount;
    GameObject httpCountDownPanel;
    Button countDownReplayBtn;
    Text countDownTxt;
    private void OpenHttpCountDown(bool isOn)
    {
        if (isOn)
        {
            isSending = true;
            sendCount = 0;
            StartCoroutine("HttpCountDown");
        }
        else
        {
            isSending = false;
            StopCoroutine("HttpCountDown");
        }
    }
    IEnumerator HttpCountDown()
    {
        while (isSending)
        {
            yield return new WaitForSeconds(1.0f);
            sendCount++;
            Debug.Log($"等待了{sendCount}秒");
            if (sendCount == 3)
            {
                if (!httpCountDownPanel.activeSelf) httpCountDownPanel.SetActive(true);
                if (countDownReplayBtn.gameObject.activeSelf) countDownReplayBtn.gameObject.SetActive(false);
            }
            else if (sendCount > 10)
            {
                Debug.Log("等了10秒了,连接失败");
                isSending = false;
                countDownTxt.text = "等了10秒了,连接失败";
                if (!countDownReplayBtn.gameObject.activeSelf) countDownReplayBtn.gameObject.SetActive(true);
 
                countDownReplayBtn.onClick.AddListener(Close);
                break;
            }
        }
    }
    #endregion
 
 
    private void Close()
    {
        gameObject.SetActive(false);
        GameConfig.NextSceneName = endlessGameScene;
        SceneManager.LoadScene(loadingScene);
    }
 
    private void GetHttpData(List<HttpGetInfo> allHttpGetInfoLis, int myrank, HttpGetInfo mydataHttpInfo)
    {
        scroller = transform.Find("Scroller").GetComponent<EnhancedScroller>();
        scroller.Delegate = this;
 
        RefreshHttpData(allHttpGetInfoLis, myrank, mydataHttpInfo);
 
        if (allHttpGetInfoLis == null) return;
 
        txt_Name.text = mydataHttpInfo.nickname;
        txt_HScore.text = mydataHttpInfo.score.ToString();
        txt_Wave.text = mydataHttpInfo.waveInfo;
 
        PlayDoTween();
    }
 
    private void PlayDoTween()
    {
        Sequence agentTweenSeq = DOTween.Sequence();
        //先出现
        Image tsTiele = transform.Find("Image_Tiele").GetComponent<Image>();
        agentTweenSeq.Append(tsTiele.transform.DOScale(Vector3.one, 0.2f));//变化图片大小
        agentTweenSeq.Join(tsTiele.DOFade(1, 0.3f));
 
        Image Image1 = transform.Find("Image1").GetComponent<Image>();
        agentTweenSeq.Append(Image1.transform.DOScale(Vector3.one, 0.2f));//变化图片大小
        agentTweenSeq.Join(Image1.DOFade(1, 0.2f));
        agentTweenSeq.Join(waveText.DOText(_waveInfo, 0.2f));
 
        Image Image2 = transform.Find("Image2").GetComponent<Image>();
        agentTweenSeq.Append(Image2.transform.DOScale(Vector3.one, 0.2f));//变化图片大小
        agentTweenSeq.Join(Image2.DOFade(1, 0.2f));
 
        Image Image3 = transform.Find("Image3").GetComponent<Image>();
        agentTweenSeq.Append(Image3.transform.DOScale(Vector3.one, 0.2f));//变化图片大小
        agentTweenSeq.Join(Image3.DOFade(1, 0.2f));
        agentTweenSeq.Join(DOTween.To(delegate (float value)
        {
            var temp = Math.Floor(value);
            scoreText.text = temp + "";
        }, 0, _myScore, 1.0f));
        agentTweenSeq.AppendCallback(StartLoadLargeData);
 
        tsLight = transform.Find("ImageLight");
        Image ImageLight = tsLight.GetComponent<Image>();
        agentTweenSeq.Join(ImageLight.transform.DOScale(Vector3.one, 0.2f));//变化图片大小
        agentTweenSeq.Join(ImageLight.DOFade(1, 0.2f).OnComplete(RotateLight));
        //agentTweenSeq.AppendCallback(RotateLight);
 
        agentTweenSeq.AppendCallback(TextDoTweenFinish);
    }
 
    bool isRotate = false;
    float offect = 0;
    /// <summary>
    /// LateUpdate is called every frame, if the Behaviour is enabled.
    /// It is called after all Update functions have been called.
    /// </summary>
    void LateUpdate()
    {
        if (isRotate)
        {
            offect += Time.deltaTime;
            tsLight.localRotation = Quaternion.Euler(0, 0, offect * 30);
        }
    }
 
    /// <summary>
    /// 开始展示排行榜
    /// </summary>
    protected void StartLoadLargeData()
    {
        StartCoroutine(PlayerItemDoTween());
    }
 
    /// <summary>
    /// 播放排行榜动效
    /// </summary>
    /// <returns></returns>
    IEnumerator PlayerItemDoTween()
    {
 
        yield return new WaitForEndOfFrame();
        scrollerCanvasGroup.alpha = 1;
        itemCanvasGroup.alpha = 1;
 
        image_HScore.SetActive(true);
 
        yield break;
    }
 
    /// <summary>
    /// 文字显示结束,显示波次和是否最高记录
    /// </summary>
    protected void TextDoTweenFinish()
    {
        waveText.gameObject.SetActive(true);
        if (isNewH)
        {
            image_NewRecord.SetActive(true);
 
            if (tmpRankUp > 0)
            {
                Debug.Log("排名上升了:" + tmpRankUp);
                if (!rankUpbj.gameObject.activeSelf)
                {
                    rankUpbj.gameObject.SetActive(true);
                    DOTween.To(delegate (float value)
                        {
                            var temp = Math.Floor(value);
                            rankUpbj.text = temp + "";
                        }, 0, tmpRankUp, 0.5f);
                }
            }
            else
            {
                Debug.Log("排名下降了");
            }
        }
 
        endPs?.Play();
 
        closeBtn.interactable = true;
    }
 
    Transform tsLight;
    /// <summary>
    /// 光出现后开始旋转
    /// </summary>
    protected void RotateLight()
    {
        isRotate = true;
        offect = 0;
    }
 
    #region EnhancedScroller Handlers
 
 
    /// <summary>
    /// 个数
    /// </summary>
    /// <param name="scroller"></param>
    /// <returns></returns>
    public int GetNumberOfCells(EnhancedScroller scroller)
    {
        return _data.Count;
    }
 
 
    /// <summary>
    /// 控制item的大小
    /// </summary>
    /// <param name="scroller"></param>
    /// <param name="dataIndex"></param>
    /// <returns></returns>
    public float GetCellViewSize(EnhancedScroller scroller, int dataIndex)
    {
        return 70f;
        //return (dataIndex % 2 == 0 ? 30f : 100f);
    }
 
    /// <summary>
    /// 渲染
    /// </summary>
    /// <param name="scroller"></param>
    /// <param name="dataIndex"></param>
    /// <param name="cellIndex"></param>
    /// <returns></returns>
    public EnhancedScrollerCellView GetCellView(EnhancedScroller scroller, int dataIndex, int cellIndex)
    {
        FinalPanelItem cellView = scroller.GetCellView(cellViewPrefab) as FinalPanelItem;
        cellView.SetData(_data[dataIndex]);
        //cellView.CheckRank(tmpRankUp);
        if (dataIndex == _data.Count - 2)
        {
            Debug.Log($"是否可以刷新新列表了  _limit:{_limit}    _data.Count:{_data.Count}");
 
            if (_limit >= 3000)
            {
                Debug.Log("数据下载量最大3000");
            }
            else if (_limit > _data.Count + 3)//从第三名开始无限滚动
            {
                Debug.Log("当前服务器没有那么多数据");
            }
            else
            {
                _limit += _limitUp;
                Debug.Log("刷新新列表了,下载了  " + _limit + "  个数据:");
                finalHttp.Init(RefreshHttpData, _limit);//重置状态,设置回调和拉取的所有数据
                StartHttp();
            }
        }
        return cellView;
    }
 
    private void RefreshHttpData(List<HttpGetInfo> allHttpGetInfoLis, int myrank, HttpGetInfo mydataHttpInfo)
    {
        if (allHttpGetInfoLis == null)
        {
            httpCountDownPanel.SetActive(true);
            countDownTxt.text = "获取排行榜失败";
            countDownReplayBtn.onClick.AddListener(Close);
            return;
        }
 
        // if (isSending)
        // {
        //     OpenHttpCountDown(false);
        // }
 
        FinalPanelItemData data;
 
        if (allHttpGetInfoLis.Count > 0)
        {
            //排名发生了变化
            if (tmpRank != myrank)
            {
                tmpRankUp = tmpRank - myrank;
 
                tmpRank = myrank;
                PlayerPrefs.SetInt(playerPrefs_HRank, tmpRank);
            }
            else
            {
 
            }
 
            if (myrank > 3000)
            {
                txt_Index.text = "3000+";
            }
            else
            {
                txt_Index.text = myrank.ToString();
            }
 
            if (allHttpGetInfoLis.Count >= 1)
            {
                data = new FinalPanelItemData();
                data.httpInfo = allHttpGetInfoLis[0];
                data.index = 1;
                itemFirst.gameObject.SetActive(true);
                itemFirst.SetData(data);
                //itemFirst.CheckRank(tmpRankUp);
            }
            else
            {
                itemFirst.gameObject.SetActive(false);
                itemSecond.gameObject.SetActive(false);
                itemThird.gameObject.SetActive(false);
                return;
            }
            if (allHttpGetInfoLis.Count >= 2)
            {
                data = new FinalPanelItemData();
                data.httpInfo = allHttpGetInfoLis[1];
                data.index = 2;
                itemSecond.gameObject.SetActive(true);
                itemSecond.SetData(data);
                //itemFirst.CheckRank(tmpRankUp);
            }
            else
            {
                itemSecond.gameObject.SetActive(false);
                itemThird.gameObject.SetActive(false);
                return;
            }
            if (allHttpGetInfoLis.Count > 2)
            {
                data = new FinalPanelItemData();
                data.httpInfo = allHttpGetInfoLis[2];
                data.index = 3;
                itemThird.gameObject.SetActive(true);
                itemThird.SetData(data);
                //itemFirst.CheckRank(tmpRankUp);
            }
            else
            {
                itemThird.gameObject.SetActive(false);
                return;
            }
            if (allHttpGetInfoLis.Count > 3)
            {
                _data.Clear();
                for (int i = 3; i < allHttpGetInfoLis.Count; i++)
                {
                    data = new FinalPanelItemData();
                    data.httpInfo = allHttpGetInfoLis[i];
                    data.index = i + 1;
                    _data.Add(data);
                }
                scroller.ReloadData();
            }
 
        }
    }
 
 
 
 
    #endregion
 
}
 
public class FinalPanelItemData
{
    public HttpGetInfo httpInfo;
    public int index;
}