ManaData.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. using LitJson;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using System;
  5. using System.Xml;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. public class ManaData : MonoBehaviour
  9. {
  10. #region 变量
  11. public static int Level
  12. {
  13. get { return _Level; }
  14. set
  15. {
  16. _Level = value;
  17. for (int i = 0; i < SkillList.Count; i++)
  18. {
  19. SkillList[i].OnLevelChange();
  20. }
  21. ManaLog.Log(string.Format("已升级 花园等级 : <color=red>{0}</color>", _Level));
  22. }
  23. }
  24. public static float Person
  25. {
  26. get { return _Person; }
  27. set
  28. {
  29. _Person = value;
  30. ManaReso.SetText("F_PersonLab", _Person.ToString("0"));
  31. }
  32. }
  33. public static float CoinPerson
  34. {
  35. get { return _CoinPerson; }
  36. set
  37. {
  38. _CoinPerson = value;
  39. ManaReso.SetText("F_CoinPersonLab", _CoinPerson.ToString("0"));
  40. }
  41. }
  42. public static double Coin
  43. {
  44. get { return _Coin; }
  45. set
  46. {
  47. _Coin = value;
  48. ManaReso.SetText("F_CoinLab", _Coin.ToString("0"));
  49. ManaReso.SetText("C_CoinLab", _Coin.ToString("0"));
  50. }
  51. }
  52. public static double Diamond
  53. {
  54. get { return _Diamond; }
  55. set
  56. {
  57. _Diamond = value;
  58. ManaReso.SetText("F_DiamondLab", _Diamond.ToString("0"));
  59. }
  60. }
  61. private static int _Level;
  62. private static float _Person;
  63. private static float _CoinPerson;
  64. private static double _Coin;
  65. private static double _Diamond;
  66. public static float SkillPlus;
  67. public static float SkillPerson;
  68. public static float SkillPersonBuff;
  69. public static float SkillCoinPerson;
  70. public static bool SkillBar;
  71. public static bool Connect;
  72. public static float OpTime;
  73. public static float CircleTime;
  74. public static float OpTimer;
  75. public static float CircleTimer;
  76. public static float NewPerson;
  77. public static float NewCoinPerson;
  78. public static List<Skill> CoolList;
  79. public static List<SkillRoot> SkillList;
  80. public static List<SkillRoot> UsingList;
  81. public static Dictionary<string, SkillRoot> SkillDic;
  82. #endregion
  83. private void Awake()
  84. {
  85. Initializer.RegistValue += RegistValue;
  86. #region 读技能配置
  87. SkillDic = new Dictionary<string, SkillRoot>();
  88. SkillList = new List<SkillRoot>();
  89. List<XmlAttributeCollection> attributesList = Data.GetSkillConfig();
  90. for (int i = 0; i < attributesList.Count; i++)
  91. {
  92. SkillRoot skillRoot;
  93. #region MyRegion
  94. if (attributesList[i].Count == 17)
  95. {
  96. skillRoot = new Pack(attributesList[i]);
  97. }
  98. else if (attributesList[i].Count == 23)
  99. {
  100. skillRoot = new Ability(attributesList[i]);
  101. }
  102. else if (attributesList[i].Count == 33)
  103. {
  104. if (string.IsNullOrEmpty(attributesList[i][4].Value))
  105. {
  106. skillRoot = new Skill(attributesList[i]);
  107. }
  108. else
  109. {
  110. skillRoot = new BigSkill(attributesList[i]);
  111. }
  112. }
  113. else
  114. {
  115. throw new Exception(attributesList[i].Count.ToString());
  116. }
  117. #endregion
  118. SkillList.Add(skillRoot);
  119. SkillDic.Add(skillRoot.Name, skillRoot);
  120. }
  121. SkillList.Sort(SkillRoot.SortByClassID);
  122. #endregion
  123. #region 创建技能条
  124. for (int i = 0; i < SkillList.Count; i++)
  125. {
  126. if (SkillList[i].Tab != SkillTab.Null)
  127. {
  128. ManaReso.GetSkillItem(SkillList[i]);
  129. }
  130. }
  131. #endregion
  132. }
  133. private void Update()
  134. {
  135. if (Math.Abs(Time.timeScale) < 0.0005f)
  136. {
  137. return;
  138. }
  139. if (Input.anyKeyDown)
  140. {
  141. OpTimer = 0;
  142. if (ManaReso.Get("GardenMini").gameObject.activeSelf || ManaReso.Get("F_Manage").gameObject.activeSelf)
  143. {
  144. return;
  145. }
  146. GameObject go = ManaReso.Get("C_Main").gameObject;
  147. if (go.activeSelf == false)
  148. {
  149. go.transform.TweenForCG();
  150. }
  151. }
  152. else
  153. {
  154. OpTimer += Time.deltaTime;
  155. if (OpTimer >= OpTime)
  156. {
  157. OpTimer = 0;
  158. GameObject go = ManaReso.Get("C_Main").gameObject;
  159. if (go.activeSelf)
  160. {
  161. go.transform.TweenBacCG();
  162. }
  163. }
  164. }
  165. }
  166. private void FixedUpdate()
  167. {
  168. #region 使用技能
  169. for (int i = 0; i < UsingList.Count; i++)
  170. {
  171. if (UsingList[i].DoUse())
  172. {
  173. i--;
  174. }
  175. }
  176. #endregion
  177. #region 冷却技能
  178. for (int i = 0; i < CoolList.Count; i++)
  179. {
  180. if (CoolList[i].DoCool())
  181. {
  182. i--;
  183. }
  184. }
  185. #endregion
  186. #region 计算参观收入
  187. CircleTimer -= Time.deltaTime;
  188. if (CircleTimer < 0)
  189. {
  190. CircleTimer = CircleTime;
  191. NewPerson = Person*(1 + SkillPersonBuff) + SkillPerson;
  192. NewCoinPerson = CoinPerson + SkillCoinPerson;
  193. Coin += (NewPerson*NewCoinPerson*CircleTime)*(1 + SkillPlus);
  194. ManaLog.Log(string.Format("参观收益<color=red>{0:0}</color> = <color=red>{1}</color> * <color=red>{2}</color> * <color=red>{3}</color> * <color=red>{4}</color> (人次*金币*时间*加成)", (NewPerson * NewCoinPerson * CircleTime) * (1 + SkillPlus), NewPerson, NewCoinPerson, CircleTime, 1 + SkillPlus));
  195. }
  196. #endregion
  197. }
  198. private void RegistValue()
  199. {
  200. OpTime = 5;
  201. CircleTime = 10;
  202. CircleTimer = 10;
  203. CoolList = new List<Skill>();
  204. UsingList = new List<SkillRoot>();
  205. #region 读技能存档
  206. double elapse = DateTime.Now.Subtract(DateTime.Parse(Data.PlayerNode.SelectSingleNode("QuitTime").Attributes[0].Value)).TotalSeconds;
  207. XmlNodeList xmlNodeList = Data.PlayerNode.SelectSingleNode("SkillList").ChildNodes;
  208. List<SkillRoot> ffCoolList = new List<SkillRoot>();
  209. List<List<SkillRoot>> ffList = new List<List<SkillRoot>>();
  210. if (elapse > CircleTimer)
  211. {
  212. int ffCircle = 1 + Mathf.FloorToInt((float)((elapse - CircleTimer) / CircleTime));
  213. for (int i = 0; i < ffCircle; i++)
  214. {
  215. ffList.Add(new List<SkillRoot>());
  216. }
  217. }
  218. for (int i = 0; i < xmlNodeList.Count; i++)
  219. {
  220. if (xmlNodeList[i].Attributes[0].Value == SkillType.Skill.ToString())
  221. {
  222. Skill skill = (Skill)SkillDic[xmlNodeList[i].Name];
  223. skill.RegistReference();
  224. skill.Level = int.Parse(xmlNodeList[i].Attributes[2].Value);
  225. skill.CoolTimer = float.Parse(xmlNodeList[i].Attributes[3].Value);
  226. skill.UsingTimer = float.Parse(xmlNodeList[i].Attributes[4].Value);
  227. skill._ItemStatus = (SkillStatus)Enum.Parse(typeof(SkillStatus), xmlNodeList[i].Attributes[1].Value);
  228. skill.RegistValue(elapse, ffList);
  229. }
  230. else if (xmlNodeList[i].Attributes[0].Value == SkillType.BigSkill.ToString())
  231. {
  232. BigSkill bigSkill = (BigSkill)SkillDic[xmlNodeList[i].Name];
  233. bigSkill.RegistReference();
  234. bigSkill.Level = int.Parse(xmlNodeList[i].Attributes[3].Value);
  235. bigSkill.CoolTimer = float.Parse(xmlNodeList[i].Attributes[4].Value);
  236. bigSkill.UsingTimer = float.Parse(xmlNodeList[i].Attributes[5].Value);
  237. bigSkill._ItemStatus = (SkillStatus)Enum.Parse(typeof(SkillStatus), xmlNodeList[i].Attributes[1].Value); //Sa0
  238. bigSkill._BarStatus = (SkillStatus)Enum.Parse(typeof(SkillStatus), xmlNodeList[i].Attributes[2].Value); //Sa1
  239. bigSkill.RegistValue(elapse, ffList);
  240. }
  241. else if (xmlNodeList[i].Attributes[0].Value == SkillType.Pack.ToString())
  242. {
  243. Pack pack = (Pack)SkillDic[xmlNodeList[i].Attributes[1].Value];
  244. pack.RegistReference();
  245. pack.Level = int.Parse(xmlNodeList[i].Attributes[3].Value);
  246. pack._ItemStatus = (SkillStatus)Enum.Parse(typeof(SkillStatus), xmlNodeList[i].Attributes[2].Value);
  247. pack.RegistValue(elapse, ffList);
  248. }
  249. else if (xmlNodeList[i].Attributes[0].Value == SkillType.Ability.ToString())
  250. {
  251. Ability ability = (Ability)SkillDic[xmlNodeList[i].Name];
  252. ability.RegistReference();
  253. ability.Level = int.Parse(xmlNodeList[i].Attributes[2].Value);
  254. ability._ItemStatus = (SkillStatus)Enum.Parse(typeof(SkillStatus), xmlNodeList[i].Attributes[1].Value);
  255. ability.RegistValue(elapse, ffList);
  256. }
  257. else
  258. {
  259. throw new Exception();
  260. }
  261. }
  262. #endregion
  263. #region 读花朵存档
  264. XmlAttributeCollection attributes = Data.PlayerNode.SelectSingleNode("FlowerList").Attributes;
  265. if (string.IsNullOrEmpty(attributes[0].Value) == false)
  266. {
  267. string[] strings = attributes[0].Value.Split(' ');
  268. for (int i = 0; i < strings.Length; i++)
  269. {
  270. if (string.IsNullOrEmpty(strings[i]))
  271. {
  272. continue;
  273. }
  274. int id = int.Parse(strings[i]);
  275. ManaGarden.FlowerDic[id].Unlock = true;
  276. }
  277. }
  278. attributes = Data.PlayerNode.SelectSingleNode("PlantList").Attributes;
  279. for (int i = 0; i < attributes.Count; i++)
  280. {
  281. int id = int.Parse(attributes[i].Value);
  282. ManaGarden.PlaceFlower(id, ManaReso.Get(attributes[i].Name));
  283. }
  284. #endregion
  285. #region 读数据存档
  286. Level = int.Parse(Data.PlayerNode.SelectSingleNode("Level").Attributes[0].Value);
  287. Coin = double.Parse(Data.PlayerNode.SelectSingleNode("Coin").Attributes[0].Value);
  288. Diamond = double.Parse(Data.PlayerNode.SelectSingleNode("Diamond").Attributes[0].Value);
  289. #endregion
  290. #region 计算离线收入
  291. double collectCoin = 0;
  292. if (ffList.Count > 0)
  293. {
  294. for (int i = 0; i < ffList.Count; i++)
  295. {
  296. for (int j = 0; j < ffCoolList.Count; j++)
  297. {
  298. if (ffCoolList[j].SkillType == SkillType.Skill || ffCoolList[j].SkillType == SkillType.BigSkill)
  299. {
  300. if (i == 0)
  301. {
  302. ((Skill) ffCoolList[j]).CoolTimer -= CircleTimer;
  303. }
  304. else
  305. {
  306. ((Skill)ffCoolList[j]).CoolTimer -= CircleTime;
  307. }
  308. }
  309. }
  310. for (int j = 0; j < ffList[i].Count; j++)
  311. {
  312. ffList[i][j].Annul();
  313. ffCoolList.Add(ffList[i][j]);
  314. }
  315. NewPerson = Person * (1 + SkillPersonBuff) + SkillPerson;
  316. NewCoinPerson = CoinPerson + SkillCoinPerson;
  317. collectCoin += (NewPerson * NewCoinPerson * CircleTime) * (1 + SkillPlus);
  318. }
  319. Coin += collectCoin;
  320. ManaReso.SetText("A_IconLab", collectCoin.ToString("0"));
  321. CircleTimer = (float)((elapse - CircleTimer) % CircleTime);
  322. }
  323. else
  324. {
  325. CircleTimer = (float)(CircleTimer - elapse);
  326. ManaReso.SetText("A_IconLab", "0");
  327. }
  328. #endregion
  329. }
  330. public static bool Pay(double amt, Current current)
  331. {
  332. amt = (long) amt;
  333. if (current == Current.Free)
  334. {
  335. return true;
  336. }
  337. else if (current == Current.AD)
  338. {
  339. return true;
  340. }
  341. else if (current == Current.Cash)
  342. {
  343. return true;
  344. }
  345. else if (current == Current.Coin)
  346. {
  347. if (Coin >= amt)
  348. {
  349. Coin -= amt;
  350. return true;
  351. }
  352. else
  353. {
  354. ManaLog.Log("您没有足够的金币");
  355. return false;
  356. }
  357. }
  358. else if (current == Current.Diamond)
  359. {
  360. if (Diamond >= amt)
  361. {
  362. Diamond -= amt;
  363. return true;
  364. }
  365. else
  366. {
  367. ManaLog.Log("您没有足够的钻石");
  368. return false;
  369. }
  370. }
  371. else
  372. {
  373. throw new Exception(current.ToString());
  374. }
  375. }
  376. }