ManaMiniGame.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Serialization;
  4. using System;
  5. using System.Xml;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using Random = UnityEngine.Random;
  9. public class Award
  10. {
  11. #region 变量
  12. public int DiamondMin;
  13. public int DiamondMax;
  14. public string CoinFml;
  15. public string FlowerFml;
  16. public string DiamondFml;
  17. public List<float> Odds;
  18. public List<float> Standard;
  19. #endregion
  20. public Award(XmlAttributeCollection attribute)
  21. {
  22. CoinFml = attribute[1].Value;
  23. FlowerFml = attribute[4].Value;
  24. DiamondFml = attribute[3].Value;
  25. string[] strings = attribute[2].Value.Split(',');
  26. DiamondMin = int.Parse(strings[0]);
  27. DiamondMax = int.Parse(strings[1]);
  28. strings = attribute[5].Value.Split(',');
  29. Odds = new List<float>()
  30. {
  31. float.Parse(strings[0]),
  32. float.Parse(strings[1]),
  33. float.Parse(strings[2]),
  34. };
  35. strings = attribute[6].Value.Split(',');
  36. Standard = new List<float>()
  37. {
  38. float.Parse(strings[0]),
  39. float.Parse(strings[1]),
  40. float.Parse(strings[2]),
  41. };
  42. }
  43. public void GetAward(int score)
  44. {
  45. ManaReso.SetActive("Da_Flower", false);
  46. ManaReso.SetActive("Da_Diamond", false);
  47. int coin = (int) Auxiliary.FmlParse(CoinFml, "s", score.ToString());
  48. ManaData.Coin += coin;
  49. ManaReso.SetText("Da_CoinLab", coin.ToString());
  50. float diamondRate = (float) Auxiliary.FmlParse(DiamondFml, "l", Mathf.Clamp(ManaData.Level, 1, 1000).ToString());
  51. if (Random.Range(0, 1f) <= diamondRate)
  52. {
  53. ManaReso.SetActive("Da_Diamond", true);
  54. int diamond = (int) Mathf.Lerp(DiamondMin, DiamondMax, Random.Range(0, 1f));
  55. ManaReso.SetText("Da_DiamondLab", diamond.ToString());
  56. ManaData.Diamond += diamond;
  57. }
  58. int grade;
  59. if (score < Standard[0])
  60. {
  61. grade = 0;
  62. }
  63. else if (score < Standard[1])
  64. {
  65. grade = 1;
  66. }
  67. else if (score < Standard[2])
  68. {
  69. grade = 2;
  70. }
  71. else
  72. {
  73. throw new Exception();
  74. }
  75. float flowerRate = (float) Auxiliary.FmlParse(DiamondFml, "l", ManaData.Level.ToString(), "f", ManaGarden.MyFlower.ToString());
  76. if (Random.Range(0, 1f) <= flowerRate)
  77. {
  78. if (Random.Range(0, 1f) <= Odds[grade])
  79. {
  80. ManaReso.SetActive("Da_Flower", true);
  81. FlowerInfo flowerInfo;
  82. while (true)
  83. {
  84. flowerInfo = ManaGarden.FlowerInfoList[Random.Range(0, ManaGarden.TotalFlower - 1)];
  85. if (flowerInfo.Unlock)
  86. {
  87. break;
  88. }
  89. }
  90. ManaReso.SetText("Da_FlowerLab", Language.GetStr("FlowerName", "Flower" + flowerInfo.Id));
  91. }
  92. }
  93. }
  94. }
  95. public class ManaMiniGame : Regist
  96. {
  97. #region 变量
  98. public static int Score
  99. {
  100. get { return _Score; }
  101. set
  102. {
  103. _Score = value;
  104. ManaReso.SetText("D_ScoreLab", _Score.ToString());
  105. }
  106. }
  107. public static bool Game
  108. {
  109. get { return _Game; }
  110. set
  111. {
  112. _Game = value;
  113. if (_Game)
  114. {
  115. ManaReso.SetText("D_StatusLab", Language.GetStr("UI", "D_StatusLab1"));
  116. }
  117. else
  118. {
  119. ManaReso.SetText("D_StatusLab", Language.GetStr("UI", "D_StatusLab0"));
  120. }
  121. }
  122. }
  123. public static bool Pause
  124. {
  125. get { return _Pause; }
  126. set
  127. {
  128. _Pause = value;
  129. if (Game)
  130. {
  131. if (_Pause)
  132. {
  133. ManaReso.SetText("D_StatusLab", Language.GetStr("UI", "D_StatusLab2"));
  134. }
  135. else
  136. {
  137. ManaReso.SetText("D_StatusLab", Language.GetStr("UI", "D_StatusLab1"));
  138. }
  139. }
  140. }
  141. }
  142. public static bool Panalty
  143. {
  144. get { return _Panalty; }
  145. set
  146. {
  147. _Panalty = value;
  148. if (_Panalty)
  149. {
  150. ManaReso.SetText("D_StatusLab", Language.GetStr("UI", "D_StatusLab3"));
  151. }
  152. else
  153. {
  154. ManaReso.SetText("D_StatusLab", Language.GetStr("UI", "D_StatusLab1"));
  155. }
  156. }
  157. }
  158. public static float GameTimer
  159. {
  160. get { return _GameTimer; }
  161. set
  162. {
  163. _GameTimer = value;
  164. TimerBk.fillAmount = _GameTimer / GameTimer;
  165. TimerLab.text = _GameTimer.ToString("0.0");
  166. }
  167. }
  168. public static float PrepareTimer
  169. {
  170. get { return _PrepareTimer; }
  171. set
  172. {
  173. _PrepareTimer = value;
  174. BtnLab.text = PrepareStr + " " + Mathf.CeilToInt(_PrepareTimer);
  175. }
  176. }
  177. private static int _Score;
  178. private static bool _Game;
  179. private static bool _Pause;
  180. private static bool _Panalty;
  181. private static float _GameTimer;
  182. private static float _PrepareTimer;
  183. public static string PrepareStr;
  184. public static Text BtnLab;
  185. public static Text TimerLab;
  186. public static Image TimerBk;
  187. public static Award Award;
  188. public static List<Flower> OpList;
  189. public static List<Flower> IdleList;
  190. private static float OpTime;
  191. private static float OpTimer;
  192. private static float GameTime;
  193. private static float PanaltyTime;
  194. private static float PanaltyTimer;
  195. #endregion
  196. private void FixedUpdate()
  197. {
  198. if (PrepareTimer > 0)
  199. {
  200. PrepareTimer -= Time.fixedDeltaTime;
  201. if (PrepareTimer <= 0)
  202. {
  203. GameBegin();
  204. }
  205. }
  206. if (!Game || Pause)
  207. {
  208. return;
  209. }
  210. GameTimer -= Time.fixedDeltaTime;
  211. if (GameTimer <= 0)
  212. {
  213. GameOver();
  214. return;
  215. }
  216. if (Panalty)
  217. {
  218. PanaltyTimer -= Time.fixedDeltaTime;
  219. if (PanaltyTimer <= 0)
  220. {
  221. Panalty = false;
  222. }
  223. }
  224. if (IdleList.Count > 0)
  225. {
  226. OpTimer -= Time.fixedDeltaTime;
  227. if (OpTimer <= 0)
  228. {
  229. OpTime -= OpTime * 0.02f;
  230. OpTimer = OpTime;
  231. Flower flower = IdleList[Random.Range(0, IdleList.Count)];
  232. flower.CreateOp(OpList.Count);
  233. OpList.Add(flower);
  234. IdleList.Remove(flower);
  235. }
  236. }
  237. }
  238. public override void RegistValueA()
  239. {
  240. PrepareStr = Language.GetStr("UI", "D_BeginLab1");
  241. OpTime = 1.5f;
  242. GameTime = 45;
  243. PanaltyTime = 1;
  244. OpTimer = OpTime;
  245. GameTimer = GameTime;
  246. Award = new Award(Data.GetAwardConfig());
  247. }
  248. public override void RegistReference()
  249. {
  250. TimerBk = ManaReso.Get<Image>("D_TimerIcon");
  251. BtnLab = ManaReso.Get<Text>("D_BeginLab");
  252. TimerLab = ManaReso.Get<Text>("D_TimerLab");
  253. }
  254. #region MiniGame
  255. public static void Rip()
  256. {
  257. if (Panalty)
  258. {
  259. return;
  260. }
  261. if (OpList.Count == 0)
  262. {
  263. return;
  264. }
  265. if (OpList[0].Operate(OpType.Rip))
  266. {
  267. IdleList.Add(OpList[0]);
  268. OpList.Remove(OpList[0]);
  269. ResetOperate();
  270. }
  271. else
  272. {
  273. Panalty = true;
  274. PanaltyTimer = PanaltyTime;
  275. }
  276. }
  277. public static void Water()
  278. {
  279. if (Panalty)
  280. {
  281. return;
  282. }
  283. if (OpList.Count == 0)
  284. {
  285. return;
  286. }
  287. if (OpList[0].Operate(OpType.Water))
  288. {
  289. IdleList.Add(OpList[0]);
  290. OpList.Remove(OpList[0]);
  291. ResetOperate();
  292. }
  293. else
  294. {
  295. Panalty = true;
  296. PanaltyTimer = PanaltyTime;
  297. }
  298. }
  299. public static void Fertilize()
  300. {
  301. if (Panalty)
  302. {
  303. return;
  304. }
  305. if (OpList.Count == 0)
  306. {
  307. return;
  308. }
  309. if (OpList[0].Operate(OpType.Fertilize))
  310. {
  311. IdleList.Add(OpList[0]);
  312. OpList.Remove(OpList[0]);
  313. ResetOperate();
  314. }
  315. else
  316. {
  317. Panalty = true;
  318. PanaltyTimer = PanaltyTime;
  319. }
  320. }
  321. public static void GameOver()
  322. {
  323. Award.GetAward(Score);
  324. ManaReso.SetActive("Da_Info", true);
  325. ManaReso.SetActive("Da_Quit", false);
  326. ManaReso.SetActive("Da_Cancel", false);
  327. ManaReso.SetActive("Da_GetAward", true);
  328. GameAbort();
  329. }
  330. public static void GameBegin()
  331. {
  332. ManaReso.Get("D_Rip1").SetActive(true);
  333. ManaReso.Get("D_Water1").SetActive(true);
  334. ManaReso.Get("D_Fertilize1").SetActive(true);
  335. ManaReso.Get("D_Begin").SetActive(false);
  336. Score = 0;
  337. Game = true;
  338. Pause = false;
  339. Panalty = false;
  340. OpTimer = OpTime;
  341. GameTimer = GameTime;
  342. PanaltyTimer = 0;
  343. for (int i = 0; i < IdleList.Count; i++)
  344. {
  345. IdleList[i].GameBegin();
  346. }
  347. }
  348. public static void GameAbort()
  349. {
  350. ManaReso.SetActive("D_Rip1", false);
  351. ManaReso.SetActive("D_Water1", false);
  352. ManaReso.SetActive("D_Fertilize1", false);
  353. ManaReso.SetActive("D_Begin", true);
  354. ManaReso.SetText("Da_Tit", string.Format(Language.GetStr("UI", "Da_Tit1")));
  355. ManaReso.SetText("Da_Lab", string.Format("{0}{1}", Language.GetStr("UI", "Da_Lab1"), Score));
  356. Score = 0;
  357. Game = false;
  358. Pause = false;
  359. Panalty = false;
  360. OpTimer = OpTime;
  361. GameTimer = GameTime;
  362. PanaltyTimer = 0;
  363. for (int i = 0; i < IdleList.Count; i++)
  364. {
  365. IdleList[i].GameOver();
  366. }
  367. for (int i = 0; i < OpList.Count; i++)
  368. {
  369. OpList[i].GameOver();
  370. }
  371. }
  372. public static void GamePrepare()
  373. {
  374. OpList = new List<Flower>();
  375. IdleList = new List<Flower>();
  376. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[0], ManaReso.Get("SlotMini1")));
  377. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[1], ManaReso.Get("SlotMini2")));
  378. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[2], ManaReso.Get("SlotMini3")));
  379. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[3], ManaReso.Get("SlotMini4")));
  380. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[4], ManaReso.Get("SlotMini5")));
  381. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[5], ManaReso.Get("SlotMini6")));
  382. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[6], ManaReso.Get("SlotMini7")));
  383. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[7], ManaReso.Get("SlotMini8")));
  384. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[8], ManaReso.Get("SlotMini9")));
  385. }
  386. public static void ResetOperate()
  387. {
  388. if (OpList.Count >= 2)
  389. {
  390. OpList[0].SetFirstOp();
  391. OpList[1].SetSecondOp();
  392. }
  393. else if (OpList.Count >= 1)
  394. {
  395. OpList[0].SetFirstOp();
  396. }
  397. }
  398. #endregion
  399. }