ManaData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. GameObject go = ManaReso.Get("C_Main").gameObject;
  143. if (go.activeSelf == false)
  144. {
  145. go.transform.TweenForCG();
  146. }
  147. }
  148. else
  149. {
  150. OpTimer += Time.deltaTime;
  151. if (OpTimer >= OpTime)
  152. {
  153. OpTimer = 0;
  154. GameObject go = ManaReso.Get("C_Main").gameObject;
  155. if (go.activeSelf)
  156. {
  157. go.transform.TweenBacCG();
  158. }
  159. }
  160. }
  161. }
  162. private void FixedUpdate()
  163. {
  164. #region 使用技能
  165. for (int i = 0; i < UsingList.Count; i++)
  166. {
  167. if (UsingList[i].DoUse())
  168. {
  169. i--;
  170. }
  171. }
  172. #endregion
  173. #region 冷却技能
  174. for (int i = 0; i < CoolList.Count; i++)
  175. {
  176. if (CoolList[i].DoCool())
  177. {
  178. i--;
  179. }
  180. }
  181. #endregion
  182. #region 计算参观收入
  183. CircleTimer -= Time.deltaTime;
  184. if (CircleTimer < 0)
  185. {
  186. CircleTimer = CircleTime;
  187. NewPerson = Person*(1 + SkillPersonBuff) + SkillPerson;
  188. NewCoinPerson = CoinPerson + SkillCoinPerson;
  189. Coin += (NewPerson*NewCoinPerson*CircleTime)*(1 + SkillPlus);
  190. 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));
  191. }
  192. #endregion
  193. }
  194. private void RegistValue()
  195. {
  196. OpTime = 5;
  197. CircleTime = 10;
  198. CircleTimer = 10;
  199. CoolList = new List<Skill>();
  200. UsingList = new List<SkillRoot>();
  201. #region 读技能存档
  202. double elapse = DateTime.Now.Subtract(DateTime.Parse(Data.PlayerNode.SelectSingleNode("QuitTime").Attributes[0].Value)).TotalSeconds;
  203. XmlNodeList xmlNodeList = Data.PlayerNode.SelectSingleNode("SkillList").ChildNodes;
  204. List<List<SkillRoot>> collectList = new List<List<SkillRoot>>();
  205. if (elapse > CircleTimer)
  206. {
  207. int lastCircle = 1 + Mathf.FloorToInt((float)((elapse - CircleTimer) / CircleTime));
  208. for (int i = 0; i < lastCircle; i++)
  209. {
  210. collectList.Add(new List<SkillRoot>());
  211. }
  212. }
  213. for (int i = 0; i < xmlNodeList.Count; i++)
  214. {
  215. if (xmlNodeList[i].Attributes[0].Value == SkillType.Skill.ToString())
  216. {
  217. Skill skill = (Skill)SkillDic[xmlNodeList[i].Name];
  218. skill.RegistReference();
  219. skill.Level = int.Parse(xmlNodeList[i].Attributes[2].Value);
  220. skill.CoolTimer = float.Parse(xmlNodeList[i].Attributes[3].Value);
  221. skill.UsingTimer = float.Parse(xmlNodeList[i].Attributes[4].Value);
  222. skill._ItemStatus = (SkillStatus)Enum.Parse(typeof(SkillStatus), xmlNodeList[i].Attributes[1].Value);
  223. skill.RegistValue(elapse, collectList);
  224. }
  225. else if (xmlNodeList[i].Attributes[0].Value == SkillType.BigSkill.ToString())
  226. {
  227. BigSkill bigSkill = (BigSkill)SkillDic[xmlNodeList[i].Name];
  228. bigSkill.RegistReference();
  229. bigSkill.Level = int.Parse(xmlNodeList[i].Attributes[3].Value);
  230. bigSkill.CoolTimer = float.Parse(xmlNodeList[i].Attributes[4].Value);
  231. bigSkill.UsingTimer = float.Parse(xmlNodeList[i].Attributes[5].Value);
  232. bigSkill._ItemStatus = (SkillStatus)Enum.Parse(typeof(SkillStatus), xmlNodeList[i].Attributes[1].Value); //Sa0
  233. bigSkill._BarStatus = (SkillStatus)Enum.Parse(typeof(SkillStatus), xmlNodeList[i].Attributes[2].Value); //Sa1
  234. bigSkill.RegistValue(elapse, collectList);
  235. }
  236. else if (xmlNodeList[i].Attributes[0].Value == SkillType.Pack.ToString())
  237. {
  238. Pack pack = (Pack)SkillDic[xmlNodeList[i].Attributes[1].Value];
  239. pack.RegistReference();
  240. pack.Level = int.Parse(xmlNodeList[i].Attributes[3].Value);
  241. pack._ItemStatus = (SkillStatus)Enum.Parse(typeof(SkillStatus), xmlNodeList[i].Attributes[2].Value);
  242. pack.RegistValue(elapse, collectList);
  243. }
  244. else if (xmlNodeList[i].Attributes[0].Value == SkillType.Ability.ToString())
  245. {
  246. Ability ability = (Ability)SkillDic[xmlNodeList[i].Name];
  247. ability.RegistReference();
  248. ability.Level = int.Parse(xmlNodeList[i].Attributes[2].Value);
  249. ability._ItemStatus = (SkillStatus)Enum.Parse(typeof(SkillStatus), xmlNodeList[i].Attributes[1].Value);
  250. ability.RegistValue(elapse, collectList);
  251. }
  252. else
  253. {
  254. throw new Exception();
  255. }
  256. }
  257. #endregion
  258. #region 读花朵存档
  259. XmlAttributeCollection attributes = Data.PlayerNode.SelectSingleNode("FlowerList").Attributes;
  260. if (string.IsNullOrEmpty(attributes[0].Value) == false)
  261. {
  262. string[] strings = attributes[0].Value.Split(' ');
  263. for (int i = 0; i < strings.Length; i++)
  264. {
  265. if (string.IsNullOrEmpty(strings[i]))
  266. {
  267. continue;
  268. }
  269. int id = int.Parse(strings[i]);
  270. ManaGarden.FlowerDic[id].Unlock = true;
  271. }
  272. }
  273. attributes = Data.PlayerNode.SelectSingleNode("PlantList").Attributes;
  274. for (int i = 0; i < attributes.Count; i++)
  275. {
  276. int id = int.Parse(attributes[i].Value);
  277. ManaGarden.PlaceFlower(id, ManaReso.Get(attributes[i].Name));
  278. }
  279. #endregion
  280. #region 读数据存档
  281. Level = int.Parse(Data.PlayerNode.SelectSingleNode("Level").Attributes[0].Value);
  282. Coin = double.Parse(Data.PlayerNode.SelectSingleNode("Coin").Attributes[0].Value);
  283. Diamond = double.Parse(Data.PlayerNode.SelectSingleNode("Diamond").Attributes[0].Value);
  284. #endregion
  285. #region 计算离线收入
  286. double collectCoin = 0;
  287. if (collectList.Count > 0)
  288. {
  289. for (int i = 0; i < collectList.Count; i++)
  290. {
  291. for (int j = 0; j < collectList[i].Count; j++)
  292. {
  293. collectList[i][j].Annul();
  294. }
  295. NewPerson = Person * (1 + SkillPersonBuff) + SkillPerson;
  296. NewCoinPerson = CoinPerson + SkillCoinPerson;
  297. collectCoin += (NewPerson * NewCoinPerson * CircleTime) * (1 + SkillPlus);
  298. }
  299. Coin += collectCoin;
  300. ManaReso.SetText("A_IconLab", collectCoin.ToString("0"));
  301. CircleTimer = (float)((elapse - CircleTimer) % CircleTime);
  302. }
  303. else
  304. {
  305. CircleTimer = (float)(CircleTimer - elapse);
  306. ManaReso.SetText("A_IconLab", "0");
  307. }
  308. #endregion
  309. }
  310. public static bool Pay(double amt, Current current)
  311. {
  312. amt = (long) amt;
  313. if (current == Current.Free)
  314. {
  315. return true;
  316. }
  317. else if (current == Current.AD)
  318. {
  319. return true;
  320. }
  321. else if (current == Current.Cash)
  322. {
  323. return true;
  324. }
  325. else if (current == Current.Coin)
  326. {
  327. if (Coin >= amt)
  328. {
  329. Coin -= amt;
  330. return true;
  331. }
  332. else
  333. {
  334. ManaLog.Log("您没有足够的金币");
  335. return false;
  336. }
  337. }
  338. else if (current == Current.Diamond)
  339. {
  340. if (Diamond >= amt)
  341. {
  342. Diamond -= amt;
  343. return true;
  344. }
  345. else
  346. {
  347. ManaLog.Log("您没有足够的钻石");
  348. return false;
  349. }
  350. }
  351. else
  352. {
  353. throw new Exception(current.ToString());
  354. }
  355. }
  356. }