GameMgr.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /* 游戏管理者(游戏模式:单人模式、PK模式) */
  5. public class GameMgr : MonoBehaviour
  6. {
  7. public static bool debugInEditor = false;
  8. public static int gameType = 0;
  9. public GameMode gameMode;
  10. [System.NonSerialized] public bool gameOver = false;
  11. public static GameMgr ins;
  12. void Awake()
  13. {
  14. ins = this;
  15. OnlinePKTest.Init();
  16. HomeMgr.HideCacheViews();
  17. if (Application.platform == RuntimePlatform.WindowsEditor)
  18. {
  19. debugInEditor = true;
  20. }
  21. AudioMgr.Init();
  22. this.InitGameMode();
  23. if (debugInEditor) {
  24. guideFinish = true;
  25. DoGameModeStart();
  26. } else {
  27. if (!BluetoothStatus.IsAllConnected()) {
  28. GameObject view = DeviceReconnectView.Show();
  29. if (view) {
  30. view.GetComponent<DeviceReconnectView>().onComplete = CheckGuide;
  31. }
  32. } else {
  33. this.CheckGuide();
  34. }
  35. }
  36. // GameSceneLoadBtns.Create();
  37. UserGameAnalyse.CreateWhenGameStart(gameType);
  38. }
  39. void Start()
  40. {
  41. try {
  42. GlobalEventCenter.ins.onGameSceneLoad?.Invoke();
  43. } catch (System.Exception e) { Debug.LogError(e.Message); }
  44. if (ShootCheck.ins) ShootCheck.ins.AdjustNormalOrHightMode();
  45. }
  46. void OnDestroy() {
  47. try {
  48. GlobalEventCenter.ins.onGameSceneDestroy?.Invoke();
  49. } catch (System.Exception e) { Debug.LogError(e.Message); }
  50. if (AimHandler.ins) AimHandler.ins.Ban9AxisCalculate(false);
  51. clearLockerForGamePause();
  52. if (ins == this) ins = null;
  53. }
  54. void FixedUpdate()
  55. {
  56. gameMode.Update();
  57. }
  58. void Update()
  59. {
  60. gameMode.FrameUpdate();
  61. }
  62. void InitGameMode() {
  63. if (gameType == 0) gameMode = new GameModeTest(this);
  64. if (gameType == 1) gameMode = new TimeLimitGameMode(this);
  65. if (gameType == 2) gameMode = new PKGameMode(this);
  66. if (gameType == 3) gameMode = new RabbitHuntGameMode(this);
  67. if (gameType == 4) gameMode = new YejiHuntGameMode(this);
  68. if (gameType == 5) gameMode = new WolfHuntGameMode(this);
  69. if (gameType == 6) gameMode = new RabbitHuntGameMode_LocalPK(this);
  70. if (gameType == 7) gameMode = new YejiHuntGameMode_LocalPK(this);
  71. if (gameType == 8) gameMode = new WolfHuntGameMode_LocalPK(this);
  72. if (gameType == 9) gameMode = new PKGameMode_OnlinePK(this);
  73. if (gameType == 10) gameMode = new RabbitHuntGameMode_OnlinePK(this);
  74. if (gameType == 11) gameMode = new YejiHuntGameMode_OnlinePK(this);
  75. if (gameType == 12) gameMode = new WolfHuntGameMode_OnlinePK(this);
  76. }
  77. public void StopGame() {
  78. gameOver = true;
  79. if (BowCamera.ins) BowCamera.ins.enabled = false;
  80. Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
  81. foreach(var arrow in arrows)
  82. {
  83. Destroy(arrow);
  84. }
  85. }
  86. bool guideFinish = false;
  87. public void CheckGuide() {
  88. if (gameType > 0) {
  89. if (!UserSettings.ins.deviceCalibrateGuideFinish) {
  90. DeviceCalibrateView.Create();
  91. return;
  92. }
  93. if (gameType < 3) {
  94. if (!UserSettings.ins.gameRuleGuideFinish.Contains(gameType)) {
  95. GameRuleView.Create();
  96. return;
  97. }
  98. }
  99. }
  100. guideFinish = true;
  101. DoGameModeStart();
  102. }
  103. public void FinishGameRuleGuide() {
  104. if (guideFinish) return;
  105. UserSettings.ins.gameRuleGuideFinish.Add(gameType);
  106. UserSettings.ins.Save();
  107. CheckGuide();
  108. }
  109. public void FinishDeviceCalibrateGuide() {
  110. if (guideFinish) return;
  111. UserSettings.ins.deviceCalibrateGuideFinish = true;
  112. UserSettings.ins.Save();
  113. CheckGuide();
  114. }
  115. void DoGameModeStart() {
  116. gameMode.Start();
  117. }
  118. HashSet<Object> gamePauseLockers = new HashSet<Object>();
  119. public bool gamePause {
  120. get {
  121. return gamePauseLockers.Count > 0;
  122. }
  123. }
  124. public void addLockerForGamePause(Object o)
  125. {
  126. gamePauseLockers.Add(o);
  127. if (gamePauseLockers.Count > 0) {
  128. Time.timeScale = 0;
  129. }
  130. }
  131. public void removeLockerForGamePause(Object o)
  132. {
  133. gamePauseLockers.Remove(o);
  134. if (gamePauseLockers.Count == 0) {
  135. Time.timeScale = 1;
  136. }
  137. }
  138. public void clearLockerForGamePause()
  139. {
  140. gamePauseLockers.Clear();
  141. Time.timeScale = 1;
  142. }
  143. //现实的计量值转游戏场景的计量值(米)
  144. public static float RealSizeToGameSize(float realSize)
  145. {
  146. // return realSize * 0.413966f;
  147. return realSize;
  148. }
  149. //游戏场景的计量值转现实的计量值(米)
  150. public static float GameSizeToRealSize(float gameSize)
  151. {
  152. // return gameSize / 0.413966f;
  153. return gameSize;
  154. }
  155. }