ManaMiniGame.cs 15 KB

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