ManagerLittleGame.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 ManagerLittleGame : MonoBehaviour
  9. {
  10. #region 变量
  11. private Text ScoreTitle;
  12. private Text ScoreLabel;
  13. private Text GameStatus;
  14. private Text RemainTime;
  15. private Button Ripping;
  16. private Button Watering;
  17. private Button Fertilizing;
  18. private Button GameBegin;
  19. private GameObject OperatePanel;
  20. private float GameTimer;
  21. private float PenaltyTimer;
  22. private float OpFrequencyTimer;
  23. private float _GameTime;
  24. private float _PenaltyTime;
  25. private float _OpFrequencyTime;
  26. private bool GameOnA;
  27. private List<Flower> IdleFlowerList;
  28. private List<Flower> OperateFlowerList;
  29. public static ManagerLittleGame Ins;
  30. #endregion
  31. private void Awake()
  32. {
  33. ScoreTitle = ManagerResource.Ins.ScoreTitle;
  34. ScoreLabel = ManagerResource.Ins.ScoreLabel;
  35. GameStatus = ManagerResource.Ins.GameStatusLabel;
  36. RemainTime = ManagerResource.Ins.RemainTimeLabel;
  37. Ripping = ManagerResource.Ins.RippingBtn;
  38. Watering = ManagerResource.Ins.WateringBtn;
  39. Fertilizing = ManagerResource.Ins.FertilizingBtn;
  40. GameBegin = ManagerResource.Ins.GameBeginBtn;
  41. OperatePanel = ManagerResource.Ins.OperatePanel;
  42. Ripping.onClick.AddListener(OnRipping);
  43. Watering.onClick.AddListener(OnWatering);
  44. Fertilizing.onClick.AddListener(OnFertilizing);
  45. GameBegin.onClick.AddListener(OnGameBeginA);
  46. Ins = this;
  47. _GameTime = 15;
  48. _PenaltyTime = 1;
  49. _OpFrequencyTime = 1;
  50. OpFrequencyTimer = _OpFrequencyTime;
  51. IdleFlowerList = new List<Flower>();
  52. OperateFlowerList = new List<Flower>();
  53. }
  54. private void FixedUpdate()
  55. {
  56. #region 小游戏逻辑A
  57. if (!GameOnA)
  58. {
  59. return;
  60. }
  61. GameTimer -= Time.fixedDeltaTime; //小游戏计时
  62. RemainTime.text = GameTimer.ToString("0.0");
  63. if (GameTimer <= 0)
  64. {
  65. OnGameOverA();
  66. return; //小游戏结束
  67. }
  68. if (PenaltyTimer > 0) //冻结时间计时
  69. {
  70. PenaltyTimer -= Time.fixedDeltaTime;
  71. }
  72. OpFrequencyTimer -= Time.fixedDeltaTime; //生成操作计时
  73. if (OpFrequencyTimer <= 0)
  74. {
  75. if (IdleFlowerList.Count > 0)
  76. {
  77. OpFrequencyTimer = _OpFrequencyTime;
  78. Flower flower = IdleFlowerList[Random.Range(0, IdleFlowerList.Count)];
  79. flower.NeedOperate(OperateFlowerList.Count);
  80. IdleFlowerList.Remove(flower);
  81. OperateFlowerList.Add(flower);
  82. }
  83. }
  84. #endregion
  85. }
  86. #region 小游戏函数A
  87. public void Scoring(int score)
  88. {
  89. ScoreLabel.text = (int.Parse(ScoreLabel.text) + score).ToString();
  90. }
  91. private void OnRipping()
  92. {
  93. if (PenaltyTimer > 0) //冻结操作
  94. {
  95. return;
  96. }
  97. if (OperateFlowerList.Count > 0)
  98. {
  99. if (OperateFlowerList[0].Operate(OperateType.Ripping)) //操作正确
  100. {
  101. IdleFlowerList.Add(OperateFlowerList[0]);
  102. OperateFlowerList.Remove(OperateFlowerList[0]);
  103. UpdateSequence();
  104. }
  105. else //操作错误
  106. {
  107. PenaltyTimer = _PenaltyTime;
  108. ManagerHint.Ins.Show("惩罚" + _PenaltyTime + "秒");
  109. }
  110. }
  111. }
  112. private void OnWatering()
  113. {
  114. if (PenaltyTimer > 0) //冻结操作
  115. {
  116. return;
  117. }
  118. if (OperateFlowerList.Count > 0)
  119. {
  120. if (OperateFlowerList[0].Operate(OperateType.Watering)) //操作正确
  121. {
  122. IdleFlowerList.Add(OperateFlowerList[0]);
  123. OperateFlowerList.Remove(OperateFlowerList[0]);
  124. UpdateSequence();
  125. }
  126. else //操作错误
  127. {
  128. PenaltyTimer = _PenaltyTime;
  129. ManagerHint.Ins.Show("惩罚" + _PenaltyTime + "秒");
  130. }
  131. }
  132. }
  133. private void OnFertilizing()
  134. {
  135. if (PenaltyTimer > 0) //冻结操作
  136. {
  137. return;
  138. }
  139. if (OperateFlowerList.Count > 0)
  140. {
  141. if (OperateFlowerList[0].Operate(OperateType.Fertilizing)) //操作正确
  142. {
  143. IdleFlowerList.Add(OperateFlowerList[0]);
  144. OperateFlowerList.Remove(OperateFlowerList[0]);
  145. UpdateSequence();
  146. }
  147. else //操作错误
  148. {
  149. PenaltyTimer = _PenaltyTime;
  150. ManagerHint.Ins.Show("惩罚" + _PenaltyTime + "秒");
  151. }
  152. }
  153. }
  154. private void OnGameOverA()
  155. {
  156. GameOnA = false;
  157. OperatePanel.SetActive(false);
  158. ScoreTitle.gameObject.SetActive(false);
  159. GameBegin.gameObject.SetActive(true);
  160. GameStatus.gameObject.SetActive(false);
  161. for (int i = 0; i < IdleFlowerList.Count; i++)
  162. {
  163. IdleFlowerList[i].OnGameOverA();
  164. }
  165. for (int i = 0; i < OperateFlowerList.Count; i++)
  166. {
  167. OperateFlowerList[i].OnGameOverA();
  168. }
  169. IdleFlowerList = new List<Flower>();
  170. OperateFlowerList = new List<Flower>();
  171. }
  172. private void OnGameBeginA()
  173. {
  174. GameOnA = true;
  175. GameTimer = _GameTime;
  176. ScoreLabel.text = "0";
  177. OperatePanel.SetActive(true);
  178. ScoreTitle.gameObject.SetActive(true);
  179. GameBegin.gameObject.SetActive(false);
  180. GameStatus.gameObject.SetActive(true);
  181. IdleFlowerList = new List<Flower>(ManagerResource.Ins.FlowerList);
  182. for (int i = 0; i < IdleFlowerList.Count; i++)
  183. {
  184. IdleFlowerList[i].OnGameBeginA();
  185. }
  186. }
  187. private void UpdateSequence()
  188. {
  189. if (OperateFlowerList.Count >= 2)
  190. {
  191. OperateFlowerList[0].MarkAsFirst();
  192. OperateFlowerList[1].MarkAsSecond();
  193. }
  194. else if (OperateFlowerList.Count >= 1)
  195. {
  196. OperateFlowerList[0].MarkAsFirst();
  197. }
  198. }
  199. #endregion
  200. }