ManaMiniGame.cs 12 KB

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