chenxin
2020-12-03 505eff9fbd3d2b90c675a9297e26209386ca29aa
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
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;
    CanvasGroup scrollerCanvasGroup;
    CanvasGroup itemCanvasGroup;
 
    Action playEffectAC = null;
 
 
    /// <summary>
    /// 设置播放胜利特效
    /// </summary>
    /// <param name="ac"></param>
    public void SetPlayEffectAC(Action ac)
    {
        playEffectAC = ac;
    }
 
    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;
    /// <summary>
    /// 入口函数
    /// </summary>
    /// <param name="waveInfo">成绩 25关 3波</param>
    /// <param name="myScore">789220</param>
    public void SetData(string waveInfo, int myScore)
    {
        _waveInfo = waveInfo;
        _myScore = myScore;
 
        hScore = PlayerPrefs.GetInt("GemBattleHScore");
        scrollerCanvasGroup = transform.Find("Scroller").GetComponent<CanvasGroup>();
        scrollerCanvasGroup.alpha = 0;
        itemCanvasGroup = transform.Find("ItemPanel").GetComponent<CanvasGroup>();
        itemCanvasGroup.alpha = 0;
 
        if (myScore > hScore)
        {
            PlayerPrefs.SetInt("GemBattleHScore", myScore);
            hScore = myScore;
            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 = 100;
        _limitUp = 100;
        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>();
 
        //PlayerPrefs.DeleteKey("GemBattlePlayerNickName");
 
        playerNickName = PlayerPrefs.GetString("GemBattlePlayerNickName");
        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("GemBattlePlayerNickName", playerNickName);
        StartHttp();
    }
 
    private void StartHttp()
    {
        finalHttp.SendPost(playerNickName, _myScore, 1, _waveInfo);
    }
 
    private void GetHttpData(List<HttpGetInfo> allHttpGetInfoLis, int myrank)
    {
        scroller = transform.Find("Scroller").GetComponent<EnhancedScroller>();
        scroller.Delegate = this;
 
        RefreshHttpData(allHttpGetInfoLis, myrank);
 
        txt_Name.text = playerNickName;
        txt_HScore.text = _myScore.ToString();
        txt_Wave.text = _waveInfo;
        if (myrank > 3000)
        {
            txt_Index.text = "3000+";
        }
        else
        {
            txt_Index.text = myrank.ToString();
        }
 
        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));
 
        agentTweenSeq.AppendCallback(StartLoadLargeData);
 
        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));
 
        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()
    {
        scoreText.gameObject.SetActive(true);
 
        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 (playEffectAC != null)
        {
            playEffectAC();
        }
 
        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]);
 
        if (dataIndex == _data.Count - 2)
        {
            Debug.Log("是否可以刷新新列表了");
 
            if (_limit >= 3000)
            {
                Debug.Log("数据下载量最大3000");
            }
            else if (_limit >= _data.Count)
            {
                Debug.Log("当前服务器没有那么多数据");
            }
            else
            {
                _limit += _limitUp;
                Debug.Log("刷新新列表了,下载了  " + _limit + "  个数据:");
                finalHttp.Init(RefreshHttpData, _limit);//重置状态,设置回调和拉取的所有数据
            }
        }
        return cellView;
    }
 
    private void RefreshHttpData(List<HttpGetInfo> allHttpGetInfoLis, int myrank)
    {
        _data = new SmallList<FinalPanelItemData>();
        FinalPanelItemData data;
 
        if (allHttpGetInfoLis.Count > 0)
        {
            if (allHttpGetInfoLis.Count >= 1)
            {
                data = new FinalPanelItemData();
                data.httpInfo = allHttpGetInfoLis[0];
                data.index = 1;
                itemFirst.gameObject.SetActive(true);
                itemFirst.SetData(data);
            }
            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);
            }
            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);
            }
            else
            {
                itemThird.gameObject.SetActive(false);
                return;
            }
            if (allHttpGetInfoLis.Count > 3)
            {
                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;
}