ManaMiniGame.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Serialization;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using Random = UnityEngine.Random;
  8. public class ManaMiniGame : MonoBehaviour
  9. {
  10. #region 变量
  11. #region Game
  12. public static int Score
  13. {
  14. get { return _Score; }
  15. set
  16. {
  17. _Score = value;
  18. ScoreLab.text = _Score.ToString();
  19. }
  20. }
  21. public static float Timer
  22. {
  23. get { return _Timer; }
  24. set
  25. {
  26. _Timer = value;
  27. TimerBk.fillAmount = _Timer / GameTime;
  28. TimerLab.text = _Timer.ToString("0.0");
  29. }
  30. }
  31. public static bool Game
  32. {
  33. get { return _Game; }
  34. set
  35. {
  36. _Game = value;
  37. if (_Game)
  38. {
  39. StatusLab.text = "进行中";
  40. }
  41. else
  42. {
  43. StatusLab.text = "未开始";
  44. }
  45. }
  46. }
  47. public static bool Pause
  48. {
  49. get { return _Pause; }
  50. set
  51. {
  52. _Pause = value;
  53. if (Game)
  54. {
  55. if (_Pause)
  56. {
  57. StatusLab.text = "已暂停";
  58. }
  59. else
  60. {
  61. StatusLab.text = "进行中";
  62. }
  63. }
  64. }
  65. }
  66. public static bool Panalty
  67. {
  68. get { return _Panalty; }
  69. set
  70. {
  71. _Panalty = value;
  72. if (_Panalty)
  73. {
  74. StatusLab.text = "操作被冻结";
  75. }
  76. else
  77. {
  78. StatusLab.text = "进行中";
  79. }
  80. }
  81. }
  82. private static int _Score;
  83. private static float _Timer;
  84. private static bool _Game;
  85. private static bool _Pause;
  86. private static bool _Panalty;
  87. public static List<Flower> OpList;
  88. public static List<Flower> IdleList;
  89. private static float OpTime;
  90. private static float GameTime;
  91. private static float PenaltyTime;
  92. private static float OpTimer;
  93. private static float PenaltyTimer;
  94. private static Text ScoreLab;
  95. private static Text TimerLab;
  96. private static Text StatusLab;
  97. private static Image TimerBk;
  98. #endregion
  99. #endregion
  100. private void Awake()
  101. {
  102. Initializer.RegistValue += RegistValue;
  103. Initializer.RegistReference += RegistReference;
  104. }
  105. private void FixedUpdate()
  106. {
  107. #region 小游戏A
  108. if (!Game || Pause)
  109. {
  110. return;
  111. }
  112. Timer -= Time.fixedDeltaTime; //小游戏计时
  113. if (Timer <= 0)
  114. {
  115. GameOver();
  116. return; //小游戏结束
  117. }
  118. if (PenaltyTimer > 0) //冻结时间计时
  119. {
  120. PenaltyTimer -= Time.fixedDeltaTime;
  121. }
  122. OpTimer -= Time.fixedDeltaTime; //生成操作计时
  123. if (OpTimer <= 0)
  124. {
  125. if (IdleList.Count > 0)
  126. {
  127. OpTimer = OpTime;
  128. Flower flower = IdleList[Random.Range(0, IdleList.Count)];
  129. flower.CreateOp(OpList.Count);
  130. IdleList.Remove(flower);
  131. OpList.Add(flower);
  132. }
  133. }
  134. #endregion
  135. }
  136. private static void RegistValue()
  137. {
  138. OpTime = 1.5f;
  139. GameTime = 30;
  140. PenaltyTime = 1;
  141. Timer = GameTime;
  142. OpTimer = OpTime;
  143. }
  144. private static void RegistReference()
  145. {
  146. TimerBk = ManaReso.Get<Image>("D_TimerIcon");
  147. TimerLab = ManaReso.Get<Text>("D_TimerLab");
  148. ScoreLab = ManaReso.Get<Text>("D_ScoreLab");
  149. StatusLab = ManaReso.Get<Text>("D_StatusLab");
  150. }
  151. #region 小游戏A
  152. public static void Rip()
  153. {
  154. if (PenaltyTimer > 0) //冻结操作
  155. {
  156. return;
  157. }
  158. if (OpList.Count > 0)
  159. {
  160. if (OpList[0].Operate(OpType.Rip)) //操作正确
  161. {
  162. IdleList.Add(OpList[0]);
  163. OpList.Remove(OpList[0]);
  164. SetOpStatus();
  165. }
  166. else //操作错误
  167. {
  168. PenaltyTimer = PenaltyTime;
  169. ManaLog.Log(string.Format("惩罚<color=red>{0:0}</color>秒", PenaltyTime));
  170. }
  171. }
  172. }
  173. public static void Water()
  174. {
  175. if (PenaltyTimer > 0) //冻结操作
  176. {
  177. return;
  178. }
  179. if (OpList.Count > 0)
  180. {
  181. if (OpList[0].Operate(OpType.Water)) //操作正确
  182. {
  183. IdleList.Add(OpList[0]);
  184. OpList.Remove(OpList[0]);
  185. SetOpStatus();
  186. }
  187. else //操作错误
  188. {
  189. PenaltyTimer = PenaltyTime;
  190. ManaLog.Log(string.Format("惩罚<color=red>{0:0}</color>秒", PenaltyTime));
  191. }
  192. }
  193. }
  194. public static void Fertilize()
  195. {
  196. if (PenaltyTimer > 0) //冻结操作
  197. {
  198. return;
  199. }
  200. if (OpList.Count > 0)
  201. {
  202. if (OpList[0].Operate(OpType.Fertilize)) //操作正确
  203. {
  204. IdleList.Add(OpList[0]);
  205. OpList.Remove(OpList[0]);
  206. SetOpStatus();
  207. }
  208. else //操作错误
  209. {
  210. PenaltyTimer = PenaltyTime;
  211. ManaLog.Log(string.Format("惩罚<color=red>{0:0}</color>秒", PenaltyTime));
  212. }
  213. }
  214. }
  215. public static void GameBegin()
  216. {
  217. ManaReso.Get("D_Rip1").SetActive(true);
  218. ManaReso.Get("D_Water1").SetActive(true);
  219. ManaReso.Get("D_Fertilize1").SetActive(true);
  220. ManaReso.Get("D_Begin").SetActive(false);
  221. Score = 0;
  222. Game = true;
  223. Pause = false;
  224. Timer = GameTime;
  225. for (int i = 0; i < IdleList.Count; i++)
  226. {
  227. IdleList[i].GameBegin();
  228. }
  229. OpList = new List<Flower>();
  230. }
  231. public static void GameAbort()
  232. {
  233. ManaReso.Get("D_Rip1").SetActive(false);
  234. ManaReso.Get("D_Water1").SetActive(false);
  235. ManaReso.Get("D_Fertilize1").SetActive(false);
  236. ManaReso.Get("D_Begin").SetActive(true);
  237. Score = 0;
  238. Game = false;
  239. Pause = false;
  240. Timer = GameTime;
  241. OpTimer = OpTime;
  242. for (int i = 0; i < IdleList.Count; i++)
  243. {
  244. IdleList[i].GameOver();
  245. }
  246. for (int i = 0; i < OpList.Count; i++)
  247. {
  248. OpList[i].GameOver();
  249. }
  250. }
  251. public static void GamePrepare()
  252. {
  253. IdleList = new List<Flower>();
  254. OpList = new List<Flower>();
  255. IdleList.Add(ManaReso.GetFlower(1, false, ManaReso.Get("MiniTra1")));
  256. IdleList.Add(ManaReso.GetFlower(2, false, ManaReso.Get("MiniTra2")));
  257. IdleList.Add(ManaReso.GetFlower(3, false, ManaReso.Get("MiniTra3")));
  258. IdleList.Add(ManaReso.GetFlower(4, false, ManaReso.Get("MiniTra4")));
  259. IdleList.Add(ManaReso.GetFlower(5, false, ManaReso.Get("MiniTra5")));
  260. IdleList.Add(ManaReso.GetFlower(6, false, ManaReso.Get("MiniTra6")));
  261. IdleList.Add(ManaReso.GetFlower(7, false, ManaReso.Get("MiniTra7")));
  262. IdleList.Add(ManaReso.GetFlower(8, false, ManaReso.Get("MiniTra8")));
  263. IdleList.Add(ManaReso.GetFlower(9, false, ManaReso.Get("MiniTra9")));
  264. }
  265. private static void GameOver()
  266. {
  267. ManaReso.SetActive("D_Rip1", false);
  268. ManaReso.SetActive("D_Water1",false);
  269. ManaReso.SetActive("D_Fertilize1",false);
  270. ManaReso.SetActive("D_Begin",true);
  271. ManaReso.SetText("Da_Tit", "游戏结束");
  272. ManaReso.SetText("Da_Lab", "得分 : " + Score);
  273. ManaReso.SetActive("Da_Quit", false);
  274. ManaReso.SetActive("Da_Cancel", false);
  275. ManaReso.SetActive("Da_Info", true);
  276. ManaReso.SetActive("Da_GetAward", true);
  277. Score = 0;
  278. Game = false;
  279. Pause = false;
  280. Timer = GameTime;
  281. OpTimer = OpTime;
  282. for (int i = 0; i < IdleList.Count; i++)
  283. {
  284. IdleList[i].GameOver();
  285. }
  286. for (int i = 0; i < OpList.Count; i++)
  287. {
  288. OpList[i].GameOver();
  289. }
  290. IdleList.AddRange(OpList);
  291. }
  292. private static void SetOpStatus()
  293. {
  294. if (OpList.Count >= 2)
  295. {
  296. OpList[0].SetFirstOp();
  297. OpList[1].SetSecondOp();
  298. }
  299. else if (OpList.Count >= 1)
  300. {
  301. OpList[0].SetFirstOp();
  302. }
  303. }
  304. #endregion
  305. }