GameMgr.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using UnityEngine;
  2. public class GameMgr : MonoBehaviour
  3. {
  4. public static int gameMode = 2;
  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. }
  82. /**闯关模式 */
  83. public class GameMode1 {
  84. public static int level;
  85. public int arrowCount = 3;
  86. public int arrowCountMax = 3;
  87. public int score = 0;
  88. public int oneStarScore = 8;
  89. public bool finish;
  90. public void init(GameMgr gameMgr) {
  91. GameObject gv1 = Resources.Load<GameObject>("Prefabs/Views/ChallengeGameView");
  92. GameObject.Instantiate(gv1, Vector3.zero, new Quaternion());
  93. //debug
  94. TargetBody tb = GameObject.Find("GameArea/010/TargetBody").GetComponent<TargetBody>();
  95. TargetBody tb1 = GameObject.Find("GameArea/010 (1)/TargetBody").GetComponent<TargetBody>();
  96. // TargetBody tb2 = GameObject.Find("GameArea/010 (2)/TargetBody").GetComponent<TargetBody>();
  97. GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().validTargets.Add(tb);
  98. GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().validTargets.Add(tb1);
  99. // GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().validTargets.Add(tb2);
  100. }
  101. public void hitTarget(GameMgr gameMgr, int score) {
  102. this.score += score;
  103. this.arrowCount -= 1;
  104. }
  105. public bool checkFinish(GameMgr gameMgr) {
  106. if (this.finish) return true;
  107. if (this.arrowCount <= 0) {
  108. this.finish = true;
  109. gameMgr.gameOver = true;
  110. gameMgr.StopGame();
  111. GameObject gwv1 = Resources.Load<GameObject>("Prefabs/Views/GameResultView");
  112. GameObject.Instantiate(gwv1, Vector3.zero, new Quaternion());
  113. return true;
  114. }
  115. return false;
  116. }
  117. public GameWinResult getWinResult() {
  118. GameWinResult result = new GameWinResult();
  119. result.starCount = this.score / this.oneStarScore;
  120. result.score = this.score;
  121. string key = "Challenge_Star_";
  122. int star = PlayerPrefs.GetInt(key + level, 0);
  123. if (result.starCount > star)
  124. {
  125. PlayerPrefs.SetInt(key + level, result.starCount);
  126. }
  127. return result;
  128. }
  129. }
  130. /**限时模式 */
  131. public class GameMode2 {
  132. public int hitCount = 0;
  133. public int score = 0;
  134. public int oneStarScore = 10;
  135. public float time = 60;
  136. public bool finish;
  137. public void init(GameMgr gameMgr) {
  138. GameObject gv1 = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameView");
  139. GameObject.Instantiate(gv1, Vector3.zero, new Quaternion());
  140. TargetBody tb = GameObject.Find("GameArea/010/TargetBody").GetComponent<TargetBody>();
  141. GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().validTargets.Add(tb);
  142. }
  143. public void hitTarget(GameMgr gameMgr, int score) {
  144. this.score += score;
  145. this.hitCount++;
  146. }
  147. public bool checkFinish(GameMgr gameMgr) {
  148. return this.finish;
  149. }
  150. public GameWinResult getWinResult() {
  151. GameWinResult result = new GameWinResult();
  152. result.starCount = this.score / this.oneStarScore;
  153. result.score = this.score;
  154. string keyLowest = "TimeLimitGameLowestScore";
  155. string keyHighest = "TimeLimitGameHighestScore";
  156. string keyLowestHitCount = "TimeLimitGameLowestHitCount";
  157. string keyHighestHitCount = "TimeLimitGameHighestHitCount";
  158. int lowestScore = PlayerPrefs.GetInt(keyLowest, -1);
  159. int highestScore = PlayerPrefs.GetInt(keyHighest, -1);
  160. if (lowestScore == -1 || result.score < lowestScore) {
  161. PlayerPrefs.SetInt(keyLowest, result.score);
  162. PlayerPrefs.SetInt(keyLowestHitCount, hitCount);
  163. }
  164. if (highestScore == -1 || result.score > highestScore) {
  165. PlayerPrefs.SetInt(keyHighest, result.score);
  166. PlayerPrefs.SetInt(keyHighestHitCount, hitCount);
  167. }
  168. return result;
  169. }
  170. public int[] GetScores()
  171. {
  172. int[] scores = new int[4];
  173. string keyLowest = "TimeLimitGameLowestScore";
  174. string keyHighest = "TimeLimitGameHighestScore";
  175. string keyLowestHitCount = "TimeLimitGameLowestHitCount";
  176. string keyHighestHitCount = "TimeLimitGameHighestHitCount";
  177. scores[0] = PlayerPrefs.GetInt(keyHighestHitCount, 0);
  178. scores[1] = PlayerPrefs.GetInt(keyHighest, 0);
  179. scores[2] = PlayerPrefs.GetInt(keyLowestHitCount, 0);
  180. scores[3] = PlayerPrefs.GetInt(keyLowest, 0);
  181. return scores;
  182. }
  183. public void UpdateTime(GameMgr gameMgr) {
  184. if (this.finish) return;
  185. if (this.time > 0) {
  186. this.time -= Time.deltaTime;
  187. }
  188. else
  189. {
  190. this.time = 0;
  191. this.finish = true;
  192. gameMgr.gameOver = true;
  193. gameMgr.StopGame();
  194. GameObject gwv1 = Resources.Load<GameObject>("Prefabs/Views/GameResultView");
  195. GameObject.Instantiate(gwv1, Vector3.zero, new Quaternion());
  196. }
  197. }
  198. public string getTimeStr()
  199. {
  200. int seconds = Mathf.FloorToInt(this.time);
  201. string str = "";
  202. int m = seconds / 60;
  203. if (m < 10) {
  204. str += 0;
  205. }
  206. str += m;
  207. str += " : ";
  208. int s = seconds % 60;
  209. if (s < 10)
  210. {
  211. str += 0;
  212. }
  213. str += s;
  214. return str;
  215. }
  216. }
  217. public class GameWinResult {
  218. public int starCount;
  219. public int score;
  220. }