GameMgr.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using UnityEngine;
  2. public class GameMgr : MonoBehaviour
  3. {
  4. public static int gameType = 0;
  5. public GameMode gameMode;
  6. public bool gameOver = false;
  7. public static GameMgr ins;
  8. void Start()
  9. {
  10. ins = this;
  11. AudioMgr.init();
  12. this.InitGameMode();
  13. }
  14. void FixedUpdate()
  15. {
  16. gameMode.Update();
  17. }
  18. void InitGameMode() {
  19. if (gameType == 0) gameMode = new GameModeTest(this);
  20. if (gameType == 1) gameMode = new GameMode1(this);
  21. }
  22. public void StopGame() {
  23. Destroy(GameObject.FindObjectOfType<BowCamera>());
  24. Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
  25. foreach(var arrow in arrows)
  26. {
  27. Destroy(arrow);
  28. }
  29. }
  30. public void OpenBluetoothDebug() {
  31. BluetoothHolder.ins.openDebug();
  32. }
  33. }
  34. public abstract class GameMode
  35. {
  36. public GameMgr gameMgr;
  37. public GameMode(GameMgr gameMgr) {
  38. this.gameMgr = gameMgr;
  39. }
  40. public abstract void HitTarget(int score);
  41. public abstract object[] Settle();
  42. public virtual void Update() {}
  43. }
  44. public class GameModeTest : GameMode {
  45. public GameModeTest(GameMgr gameMgr) : base(gameMgr) {}
  46. public override void HitTarget(int score) {}
  47. public override object[] Settle() { return null; }
  48. }
  49. /**单人限时模式 */
  50. public class GameMode1 : GameMode {
  51. public int score = 0;
  52. int oneStarScore = 10;
  53. float time = 60;
  54. public GameMode1(GameMgr gameMgr) : base(gameMgr)
  55. {
  56. //添加游戏界面
  57. GameObject view = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameView");
  58. GameObject.Instantiate(view);
  59. //记录可射击的靶子
  60. TargetBody targetBody = GameObject.Find("GameArea/010/TargetBody").GetComponent<TargetBody>();
  61. GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().validTargets.Add(targetBody);
  62. }
  63. public override void HitTarget(int score) {
  64. this.score += score;
  65. }
  66. public override object[] Settle() {
  67. int starCount = this.score / this.oneStarScore;
  68. string highestScoreKey = "TimeLimitGameHighestScore";
  69. int highestScore = PlayerPrefs.GetInt(highestScoreKey, 0);
  70. if (this.score > highestScore) {
  71. PlayerPrefs.SetInt(highestScoreKey, this.score);
  72. }
  73. return new object[]{starCount, this.score};
  74. }
  75. public override void Update() {
  76. if (gameMgr.gameOver) return;
  77. if (this.time > 0) {
  78. this.time -= Time.deltaTime;
  79. } else {
  80. this.time = 0;
  81. gameMgr.gameOver = true;
  82. gameMgr.StopGame();
  83. //添加结算界面
  84. GameObject view = Resources.Load<GameObject>("Prefabs/Views/GameSettleView1");
  85. GameObject.Instantiate(view);
  86. }
  87. }
  88. public int GetHighestScore()
  89. {
  90. string highestScoreKey = "TimeLimitGameHighestScore";
  91. return PlayerPrefs.GetInt(highestScoreKey, 0);
  92. }
  93. public string GetTimeStr()
  94. {
  95. int seconds = Mathf.FloorToInt(this.time);
  96. string str = "";
  97. int m = seconds / 60;
  98. if (m < 10) {
  99. str += 0;
  100. }
  101. str += m;
  102. str += " : ";
  103. int s = seconds % 60;
  104. if (s < 10)
  105. {
  106. str += 0;
  107. }
  108. str += s;
  109. return str;
  110. }
  111. }