BiologyGameController.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ShadowKit;
  5. public class BiologyGameController : GameBaseController
  6. {
  7. [SerializeField]
  8. private BloodManager m_bloodManager;//刷怪工具
  9. [SerializeField]
  10. private Bowman m_bowman;//抓怪逻辑
  11. private int score = 0;
  12. private float gameTime = 0;
  13. private float MaxTime = 60;//俩分钟
  14. private bool isRunning = false;
  15. private void Awake()
  16. {
  17. GameStop();
  18. }
  19. protected override void Start()
  20. {
  21. base.Start();
  22. MessageCenterController.Instance.Register(GameEnum.MESSAGE_SCORE_ADD, AddScore);
  23. MessageCenterController.Instance.Register(GameEnum.MESSAGE_SCORE_DEC, DecScore);
  24. }
  25. protected override void OnDestroy()
  26. {
  27. base.OnDestroy();
  28. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SCORE_ADD, AddScore);
  29. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_SCORE_DEC, DecScore);
  30. }
  31. private void OnDisable()
  32. {
  33. //被切换出去的时候要重置数据
  34. Score = 0;//重置分数
  35. gameTime = MaxTime;//重置游戏时间
  36. GameStop();
  37. }
  38. //游戏开始
  39. protected override void GameStart()
  40. {
  41. base.GameStart();
  42. if (isRunning)
  43. {
  44. return;
  45. }
  46. Score = 0;//重置分数
  47. isRunning = true;
  48. gameTime = MaxTime;//重置游戏时间
  49. m_bloodManager.StartCreate(); ;
  50. m_bowman.gameObject.SetActive(true);
  51. if(IsInvoking("HeartJump"))
  52. {
  53. CDebug.Log("心跳错误");
  54. return;
  55. }
  56. InvokeRepeating("HeartJump", 1, 1);
  57. }
  58. protected override void GameQuit()
  59. {
  60. base.GameQuit();
  61. GameStop();
  62. }
  63. //private float las
  64. private void LateUpdate()
  65. {
  66. }
  67. //游戏停止
  68. private void GameStop()
  69. {
  70. isRunning = false;
  71. m_bloodManager.StopCreate();
  72. m_bowman.gameObject.SetActive(false);
  73. if (IsInvoking("HeartJump"))
  74. {
  75. CDebug.Log("移除心跳");
  76. CancelInvoke("HeartJump");
  77. return;
  78. }
  79. }
  80. private void AddScore(System.Object obj = null)
  81. {
  82. if (!isRunning)
  83. {
  84. return;
  85. }
  86. BloodItem item = (BloodItem)obj;
  87. if (m_bowman.IsGod(item))
  88. {
  89. //只有拿了水加分
  90. if (item.MType.Equals(BloodManager.BloodItemType.Water))
  91. {
  92. CDebug.Log("add score");
  93. Score += 3;
  94. }
  95. else
  96. {
  97. Score += 10;
  98. }
  99. }
  100. else
  101. {
  102. CDebug.Log("拿错扣分");
  103. Score -= 2;
  104. }
  105. UI.RefreshText((int)gameTime, Score);
  106. }
  107. public int Score
  108. {
  109. get { return score; }
  110. set {
  111. score = Mathf.Clamp(value, 0, 100);
  112. if(score >= 100)
  113. {
  114. //满分自动结束
  115. GameStop();
  116. SendResult();
  117. }
  118. }
  119. }
  120. private const int desScoreValue = 3;
  121. private void DecScore(System.Object obj = null)
  122. {
  123. if (!isRunning)
  124. {
  125. return;
  126. }
  127. BloodItem item = (BloodItem)obj;
  128. if (m_bowman.IsRecyle(item))
  129. {
  130. CDebug.Log("漏掉了" + item.MType.ToString());
  131. Score -= desScoreValue;
  132. }
  133. UI.RefreshText((int)gameTime, Score);
  134. }
  135. //心跳倒计时
  136. private void HeartJump()
  137. {
  138. gameTime -= 1;
  139. UI.RefreshText((int)gameTime, Score);
  140. if (gameTime <= 0)
  141. {
  142. GameStop();
  143. SendResult();
  144. }
  145. }
  146. //游戏结算预留
  147. private void SendResult()
  148. {
  149. UI.RefreshText((int)gameTime, Score);
  150. SendResultData(Score);
  151. }
  152. private BiologyGameUI UI
  153. {
  154. get {
  155. return base.m_UI as BiologyGameUI;
  156. }
  157. }
  158. }