ManaMiniGame.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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, Mathf.Infinity).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.FlowerInfoDic[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. private static int _Score;
  169. private static bool _Game;
  170. private static bool _Pause;
  171. private static bool _Panalty;
  172. private static float _GameTimer;
  173. public static Text TimerLab;
  174. public static Image TimerBk;
  175. public static Award Award;
  176. public static List<Flower> OpList;
  177. public static List<Flower> IdleList;
  178. private static float OpTime;
  179. private static float OpTimer;
  180. private static float GameTime;
  181. private static float PanaltyTime;
  182. private static float PanaltyTimer;
  183. #endregion
  184. private void FixedUpdate()
  185. {
  186. if (!Game || Pause)
  187. {
  188. return;
  189. }
  190. GameTimer -= Time.fixedDeltaTime;
  191. if (GameTimer <= 0)
  192. {
  193. GameOver();
  194. return;
  195. }
  196. if (Panalty)
  197. {
  198. PanaltyTimer -= Time.fixedDeltaTime;
  199. if (PanaltyTimer <= 0)
  200. {
  201. Panalty = false;
  202. }
  203. }
  204. if (IdleList.Count > 0)
  205. {
  206. OpTimer -= Time.fixedDeltaTime;
  207. if (OpTimer <= 0)
  208. {
  209. OpTimer = OpTime;
  210. Flower flower = IdleList[Random.Range(0, IdleList.Count)];
  211. flower.CreateOp(OpList.Count);
  212. OpList.Add(flower);
  213. IdleList.Remove(flower);
  214. }
  215. }
  216. }
  217. public override void RegistValueA()
  218. {
  219. OpTime = 1.5f;
  220. GameTime = 45;
  221. PanaltyTime = 1;
  222. OpTimer = OpTime;
  223. GameTimer = GameTime;
  224. Award = new Award(Data.GetMiniGameConfig());
  225. }
  226. public override void RegistReference()
  227. {
  228. TimerBk = ManaReso.Get<Image>("D_TimerIcon");
  229. TimerLab = ManaReso.Get<Text>("D_TimerLab");
  230. }
  231. #region MiniGame
  232. public static void Rip()
  233. {
  234. if (Panalty)
  235. {
  236. return;
  237. }
  238. if (OpList.Count == 0)
  239. {
  240. return;
  241. }
  242. if (OpList[0].Operate(OpType.Rip))
  243. {
  244. IdleList.Add(OpList[0]);
  245. OpList.Remove(OpList[0]);
  246. ResetOperate();
  247. }
  248. else
  249. {
  250. Panalty = true;
  251. PanaltyTimer = PanaltyTime;
  252. }
  253. }
  254. public static void Water()
  255. {
  256. if (Panalty)
  257. {
  258. return;
  259. }
  260. if (OpList.Count == 0)
  261. {
  262. return;
  263. }
  264. if (OpList[0].Operate(OpType.Water))
  265. {
  266. IdleList.Add(OpList[0]);
  267. OpList.Remove(OpList[0]);
  268. ResetOperate();
  269. }
  270. else
  271. {
  272. Panalty = true;
  273. PanaltyTimer = PanaltyTime;
  274. }
  275. }
  276. public static void Fertilize()
  277. {
  278. if (Panalty)
  279. {
  280. return;
  281. }
  282. if (OpList.Count == 0)
  283. {
  284. return;
  285. }
  286. if (OpList[0].Operate(OpType.Fertilize))
  287. {
  288. IdleList.Add(OpList[0]);
  289. OpList.Remove(OpList[0]);
  290. ResetOperate();
  291. }
  292. else
  293. {
  294. Panalty = true;
  295. PanaltyTimer = PanaltyTime;
  296. }
  297. }
  298. public static void GameOver()
  299. {
  300. Award.GetAward(Score);
  301. GameAbort();
  302. }
  303. public static void GameBegin()
  304. {
  305. ManaReso.Get("D_Rip1").SetActive(true);
  306. ManaReso.Get("D_Water1").SetActive(true);
  307. ManaReso.Get("D_Fertilize1").SetActive(true);
  308. ManaReso.Get("D_Begin").SetActive(false);
  309. Score = 0;
  310. Game = true;
  311. Pause = false;
  312. Panalty = false;
  313. OpTimer = OpTime;
  314. GameTimer = GameTime;
  315. PanaltyTimer = 0;
  316. for (int i = 0; i < IdleList.Count; i++)
  317. {
  318. IdleList[i].GameBegin();
  319. }
  320. }
  321. public static void GameAbort()
  322. {
  323. ManaReso.SetActive("D_Rip1", false);
  324. ManaReso.SetActive("D_Water1", false);
  325. ManaReso.SetActive("D_Fertilize1", false);
  326. ManaReso.SetActive("D_Begin", true);
  327. ManaReso.SetText("Da_Tit", string.Format(Language.GetStr("UI", "Da_Tit1")));
  328. ManaReso.SetText("Da_Lab", string.Format("{0}{1}", Language.GetStr("UI", "Da_Lab1"), Score));
  329. ManaReso.SetActive("Da_Info", true);
  330. ManaReso.SetActive("Da_Quit", false);
  331. ManaReso.SetActive("Da_Cancel", false);
  332. ManaReso.SetActive("Da_GetAward", true);
  333. Score = 0;
  334. Game = false;
  335. Pause = false;
  336. Panalty = false;
  337. OpTimer = OpTime;
  338. GameTimer = GameTime;
  339. PanaltyTimer = 0;
  340. for (int i = 0; i < IdleList.Count; i++)
  341. {
  342. IdleList[i].GameOver();
  343. }
  344. for (int i = 0; i < OpList.Count; i++)
  345. {
  346. OpList[i].GameOver();
  347. }
  348. }
  349. public static void GamePrepare()
  350. {
  351. OpList = new List<Flower>();
  352. IdleList = new List<Flower>();
  353. IdleList.Add(ManaReso.GetFlower(1, false, ManaReso.Get("MiniTra1")));
  354. IdleList.Add(ManaReso.GetFlower(2, false, ManaReso.Get("MiniTra2")));
  355. IdleList.Add(ManaReso.GetFlower(3, false, ManaReso.Get("MiniTra3")));
  356. IdleList.Add(ManaReso.GetFlower(4, false, ManaReso.Get("MiniTra4")));
  357. IdleList.Add(ManaReso.GetFlower(5, false, ManaReso.Get("MiniTra5")));
  358. IdleList.Add(ManaReso.GetFlower(6, false, ManaReso.Get("MiniTra6")));
  359. IdleList.Add(ManaReso.GetFlower(7, false, ManaReso.Get("MiniTra7")));
  360. IdleList.Add(ManaReso.GetFlower(8, false, ManaReso.Get("MiniTra8")));
  361. IdleList.Add(ManaReso.GetFlower(9, false, ManaReso.Get("MiniTra9")));
  362. }
  363. public static void ResetOperate()
  364. {
  365. if (OpList.Count >= 2)
  366. {
  367. OpList[0].SetFirstOp();
  368. OpList[1].SetSecondOp();
  369. }
  370. else if (OpList.Count >= 1)
  371. {
  372. OpList[0].SetFirstOp();
  373. }
  374. }
  375. #endregion
  376. }