ManaMiniGame.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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 (Pause)
  230. {
  231. return;
  232. }
  233. if (Prepare)
  234. {
  235. PrepareTimer -= Time.fixedDeltaTime;
  236. if (PrepareTimer <= 0)
  237. {
  238. GameBegin();
  239. }
  240. }
  241. if (Game == false)
  242. {
  243. return;
  244. }
  245. GoldTimer -= Time.fixedDeltaTime;
  246. if (GoldTimer < 0)
  247. {
  248. GoldTimer = Random.Range(3f, 6f);
  249. GoldList.Add(ManaReso.GetGold());
  250. }
  251. if (DropDiamond)
  252. {
  253. DIamondTimer -= Time.fixedDeltaTime;
  254. if (DIamondTimer < 0)
  255. {
  256. DropDiamond = false;
  257. DiamondList.Add(ManaReso.GetDiamond());
  258. }
  259. }
  260. GameTimer -= Time.fixedDeltaTime;
  261. if (GameTimer <= 0)
  262. {
  263. GameOver();
  264. return;
  265. }
  266. if (Panalty)
  267. {
  268. PanaltyTimer -= Time.fixedDeltaTime;
  269. if (PanaltyTimer <= 0)
  270. {
  271. Panalty = false;
  272. }
  273. }
  274. if (IdleList.Count > 0)
  275. {
  276. OpTimer -= Time.fixedDeltaTime;
  277. if (OpTimer <= 0)
  278. {
  279. OpTime -= OpTime * 0.03f;
  280. OpTimer = OpTime;
  281. Flower flower = IdleList[Random.Range(0, IdleList.Count)];
  282. flower.CreateOp(OpList.Count);
  283. OpList.Add(flower);
  284. IdleList.Remove(flower);
  285. }
  286. }
  287. }
  288. public override void RegistValueA()
  289. {
  290. PrepareStr = Language.GetStr("UI", "D_BeginLab1");
  291. OpTime = 1.5f;
  292. GameTime = 45;
  293. PanaltyTime = 1;
  294. OpTimer = OpTime;
  295. GameTimer = GameTime;
  296. Award = new Award(Data.GetAwardConfig());
  297. }
  298. public override void RegistReference()
  299. {
  300. TimerIcon = ManaReso.Get<Image>("D_TimerIcon");
  301. BtnLab = ManaReso.Get<Text>("D_BeginLab");
  302. TimerLab = ManaReso.Get<Text>("D_TimerLab");
  303. }
  304. #region MiniGame
  305. public static void Rip()
  306. {
  307. if (Panalty)
  308. {
  309. return;
  310. }
  311. if (OpList.Count == 0)
  312. {
  313. return;
  314. }
  315. if (OpList[0].Operate(OpType.Rip))
  316. {
  317. IdleList.Add(OpList[0]);
  318. OpList.Remove(OpList[0]);
  319. ResetOperate();
  320. }
  321. else
  322. {
  323. Panalty = true;
  324. PanaltyTimer = PanaltyTime;
  325. }
  326. }
  327. public static void Water()
  328. {
  329. if (Panalty)
  330. {
  331. return;
  332. }
  333. if (OpList.Count == 0)
  334. {
  335. return;
  336. }
  337. if (OpList[0].Operate(OpType.Water))
  338. {
  339. IdleList.Add(OpList[0]);
  340. OpList.Remove(OpList[0]);
  341. ResetOperate();
  342. }
  343. else
  344. {
  345. Panalty = true;
  346. PanaltyTimer = PanaltyTime;
  347. }
  348. }
  349. public static void Fertilize()
  350. {
  351. if (Panalty)
  352. {
  353. return;
  354. }
  355. if (OpList.Count == 0)
  356. {
  357. return;
  358. }
  359. if (OpList[0].Operate(OpType.Fertilize))
  360. {
  361. IdleList.Add(OpList[0]);
  362. OpList.Remove(OpList[0]);
  363. ResetOperate();
  364. }
  365. else
  366. {
  367. Panalty = true;
  368. PanaltyTimer = PanaltyTime;
  369. }
  370. }
  371. public static void GameOver()
  372. {
  373. Award.GetAward(Score);
  374. ManaReso.SetActive("Da_Info", true);
  375. ManaReso.SetActive("Da_Quit", false);
  376. ManaReso.SetActive("Da_Cancel", false);
  377. ManaReso.SetActive("Da_GetAward", true);
  378. GameAbort();
  379. }
  380. public static void GameBegin()
  381. {
  382. MiniGameAmt++;
  383. GoldList = new List<DropGold>();
  384. DiamondList = new List<DropDiamond>();
  385. GoldTimer = Random.Range(3f, 6f);
  386. DIamondTimer = Random.Range(0f, 40f);
  387. Award.BonusCoin = 0;
  388. Award.BonusDiamond = 0;
  389. ManaDebug.Log(string.Format("第<color=red>{0}</color>次小游戏", MiniGameAmt));
  390. if (Random.Range(5, 9) <= MiniGameAmt)
  391. {
  392. MiniGameAmt = 0;
  393. DropDiamond = true;
  394. }
  395. else
  396. {
  397. if (Random.Range(0, 1f) <= 0.01f)
  398. {
  399. DropDiamond = true;
  400. }
  401. else
  402. {
  403. DropDiamond = false;
  404. }
  405. }
  406. ManaReso.Get("D_Rip1").SetActive(true);
  407. ManaReso.Get("D_Water1").SetActive(true);
  408. ManaReso.Get("D_Fertilize1").SetActive(true);
  409. ManaReso.Get("D_Begin").SetActive(false);
  410. Score = 0;
  411. Game = true;
  412. Pause = false;
  413. Panalty = false;
  414. Prepare = false;
  415. OpTimer = OpTime;
  416. GameTimer = GameTime;
  417. PanaltyTimer = 0;
  418. for (int i = 0; i < IdleList.Count; i++)
  419. {
  420. IdleList[i].GameBegin();
  421. }
  422. }
  423. public static void GameAbort()
  424. {
  425. ManaGarden.AwardLock = false;
  426. for (int i = 0; i < GoldList.Count; i++)
  427. {
  428. GoldList[i].Retrieve();
  429. }
  430. for (int i = 0; i < DiamondList.Count; i++)
  431. {
  432. DiamondList[i].Retrieve();
  433. }
  434. if (Game)
  435. {
  436. ManaData.Mini = false;
  437. ManaData.MiniTimer = Mathf.Lerp(3, 5, Random.Range(0, 1f));
  438. ManaReso.Get("C_MiniGame").TweenBacCG();
  439. ManaDebug.Log(string.Format("<color=red>{0:0}</color>秒后激活小游戏", ManaData.MiniTimer));
  440. }
  441. ManaReso.SetActive("D_Rip1", false);
  442. ManaReso.SetActive("D_Water1", false);
  443. ManaReso.SetActive("D_Fertilize1", false);
  444. ManaReso.SetActive("D_Begin", true);
  445. ManaReso.SetText("Da_Tit", string.Format(Language.GetStr("UI", "Da_Tit1")));
  446. ManaReso.SetText("Da_Lab", string.Format("{0}{1}", Language.GetStr("UI", "Da_Lab1"), Score));
  447. Score = 0;
  448. Game = false;
  449. Pause = false;
  450. Panalty = false;
  451. Prepare = false;
  452. OpTimer = OpTime;
  453. GameTimer = GameTime;
  454. PanaltyTimer = 0;
  455. for (int i = 0; i < IdleList.Count; i++)
  456. {
  457. IdleList[i].GameOver();
  458. }
  459. for (int i = 0; i < OpList.Count; i++)
  460. {
  461. OpList[i].GameOver();
  462. }
  463. }
  464. public static void GamePrepare()
  465. {
  466. ManaReso.SetText("Da_CoinLab", "");
  467. ManaReso.SetText("Da_FlowerLab", "");
  468. ManaReso.SetText("Da_DiamondLab", "");
  469. ManaReso.SetActive("Da_Coin", true);
  470. ManaReso.SetActive("Da_Flower", true);
  471. ManaReso.SetActive("Da_Diamond", true);
  472. ManaGarden.AwardLock = true;
  473. OpList = new List<Flower>();
  474. IdleList = new List<Flower>();
  475. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[0], ManaReso.Get("SlotMini1")));
  476. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[1], ManaReso.Get("SlotMini2")));
  477. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[2], ManaReso.Get("SlotMini3")));
  478. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[3], ManaReso.Get("SlotMini4")));
  479. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[4], ManaReso.Get("SlotMini5")));
  480. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[5], ManaReso.Get("SlotMini6")));
  481. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[6], ManaReso.Get("SlotMini7")));
  482. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[7], ManaReso.Get("SlotMini8")));
  483. IdleList.Add(ManaReso.GetFlower(ManaGarden.FlowerInfoList[8], ManaReso.Get("SlotMini9")));
  484. }
  485. public static void ResetOperate()
  486. {
  487. if (OpList.Count >= 2)
  488. {
  489. OpList[0].SetFirstOp();
  490. OpList[1].SetSecondOp();
  491. }
  492. else if (OpList.Count >= 1)
  493. {
  494. OpList[0].SetFirstOp();
  495. }
  496. }
  497. #endregion
  498. }