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
using LitJson;
using System.Collections.Generic;
using System.Linq;
using TowerDefense.Level;
using UnityEngine;
 
namespace KTGMGemClient
{
    public class MonsterSpeedUp
    {
        /// <summary>
        /// 加速的时间,秒为单位
        /// </summary>
        public float speedTime;
 
        /// <summary>
        /// 场景内出怪的时间速度值
        /// </summary>
        public float spawnTimeScale;
 
        /// <summary>
        /// 怪物移动的速度缩放值
        /// </summary>
        public float moveSpeedScale;
    }
 
    public class JsonDataCenter
    {
 
        public static Dictionary<string, List<tabledata>> allData;
 
        /// <summary>
        /// 怪物加速相关的数据结构
        /// </summary>
        public static List<MonsterSpeedUp> monSpeedUp;
 
        public static Dictionary<int, geminfo> gemInfoDic = new Dictionary<int, geminfo>();
 
        public static Dictionary<int, monster> monsterDic = new Dictionary<int, monster>();
 
        // 金币相关
        public static float GOLD_TIME = 0;
 
        public static int GOLD_ADD = 0;
 
        // 购买塔位后开启倒计时.
        public static float GRIDOPEN_CDTIME = 10.0f;
 
        // 购塔升级开启倒计时时间
        public static float TOWERLVLUP_CDTIME = 10.0f;
 
        // 技能塔的随机概率.
        public static double SKILLTOWER_RATE = 0.2f;
 
        // Svn版本信息.
        public static string SVNVERSION_INFO = "";
 
        public static float DOUBLE_GEM_TIME = 10f;
 
        /// <summary>
        /// 根据id和Level获取skillLevelInfo数据
        /// </summary>
        /// <param name="sid"></param>
        /// <param name="slevel"></param>
        /// <returns></returns>
        public static skilllevelinfo GetSkillLevelInfo(int sid, int slevel)
        {
            List<skilllevelinfo> allSkillLevelInfo = GetList<skilllevelinfo>();
 
            for (int ti = 0; ti < allSkillLevelInfo.Count; ti++)
            {
                skilllevelinfo tinfo = allSkillLevelInfo[ti];
                if ((tinfo.skill == sid) && (tinfo.level == slevel))
                    return tinfo;
            }
            return null;
        }
 
        public static T GetById<T>(int id) where T : class
        {
            string tablename = typeof(T).Name;
            List<tabledata> datas = allData[tablename];
 
            if (datas == null)
            {
                return null;
            }
 
            for (int i = 0; i < datas.Count; i++)
            {
                if (datas[i].getid() == id)
                {
                    return datas[i] as T;
                }
            }
 
            return null;
        }
 
        public static List<T> GetList<T>() where T : class
        {
            string tablename = typeof(T).Name;
 
            return allData[tablename].Cast<T>().ToList();
        }
 
        /// <summary>
        /// 根据ID获取Buff相关的数据。
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static buffinfo GetBuffFromId(int id)
        {
            List<buffinfo> allBuffinfo = GetList<buffinfo>();
 
            for (int ti = 0; ti < allBuffinfo.Count; ti++)
            {
                buffinfo tbuf = allBuffinfo[ti];
                if (tbuf.id == id)
                    return tbuf;
            }
            return null;
        }
 
        /// <summary>
        /// 根据传入的MonsterID和Level数据来获取monsterdata.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="lvl"></param>
        /// <returns></returns>
        public static monster GetMonsterData(int id, int lvl)
        {
            List<monster> alllMonster = GetList<monster>();
 
            for (int ti = 0; ti < alllMonster.Count; ti++)
            {
                monster mon = alllMonster[ti];
                if (mon.id == id && lvl == mon.level)
                    return mon;
            }
            return null;
        }
 
        /// <summary>
        /// 根据索引返回召唤需要的金币数目.
        /// </summary>
        /// <param name="idx"></param>
        /// <returns></returns>
        public static int GetGemCostFromIdx(int idx)
        {
            List<gemcost> allGemCostInfo = GetList<gemcost>();
 
            if (idx >= allGemCostInfo.Count) idx = allGemCostInfo.Count - 1;
            return allGemCostInfo[idx].cost;
        }
 
        /// <summary>
        /// 数据调入完成后,初始化相关的数据。
        /// </summary>
        public static void ProcessInitDataLoad()
        {
            monSpeedUp = new List<MonsterSpeedUp>();
            List<battle> allBattleInfo = JsonDataCenter.GetList<battle>();
 
            for (int ti = 0; ti < allBattleInfo.Count; ti++)
            {
                battle tbat = allBattleInfo[ti];
 
                switch (tbat.key)
                {
                    case "initialgold":
                        // 设置初始化金币.
                        int currency = tbat.value;
 
                        if (LevelManager.instanceExists)
                            LevelManager.instance.ResetCurrency(currency);
 
                        if (OpponentMgr.instanceExists)
                            OpponentMgr.instance.ResetCurrency(currency);
 
                        if (EndlessLevelManager.instanceExists)
                            EndlessLevelManager.instance.ResetCurrency(currency);
                        break;
                    case "skilltime":
                        // 设置技能塔出现时间
                        if (RandomTower.instanceExists)
                            RandomTower.SKILL_TOWER_TIME = tbat.value;
                        else if (EndlessRandomTower.instanceExists)
                            EndlessRandomTower.SKILL_TOWER_TIME = tbat.value;
                        break;
                    case "doublegem":
                        // 购买宝石直接获取2级宝石的时间
                        DOUBLE_GEM_TIME = tbat.value;
                        break;
                    case "buildtime":
                        GRIDOPEN_CDTIME = tbat.value;
                        break;
                    case "gemcountdown":
                        TOWERLVLUP_CDTIME = tbat.value;
                        break;
                    case "skillchance":
                        SKILLTOWER_RATE = tbat.value / 100.0f;
                        break;
                    case "SpeedUpFrist":
                        MonsterSpeedUp tms = new MonsterSpeedUp();
                        string[] tsarr = tbat.strvarr[0].Split(':');
                        if (tsarr.Length != 3)
                            Debug.Log("SpeedUpFrist 数据格式出错了.");
                        else
                        {
                            tms.speedTime = int.Parse(tsarr[0]);
                            tms.spawnTimeScale = float.Parse(tsarr[1]);
                            tms.moveSpeedScale = float.Parse(tsarr[2]);
                            monSpeedUp.Add(tms);
                            //Debug.Log("第一次加速:" + tms.speedTime.ToString() + "," + tms.spawnTimeScale.ToString() + "," + tms.moveSpeedScale.ToString());
                        }
                        break;
                    case "SpeedUpSecond":
                        MonsterSpeedUp tms1 = new MonsterSpeedUp();
                        string[] tsarr1 = tbat.strvarr[0].Split(':');
                        if (tsarr1.Length != 3)
                            Debug.Log("SpeedUpSecond 数据格式出错了.");
                        else
                        {
                            tms1.speedTime = int.Parse(tsarr1[0]);
                            tms1.spawnTimeScale = float.Parse(tsarr1[1]);
                            tms1.moveSpeedScale = float.Parse(tsarr1[2]);
                            monSpeedUp.Add(tms1);
                        }
                        break;
                }
            }
 
            List<geminfo> allGeminfo = GetList<geminfo>();
            foreach (geminfo item in allGeminfo)
            {
                if (!JsonDataCenter.gemInfoDic.ContainsKey(item.id))
                    JsonDataCenter.gemInfoDic.Add(item.id, item);
            }
 
            List<monster> alllMonster = GetList<monster>();
            foreach (monster item in alllMonster)
            {
                if (!JsonDataCenter.monsterDic.ContainsKey(item.id))
                    JsonDataCenter.monsterDic.Add(item.id, item);
            }
 
            List<goldInfo> alllgoldInfo = GetList<goldInfo>();
            foreach (goldInfo item in alllgoldInfo)
            {
                JsonDataCenter.GOLD_ADD = item.gold;
                JsonDataCenter.GOLD_TIME = item.time;
            }
 
            InitData();
        }
 
        /// <summary>
        /// 表数据处理类初始化
        /// </summary>
        private static void InitData()
        {
            EndlessEnemyData.Init();
            EndlessBuffData.Init();
            EndlessBossData.Init();
            EndlessBossSkillData.Init();
            ElfInfoData.Init();
            ElfUpgradeData.Init();
            GameConfig.JsonReadDone = true;
        }
    }
}