wangguan
2020-12-12 39dc1cad04426eeb07f8c7fc123840c7b31928b7
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
using Core.Utilities;
using System;
using System.Collections.Generic;
using TowerDefense.Level;
using TowerDefense.Towers;
using TowerDefense.UI.HUD;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using KTGMGemClient;
using DG.Tweening;
 
public class EndlessRandomTower : Singleton<EndlessRandomTower>
{
    // 当前类所在的Btn.
    public Button randomBtn;
 
    /// <summary>
    /// 购买二级宝石的按钮贴图.
    /// </summary>
    public Sprite buyBtnLevelUp;
 
    [SerializeField]
    private TextMeshProUGUI cashText;
 
    protected bool bSetBuyLvlUp;
 
    protected bool bCdTimeStart;
 
    protected float btnLvlUpCdTime;
 
    public static bool MAX_INTERNAL = false;
 
    // 玩家初始阵容
    protected List<Tower> towerArray = new List<Tower>();
 
    protected int maxTower = 0;
 
    public static readonly int MAX_TOWERDIS = 5;
 
    // 开始出现技能塔的时间:
    public static float SKILL_TOWER_TIME = 30.0f;
 
    protected System.Random mRandom;
 
    protected int rTowerIdx = 2;
 
    // 所有宝石随机总权重
    private int randomTotalWeight;
 
    // 所有宝石位置权重列表
    private List<int> weightList;
 
    public Text NormalDesc;
 
    public Text SkillDesc;
 
    public ParticleSystem btnPS;
 
    // Start is called before the first frame update
    void Start()
    {
        countDownLimit = JsonDataCenter.GetById<battle>(23).value;
        //Debug.Log("设置了倒计时:" + countDownLimit);
        if (!EndlessLevelManager.instanceExists)
            Debug.LogError("[UI] No level manager for tower list");
 
        foreach (Tower tower in EndlessLevelManager.instance.TowerLibrary)
        {
            towerArray.Add(tower);
        }
 
        maxTower = towerArray.Count;
 
        while (maxTower > MAX_TOWERDIS)
        {
            maxTower--;
            towerArray.RemoveAt(towerArray.Count - 1);
        }
 
        mRandom = new System.Random();
 
        bSetBuyLvlUp = false;
        bCdTimeStart = false;
        randomBtn.onClick.AddListener(delegate () { onClick(EFeatureTower.NULL); });
        UpdateDescDisplay();
    }
 
    public void UpdateDescDisplay()
    {
        int minLevel = GameConfig.IsUpgradeTowerLevel ? 1 : 0;
 
        NormalDesc.text = $"购买{minLevel + 1}级宝石";
    }
 
    public void ChangeBtnClickNormal()
    {
        randomBtn.onClick.RemoveAllListeners();
        randomBtn.onClick.AddListener(delegate () { onClick(EFeatureTower.NULL); });
    }
    public void ChangeBtnClick()
    {
        randomBtn.onClick.RemoveAllListeners();
        randomBtn.onClick.AddListener(delegate () { onClick(EFeatureTower.NULL); });
        randomBtn.onClick.AddListener(() =>
        {
            AudioSourceManager.Ins.Play(AudioEnum.UIDisable);
        });
    }
 
    /// <summary>
    /// 获取TowerList.
    /// </summary>
    public List<Tower> towerList
    {
        get { return this.towerArray; }
    }
 
    public void actionTest()
    {
        multipara((str, strnew) => { Debug.Log(str + strnew); }, "Ying");
    }
 
    protected void actadd(string str, string strnew)
    {
        Debug.Log("这是新增的Action String:" + str);
    }
 
    /// <summary>
    /// 用于测试Action参数的测试函数,Action多参数的用法.
    /// </summary>
    /// <param name="act"></param>
    /// <param name="p1"></param>
    protected void multipara(Action<string, string> act, string p1)
    {
        string finalstr = "Welcome to " + p1;
        act += this.actadd;
        act(finalstr, "YingYing");
    }
 
    /// <summary>
    /// 获取序列化的Tower
    /// </summary>
    /// <returns></returns>
    public Tower getRTower()
    {
        Tower t = this.towerArray[this.rTowerIdx];
        rTowerIdx++;
        if (rTowerIdx >= this.maxTower)
            rTowerIdx = 2;
        return t;
    }
 
    /// <summary>
    /// 根据Tower名字获取当前的Tower指针。
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public Tower getTowerByName(string name)
    {
        for (int ti = 0; ti < this.maxTower; ti++)
            if (name == this.towerArray[ti].towerName)
                return towerArray[ti];
 
        return GetRandomTower(EFeatureTower.NULL);
    }
 
    /// <summary>
    /// 获取随机命中位置索引
    /// </summary>
    private int GetRandomIndex(List<int> list, int totalWeight)
    {
        int random = mRandom.Next(0, totalWeight);
        int index = -1;
        int total = 0;
 
        for (int i = 0; i < list.Count; ++i)
        {
            total += list[i];
            if (total >= random)
            {
                index = i;
                break;
            }
        }
 
        return index;
    }
 
    /// <summary>
    /// 根据规则生成一个随机的塔
    /// </summary>
    /// <returns></returns>
    public Tower GetRandomTower(EFeatureTower towerType, bool isRandom = false)
    {
        int[] indexArr = { 0, 1, 2, 3, 4 };
 
        if (!isRandom)
        {
            if (towerType == EFeatureTower.NULL)
            {
                // 只获得普通宝石
                indexArr = new int[] { 0, 1, 2 };
            }
            else
            {
                // 只获得技能宝石
                indexArr = new int[] { 3, 4 };
            }
        }
 
        int random = UnityEngine.Random.Range(0, indexArr.Length);
 
        return towerArray[indexArr[random]];
    }
 
    /// <summary>
    /// 随机购买Tower的入口,如果购买成功,则需要实时更新价格
    /// </summary>
    public void onClick(EFeatureTower towerType)
    {
        if (GameConfig.IsNewbie)
        {
            AudioSourceManager.Ins.Play(AudioEnum.UI);
            btnPS?.Play();
            return;
        }
 
        if (EndlessGameUI.instance.tdBuyDisable)
        {
            cashText.color = new Color(218f / 255f, 32f / 255f, 32f / 255f);
            DOTween.To(() => cashText.color, (Color v) => cashText.color = v, cashText.color, 0.2f)
                .OnComplete(CheckCurrencyEnough);
            return;
        }
 
        AudioSourceManager.Ins.Play(AudioEnum.UI);
 
        Tower newTower = GetRandomTower(towerType, false);
 
        if (!newTower)
            throw new Exception("未能成功产生Tower");
 
        RandomPlaceTower(newTower, -1, -1, -1, -1, true);
 
        btnPS?.Play();
        //重置倒计时
 
        if (!isCountStartDown)
        {
            needClickPS.Stop();
            isCountStartDown = true;
        }
 
        countDownTime = 0f;
    }
 
 
    private bool isCountStartDown = false;//是否开始倒计时
    public void SetCountDown(bool isOn, bool isReset = false)
    {
        isCountStartDown = isOn;
        if (isReset)
        {
            countDownTime = 0;
            if (needClickPS.isPlaying)
                needClickPS.Stop();
        }
    }
 
    public void SetPS(bool isOn)
    {
        if (needClickPS.gameObject.activeSelf != isOn)
        {
            needClickPS.gameObject.SetActive(isOn);
        }
 
    }
    float countDownTime = 0f;
    float countDownLimit = 5f;
    public ParticleSystem needClickPS;//提示点击按钮
    /// <summary>
    /// This function is called every fixed framerate frame, if the MonoBehaviour is enabled.
    /// </summary>
    void FixedUpdate()
    {
        if (isCountStartDown)
        {
            countDownTime += Time.deltaTime;
            if (countDownTime > countDownLimit)
            {
 
                CheckTower();
                countDownTime = 0;
            }
        }
    }
 
    /// <summary>
    /// 查看是否满足播放按钮提示
    /// </summary>
    private void CheckTower()
    {
        Debug.Log("判断是否提示");
 
        //条件 金币是否满足,合成区是否有位置
        int result;
        int.TryParse(cashText.text.ToString(), out result);
        int current = EndlessLevelManager.instance.Currency.currentCurrency;
        int num = EndlessGameUI.instance.GetTowerNum();
 
        if (current >= result && num < 10)//自己的钱多
        {
            //Debug.Log($"当前金币 :{current}  下一次购买需要金币:{result}  塔的数量:{num}");
            isCountStartDown = false;
            needClickPS.Play();
        }
    }
 
    public void CheckMoney()
    {
        int result;
        int.TryParse(cashText.text.ToString(), out result);
        int current = EndlessLevelManager.instance.Currency.currentCurrency;
        if (current < result && needClickPS.isPlaying)//自己的钱多
        {
            needClickPS.Stop();
            isCountStartDown = true;
            countDownTime = 0;
        }
    }
 
    private void CheckCurrencyEnough()
    {
        int result;
        int.TryParse(cashText.text.ToString(), out result);
        int current = EndlessLevelManager.instance.Currency.currentCurrency;
 
        if (current >= result)
            EndlessGameUI.instance.enableRandomTowerBtn();
        else
            EndlessGameUI.instance.disableRandomTowerBtn();
    }
 
    private List<string> towerNameLis = new List<string>();//用来判断是否是首次购买宝石
 
    /// <summary>
    /// 随机找一个空白位置放置塔防
    /// </summary>
    /// <param name="tower"></param>
    public bool RandomPlaceTower(Tower tower, int level = -1, int cost = -1, int posx = -1, int posy = -1, bool isFirstAppear = false)
    {
        EndlessGameUI gameUI = EndlessGameUI.instance;
 
        if (gameUI.isBuilding)
            gameUI.CancelGhostPlacement();
 
        if (level == -1)
        {
            level = GameConfig.IsUpgradeTowerLevel ? 1 : 0;
        }
 
        return gameUI.RandomPlaceTower(tower, posx, posy, level, cost, false, isFirstAppear);
    }
 
    /// <summary>
    /// 在固定的位置,放置一个随机的塔防类型.
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    public void randomTower(int x, int y, int lvl, string tname = "")
    {
        int tidx = 0;
        Tower targetTower = null;
        if (tname == "")
        {
            tidx = mRandom.Next(0, this.maxTower);
            // 随机升级的Tower,不能升级成为成长骰子.
            int maxLoop = 20;
            while (this.towerArray[tidx].towerFeature != EFeatureTower.NULL)
            {
                tidx = mRandom.Next(0, this.maxTower);
                maxLoop--;
 
                if (maxLoop <= 0)
                {
                    tidx = this.rTowerIdx;
                    rTowerIdx++;
                    if (rTowerIdx >= maxTower)
                        rTowerIdx = 2;
 
                    Debug.LogError("超级循环发生了.");
                    break;
                }
            }
            targetTower = this.towerArray[tidx];
        }
        else
            targetTower = getTowerByName(tname);
 
        // River: 随机找一个空白位置放置塔防。
        EndlessGameUI.instance.RandomPlaceTower(targetTower, x, y, lvl, -1, true);
    }
}