River Jiang
2020-10-21 9ecea6fd1dc5c53d670f7e0400210522bda5586d
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
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using TowerDefense.Level;
using TowerDefense.Towers;
using UnityEngine;
using UnityEngine.UI;
 
 
public struct TowerLevelUp
{
    public string towerName;
    public int newLvl;
}
 
public class SceneTowerLvl : MonoBehaviour
{
    /// <summary>
    /// 当前的等级.
    /// </summary>
    public int currentLvl = 1;
    /// <summary>
    /// 升级需要的现金.
    /// </summary>
    public int upgradeCash = 100;
 
    public TextMeshProUGUI lvTextNew;
    public TextMeshProUGUI cashTextNew;
    public Image cashIcon;
 
    /// <summary>
    /// 是否可交互.
    /// </summary>
    public bool canInteract = true;
 
    /// <summary>
    /// 游戏场景内的TowerLvl升级
    /// </summary>
    public event Action<TowerLevelUp> actionSceneTowerLvlUp;
 
    /// <summary>
    /// 当前数据对应的场景内Tower.
    /// </summary>
    protected Tower towerData;
 
    /// <summary>
    /// 己方唯一的数据,定位战斗中防塔的局内等级。
    /// </summary>
    protected static Dictionary<string, int> inSceneTowerLvl = new Dictionary<string, int>();
 
    /// <summary>
    /// 初始化当前数据的TowerData.
    /// </summary>
    /// <param name="td"></param>
    public void SetTowerData( Tower td)
    {
        this.towerData = td;
        // 初始化局内等级数据为零.
        td.inSceneTowerLevel = 0;
        this.GetComponent<Image>().sprite = td.uiImage;
    }
 
    public void enableSceneTower( bool enable )
    {
        Button btn = this.GetComponent<Button>();
        if (btn)
        {
            // 所有的子结点灰掉或者恢复使用
            btn.interactable = enable;
            this.canInteract = enable;
 
            Color32 tc;
            if (!enable)
            {
                tc = new Color32(120, 120, 120, 255);
            } else
                tc = new Color32(255, 255, 255, 255); 
            this.lvTextNew.faceColor = tc;
            this.cashTextNew.faceColor = tc;
            this.cashIcon.color = tc;
        }
    }
 
    /// <summary>
    /// 根据名字获取局塔防等级。
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public static int getInSceneTowerLvl( string name,bool opponent = false )
    {
        if( !opponent)
        {
            var dic = SceneTowerLvl.inSceneTowerLvl;
            if (dic.ContainsKey(name))
                return dic[name];
        }
        else
        {
            return 0;
        }
 
        return 0;
    }
 
    /// <summary>
    /// 清空局内升级的塔防数据
    /// </summary>
    public static void clearInSceneTowerData()
    {
        inSceneTowerLvl.Clear();
    }
 
    /// <summary>
    /// 场景内点击某一个Tower类型并升级
    /// </summary>
    public void OnLvlUp()
    {
        this.currentLvl++;
        lvTextNew.text = "Lv:" + this.currentLvl.ToString();
 
        var dic = SceneTowerLvl.inSceneTowerLvl;
        if( dic.ContainsKey( towerData.towerName ))
        {
            dic[towerData.towerName] = currentLvl;
        }
        else
        {
            dic.Add(towerData.towerName, currentLvl);
        }
 
        // 更新全局的Cash.
        if (!LevelManager.instance.currency.TryPurchase(upgradeCash))
            return;
 
        // 更新当前的Cash
        this.upgradeCash += 100;
        cashTextNew.text = this.upgradeCash.ToString();
 
        // 安全执行升级事件.
        if ( this.actionSceneTowerLvlUp != null )
        {
            TowerLevelUp tlu;
            tlu.towerName = towerData.towerName;
            tlu.newLvl = currentLvl;
            this.actionSceneTowerLvlUp( tlu );
        }
    }
 
}