wangguan
2020-11-06 f6abd3f706541c72633daa84ba2b47ba1a003d02
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
using DG.Tweening;
using KTGMGemClient;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using TowerDefense.Level;
using TowerDefense.Towers.Placement;
using UnityEngine;
using UnityEngine.UI;
 
/// <summary>
/// 当前类用于开启新的TowerPlacementGrid。当前类附加在对应的购买开启按钮上。
/// </summary>
public class TowerGridOpen : MonoBehaviour
{
    // 当前Button对应的Cash Text数据
    public TextMeshProUGUI cashText;
 
    // 当前Button对应的image.
    public Image mImage;
 
    // 当前的Grid按钮对应的xy坐标.
    protected int gridX;
    protected int gridY;
 
    /// <summary>
    /// 是否处于等待开启的状态.
    /// </summary>
    protected bool bWaitOpen;
    protected float waitOpenCDTime;
 
    // 对应放置TowerGrid的类.
    protected TowerPlacementGrid towerGrid;
 
 
    // Start is called before the first frame update
    void Start()
    {
        bWaitOpen = false;
    }
 
    public void SetBuyBtnInfo(int x, int y, TowerPlacementGrid tpg)
    {
        this.gridX = x;
        this.gridY = y;
        this.towerGrid = tpg;
    }
 
    /// <summary>
    /// 当前类附加的按钮被点击,需要开启当前按钮所在的塔位.
    /// </summary>
    public void OnClick()
    {
        // 等待开启的时候不能再次点击购买了
        if (bWaitOpen) return;
 
        if (this.towerGrid != null)
        {
            float cashToOpen = TowerPlacementGrid.GRID_OPENCASH_SELF;
            bool opponent = towerGrid.opponent;
            if (opponent)
            {
                cashToOpen = TowerPlacementGrid.GRID_OPENCASH_OPPO;
                if (OpponentMgr.instance.currency.currentCurrency < cashToOpen)
                    return;
            }
            else
            {
                if (LevelManager.instance.currency.currentCurrency < cashToOpen)
                    return;
            }
 
            // 减少Cash并开启相应塔防管理器的兵线
            bool bsucc = false;
            if (opponent)
                bsucc = OpponentMgr.instance.currency.TryPurchase((int)cashToOpen);
            else
                bsucc = LevelManager.instance.currency.TryPurchase((int)cashToOpen);
 
            if (bsucc)
            {
                towerGrid.updateGridOpenCoin(gridX, gridY);
                if (mImage)
                    mImage.color = new Color(1.0f, 1.0f, 1.0f, 0.0f);
 
                bWaitOpen = true;
                waitOpenCDTime = JsonDataCenter.GRIDOPEN_CDTIME;
                Vector3 tpos = cashText.transform.position;
                tpos.y += 20;
                cashText.transform.position = tpos;
                SetWaitOpenTime();
            }
        }
 
    }
 
    /// <summary>
    /// 删除当前的界面.
    /// </summary>
    public void Release()
    {
        GameObject.Destroy(this.gameObject);
    }
 
    protected void SetWaitOpenTime()
    {
        string distime = ((int)Math.Ceiling(waitOpenCDTime)).ToString() + "s";
        cashText.text = distime;
    }
 
    // Update is called once per frame
    void Update()
    {
        if (bWaitOpen)
        {
            waitOpenCDTime -= Time.deltaTime;
            if (waitOpenCDTime <= 0)
            {
                // 购买当前的格子.
                towerGrid.BuyTowerGrid(gridX, gridY);
                Release();
                return;
            }
            SetWaitOpenTime();
        }
    }
}