ManaData.cs 13 KB

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