接下来,我们完成强化界面的升级部分。

每次升级的花费需要配置好,我们再修改一下游戏的配置。增加升级花费的条目“upgrades”

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
{
"planes": [
{
//...
"upgrades": {
"name": "升级",
"coefficient": 2,
"max": 4,
"0": 100,
"1": 200,
"2": 300,
"3": 400
}
},
{
//...
"upgrades": {
"name": "升级",
"coefficient": 3,
"max": 4,
"0": 150,
"1": 300,
"2": 450,
"3": 600
}
},
{
//...
"upgrades": {
"name": "升级",
"coefficient": 4,
"max": 4,
"0": 200,
"1": 400,
"2": 600,
"3": 800
}
},
{
//...
"upgrades": {
"name": "升级",
"coefficient": 5,
"max": 4,
"0": 300,
"1": 600,
"2": 900,
"3": 1200
}
}
]
}

coefficient:升级后属性提升的系数

max:飞机的最大等级

1、2、3、4:每次升级后所花费的钻石数量。

清理一下DataMgr.Instance.ClearAll();重置一下配置。

键值读取

在之前的配置解析过程中,每个键值我们使用的是枚举再声明一次键值,那是因为之前的配置需要全部解析一遍,而“upgrades”部分只需要查询一次即可,所以我们换个比较直接的方式读取键值。

DataKeys

修改DataKeys,添加upgrades的键值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class DataKeys 
{
//...
public const string NAME = "name";

/// <summary>
/// 升级
/// </summary>
public const string UPGRADES = "upgrades";
/// <summary>
/// 升级提升比率
/// </summary>
public const string UPGRADES_RATIO = UPGRADES + "coefficient";//直接加父键值
/// <summary>
/// 最大等级
/// </summary>
public const string LEVEL_MAX = UPGRADES + "max";
}

KeysUtil

之前KeyUtil的两个方法逻辑有重复的地方,都引用了planeId,所以修改KeyUtil的逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class KeysUtil
{
public static string GetPropertyKeys(int id, string name)
{
return id + name;
}
public static string GetPropertyKeys(string name)
{
int id = GameStateModel.Instance.SelectedPlaneId;
return id + name;
}
public static string GetPropertyItemKey(PropertyItem.ItemKey itemKey,string propertyName)
{
return GetPropertyKeys(propertyName + itemKey);
}
}

Money

修改UIUtilUIUtilData,添加修改Text组件内容的方法

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
public class UIUtilData
{
//...
public void SetText(int content)
{
SetText(content.ToString());
}
public void SetText(float content)
{
SetText(content.ToString());
}
public void SetText(string content)
{
if (Text != null)
{
Text.text = content;
}
else if (TextMeshProUGUI != null)
{
TextMeshProUGUI.text = content;
}
else
{
Debug.LogError("当前物体没有Text组件:" + Go.name);
}
}
}

在View文件夹下新建Money脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Money : ViewBase
{
protected override void InitChild()
{
}
public override void UpdateFun()
{
//Money是子View,不需要调用base.UpdateFun()
//这里直接读取值,我们没有MoneyController
UIUtil.Get("Star/BG/Text").SetText(DataMgr.Instance.Get<int>(DataKeys.STAR).ToString());
UIUtil.Get("Diamond/BG/Text").SetText(DataMgr.Instance.Get<int>(DataKeys.DIAMOND).ToString());
}
}

StrengthenView中动态挂载Money

1
2
3
4
5
protected override void InitChild()
{
//...
UIUtil.Get("Money").Go.AddComponent<Money>();
}

升级按钮

升级按钮的显示逻辑也写在StrengthenView

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
[BindPrefab(Pathes.STRENGTHEN_VIEW,Consts.BIND_PREFAB_PRIORITY_VIEW)]
public class StrengthenView : ViewBase
{
protected override void InitChild()
{
UIUtil.Get("Switchplayer").Go.AddComponent<SwitchPlayer>();
UIUtil.Get("Property").Go.AddComponent<PlaneProperty>();
UIUtil.Get("Money").Go.AddComponent<Money>();
}
public override void UpdateFun()
{
base.UpdateFun();
UpdateLevelView();
}
private void UpdateLevelView()
{
//设置升级按钮的名称
string key = KeysUtil.GetPropertyKeys(DataKeys.UPGRADES + DataKeys.NAME);
string data = DataMgr.Instance.Get<string>(key);
UIUtil.Get("Upgrades/Text").SetText(data);
//花费
key = KeysUtil.GetPropertyKeys(DataKeys.LEVEL);
int level = DataMgr.Instance.Get<int>(key);//从存档中获取当前等级
key = KeysUtil.GetPropertyKeys(DataKeys.UPGRADES + level);
int cost = DataMgr.Instance.Get<int>(key);//获取当前升级花费
UIUtil.Get("Upgrades/Upgrades/Text").SetText(cost);
}
}

星星和钻石

接下来解决升级按钮扣星星钻石的功能。

首先在强化界面中,升级按钮扣的是钻石,而加点按钮扣的是星星,我们需要再修改一下配置的json文件。

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
"planeId": 0,
"level": 0,
"attackTime": 1,
"attack": {
"name": "攻击",
"value": 5,
"costUnit": "star",//+++
"cost": 200,
"grouth": 10,
"maxValue": 500
},
"fireRate": {
"name": "攻速",
"value": 80,
"costUnit": "star",//+++
"cost": 200,
"grouth": 1,
"maxValue": 500
},
"life": {
"name": "生命",
"value": 100,
"costUnit": "star",//+++
"cost": 200,
"grouth": 50,
"maxValue": 1000
},
"upgrades": {
"name": "升级",
"coefficient": 2,
"max": 4,
"0": 100,
"1": 200,
"2": 300,
"3": 400,
"costUnit": "diamond"//+++
}

其余的部分也按照上面的模式添加即可,这里省略。

配置对应的DataKeys变量

1
2
3
4
/// <summary>
/// 花费单位(星星或钻石)
/// </summary>
public const string COST_UNIT = "costUnit";

GameStateModel

将Level、Money(星星和钻石)的获取和设置都转移到GameStateModel

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
public class GameStateModel : NormalSingleton<GameStateModel>
{
public int SelectedPlaneId { get; set; }

public int Level
{
get
{
string key = KeysUtil.GetPropertyKeys(DataKeys.LEVEL);
return DataMgr.Instance.Get<int>(key);
}
}

public int GetMoney(string costUnitKey)
{
int money = 0;
switch (costUnitKey)
{
case DataKeys.STAR:
money = DataMgr.Instance.Get<int>(DataKeys.STAR);
break;
case DataKeys.DIAMOND:
money = DataMgr.Instance.Get<int>(DataKeys.DIAMOND);
break;
default:
UnityEngine.Debug.LogError("没有发现指定的货币键值:" + costUnitKey);
break;
}
return money;
}
public void SetMoney(string costUnitKey, int money)
{
switch (costUnitKey)
{
case DataKeys.STAR:
DataMgr.Instance.Set<int>(DataKeys.STAR, money);
break;
case DataKeys.DIAMOND:
DataMgr.Instance.Set<int>(DataKeys.DIAMOND, money);
break;
default:
UnityEngine.Debug.LogError("没有发现指定的货币键值:" + costUnitKey);
break;
}
}
}

StrengthenController

我们的升级按钮在强化界面内,所以需要在StrengthenController里面添加按钮的listener

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

[BindPrefab(Pathes.STRENGTHEN_VIEW,Consts.BIND_PREFAB_PRIORITY_CONTROLLER)]
public class StrengthenController : ControllerBase
{
protected override void InitChild()
{
transform.Find("Switchplayer").gameObject.AddComponent<SwitchPlayerController>();
transform.Find("Property").gameObject.AddComponent<PlanePropertyController>();
transform.ButtonAction("Upgrades/Upgrades", Upgrades);
transform.ButtonAction("Back", UIMgr.Instance.Back);
}
private void Upgrades()
{
//判断是否能够升级
//花费是否足够,等级是否已经是最大
string key = KeysUtil.GetPropertyKeys(DataKeys.UPGRADES + DataKeys.COST_UNIT);
string costUnit = DataMgr.Instance.Get<string>(key);

key = KeysUtil.GetPropertyKeys(DataKeys.UPGRADES + GameStateModel.Instance.Level);
int cost = DataMgr.Instance.Get<int>(key);

int money = GameStateModel.Instance.GetMoney(costUnit);

int levelMax = DataMgr.Instance.Get<int>(DataKeys.LEVEL_MAX);

if (money >= cost && GameStateModel.Instance.Level < levelMax)
{
ChangeMoney(costUnit, cost);
ChangeLevel();
//赠点,生命值和攻击力的某些属性根据比率翻倍
ChangeData();
}
}
private void ChangeMoney(string costUnit,int cost)
{
int money = GameStateModel.Instance.GetMoney(costUnit);
GameStateModel.Instance.SetMoney(costUnit,money - cost);
}
private void ChangeLevel()
{
//升级
string levelKey = KeysUtil.GetPropertyKeys(DataKeys.LEVEL);
int level = GameStateModel.Instance.Level;
level++;
DataMgr.Instance.Set(levelKey, level);
}
private void ChangeData()
{
//获取升级系数,修改数据
string key = KeysUtil.GetPropertyKeys(DataKeys.UPGRADES_RATIO);
int ratio = DataMgr.Instance.Get<int>(key);

ChangeData(ratio, PropertyItem.ItemKey.value, PlaneProperty.Property.attack);
ChangeData(ratio, PropertyItem.ItemKey.maxValue, PlaneProperty.Property.attack);
ChangeData(ratio, PropertyItem.ItemKey.grouth, PlaneProperty.Property.attack);
ChangeData(ratio, PropertyItem.ItemKey.value, PlaneProperty.Property.life);
ChangeData(ratio, PropertyItem.ItemKey.maxValue, PlaneProperty.Property.life);
ChangeData(ratio, PropertyItem.ItemKey.grouth, PlaneProperty.Property.life);
}
private void ChangeData(int ratio, PropertyItem.ItemKey itemKey, PlaneProperty.Property planeProperty)
{
string key = KeysUtil.GetPropertyItemKey(itemKey,planeProperty.ToString());
int value = DataMgr.Instance.Get<int>(key);
value *= ratio;
DataMgr.Instance.Set(key, value);
}
}

PropertyItemController

提升属性的按钮,也要进行钱币的判断,我们补充上这里的逻辑

PropertyItemController中添加ChangeData方法,转移之前的AddAction方法并补充判断逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private void AddAction()
{
string key = KeysUtil.GetPropertyKeys(_key + DataKeys.COST_UNIT);
string costUnit = DataMgr.Instance.Get<string>(key);
int money = GameStateModel.Instance.GetMoney(costUnit);

key = KeysUtil.GetPropertyItemKey(ItemKey.cost, _key);
int cost = GetValue(key);

if (cost <= money)
{
ChangeData();
}
}
private void ChangeData()
{
string valueKey = KeysUtil.GetPropertyItemKey(ItemKey.value, _key);
int value = GetValue(valueKey);
string grouthKey = KeysUtil.GetPropertyItemKey(ItemKey.grouth, _key);
int grouth = GetValue(grouthKey);
value += grouth;

DataMgr.Instance.SetObject(valueKey, value);
}