ManaMiniGame.cs 14 KB

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