GameMgr.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using UnityEngine;
  2. public class GameMgr : MonoBehaviour
  3. {
  4. public static int gameMode = 1;
  5. public GameMode1 gameMode1;
  6. public GameMode2 gameMode2;
  7. public bool gameOver = false;
  8. public static GameMgr ins;
  9. void Start()
  10. {
  11. ins = this;
  12. AudioMgr.init();
  13. this.initGameMode();
  14. }
  15. void FixedUpdate()
  16. {
  17. if (this.gameMode2 != null)
  18. {
  19. this.gameMode2.UpdateTime(this);
  20. }
  21. }
  22. void initGameMode() {
  23. if (gameMode == 1)
  24. {
  25. this.gameMode1 = new GameMode1();
  26. this.gameMode1.init(this);
  27. }
  28. else if (gameMode == 2)
  29. {
  30. this.gameMode2 = new GameMode2();
  31. this.gameMode2.init(this);
  32. }
  33. }
  34. public void hitTarget(int score) {
  35. if (gameMode == 1)
  36. {
  37. this.gameMode1.hitTarget(this, score);
  38. }
  39. else if (gameMode == 2)
  40. {
  41. this.gameMode2.hitTarget(this, score);
  42. }
  43. }
  44. public bool checkFinish() {
  45. if (gameMode == 1)
  46. {
  47. return this.gameMode1.checkFinish(this);
  48. }
  49. else if (gameMode == 2)
  50. {
  51. return this.gameMode2.checkFinish(this);
  52. }
  53. return false;
  54. }
  55. public GameWinResult GetGameWinResult()
  56. {
  57. if (gameMode == 1)
  58. {
  59. return gameMode1.getWinResult();
  60. }
  61. else if (gameMode == 2)
  62. {
  63. return gameMode2.getWinResult();
  64. }
  65. return null;
  66. }
  67. public void Pause() {
  68. Time.timeScale = 0;
  69. }
  70. public void Resume() {
  71. Time.timeScale = 1;
  72. }
  73. public void StopGame() {
  74. Destroy(GameObject.FindObjectOfType<BowCamera>());
  75. Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
  76. foreach(var arrow in arrows)
  77. {
  78. Destroy(arrow);
  79. }
  80. }
  81. public void OpenBluetoothDebug() {
  82. BluetoothHolder.ins.openDebug();
  83. }
  84. }
  85. /**闯关模式 */
  86. public class GameMode1 {
  87. public static int level;
  88. public int arrowCount = 3;
  89. public int arrowCountMax = 3;
  90. public int score = 0;
  91. public int oneStarScore = 8;
  92. public bool finish;
  93. public void init(GameMgr gameMgr) {
  94. GameObject gv1 = Resources.Load<GameObject>("Prefabs/Views/ChallengeGameView");
  95. GameObject.Instantiate(gv1, Vector3.zero, new Quaternion());
  96. //debug
  97. TargetBody tb = GameObject.Find("GameArea/010/TargetBody").GetComponent<TargetBody>();
  98. // TargetBody tb1 = GameObject.Find("GameArea/010 (1)/TargetBody").GetComponent<TargetBody>();
  99. // TargetBody tb2 = GameObject.Find("GameArea/010 (2)/TargetBody").GetComponent<TargetBody>();
  100. GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().validTargets.Add(tb);
  101. // GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().validTargets.Add(tb1);
  102. // GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().validTargets.Add(tb2);
  103. }
  104. public void hitTarget(GameMgr gameMgr, int score) {
  105. // this.score += score;
  106. // this.arrowCount -= 1;
  107. }
  108. public bool checkFinish(GameMgr gameMgr) {
  109. if (this.finish) return true;
  110. if (this.arrowCount <= 0) {
  111. this.finish = true;
  112. gameMgr.gameOver = true;
  113. gameMgr.StopGame();
  114. GameObject gwv1 = Resources.Load<GameObject>("Prefabs/Views/GameResultView");
  115. GameObject.Instantiate(gwv1, Vector3.zero, new Quaternion());
  116. return true;
  117. }
  118. return false;
  119. }
  120. public GameWinResult getWinResult() {
  121. GameWinResult result = new GameWinResult();
  122. result.starCount = this.score / this.oneStarScore;
  123. result.score = this.score;
  124. string key = "Challenge_Star_";
  125. int star = PlayerPrefs.GetInt(key + level, 0);
  126. if (result.starCount > star)
  127. {
  128. PlayerPrefs.SetInt(key + level, result.starCount);
  129. }
  130. return result;
  131. }
  132. }
  133. /**限时模式 */
  134. public class GameMode2 {
  135. public int hitCount = 0;
  136. public int score = 0;
  137. public int oneStarScore = 10;
  138. public float time = 60;
  139. public bool finish;
  140. public void init(GameMgr gameMgr) {
  141. GameObject gv1 = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameView");
  142. GameObject.Instantiate(gv1, Vector3.zero, new Quaternion());
  143. TargetBody tb = GameObject.Find("GameArea/010/TargetBody").GetComponent<TargetBody>();
  144. GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().validTargets.Add(tb);
  145. }
  146. public void hitTarget(GameMgr gameMgr, int score) {
  147. this.score += score;
  148. this.hitCount++;
  149. }
  150. public bool checkFinish(GameMgr gameMgr) {
  151. return this.finish;
  152. }
  153. public GameWinResult getWinResult() {
  154. GameWinResult result = new GameWinResult();
  155. result.starCount = this.score / this.oneStarScore;
  156. result.score = this.score;
  157. string keyLowest = "TimeLimitGameLowestScore";
  158. string keyHighest = "TimeLimitGameHighestScore";
  159. string keyLowestHitCount = "TimeLimitGameLowestHitCount";
  160. string keyHighestHitCount = "TimeLimitGameHighestHitCount";
  161. int lowestScore = PlayerPrefs.GetInt(keyLowest, -1);
  162. int highestScore = PlayerPrefs.GetInt(keyHighest, -1);
  163. if (lowestScore == -1 || result.score < lowestScore) {
  164. PlayerPrefs.SetInt(keyLowest, result.score);
  165. PlayerPrefs.SetInt(keyLowestHitCount, hitCount);
  166. }
  167. if (highestScore == -1 || result.score > highestScore) {
  168. PlayerPrefs.SetInt(keyHighest, result.score);
  169. PlayerPrefs.SetInt(keyHighestHitCount, hitCount);
  170. }
  171. return result;
  172. }
  173. public int[] GetScores()
  174. {
  175. int[] scores = new int[4];
  176. string keyLowest = "TimeLimitGameLowestScore";
  177. string keyHighest = "TimeLimitGameHighestScore";
  178. string keyLowestHitCount = "TimeLimitGameLowestHitCount";
  179. string keyHighestHitCount = "TimeLimitGameHighestHitCount";
  180. scores[0] = PlayerPrefs.GetInt(keyHighestHitCount, 0);
  181. scores[1] = PlayerPrefs.GetInt(keyHighest, 0);
  182. scores[2] = PlayerPrefs.GetInt(keyLowestHitCount, 0);
  183. scores[3] = PlayerPrefs.GetInt(keyLowest, 0);
  184. return scores;
  185. }
  186. public void UpdateTime(GameMgr gameMgr) {
  187. if (this.finish) return;
  188. if (this.time > 0) {
  189. this.time -= Time.deltaTime;
  190. }
  191. else
  192. {
  193. this.time = 0;
  194. this.finish = true;
  195. gameMgr.gameOver = true;
  196. gameMgr.StopGame();
  197. GameObject gwv1 = Resources.Load<GameObject>("Prefabs/Views/GameResultView");
  198. GameObject.Instantiate(gwv1, Vector3.zero, new Quaternion());
  199. }
  200. }
  201. public string getTimeStr()
  202. {
  203. int seconds = Mathf.FloorToInt(this.time);
  204. string str = "";
  205. int m = seconds / 60;
  206. if (m < 10) {
  207. str += 0;
  208. }
  209. str += m;
  210. str += " : ";
  211. int s = seconds % 60;
  212. if (s < 10)
  213. {
  214. str += 0;
  215. }
  216. str += s;
  217. return str;
  218. }
  219. }
  220. public class GameWinResult {
  221. public int starCount;
  222. public int score;
  223. }