GameMgr.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. }
  53. void FixedUpdate()
  54. {
  55. gameMode.Update();
  56. }
  57. void Update()
  58. {
  59. gameMode.FrameUpdate();
  60. }
  61. void InitGameMode() {
  62. if (gameType == 0) gameMode = new GameModeTest(this);
  63. if (gameType == 1) gameMode = new TimeLimitGameMode(this);
  64. if (gameType == 2) gameMode = new PKGameMode(this);
  65. if (gameType == 3) gameMode = new RabbitHuntGameMode(this);
  66. if (gameType == 4) gameMode = new YejiHuntGameMode(this);
  67. if (gameType == 5) gameMode = new WolfHuntGameMode(this);
  68. if (gameType == 6) gameMode = new RabbitHuntGameMode_LocalPK(this);
  69. if (gameType == 7) gameMode = new YejiHuntGameMode_LocalPK(this);
  70. if (gameType == 8) gameMode = new WolfHuntGameMode_LocalPK(this);
  71. if (gameType == 9) gameMode = new PKGameMode_OnlinePK(this);
  72. if (gameType == 10) gameMode = new RabbitHuntGameMode_OnlinePK(this);
  73. if (gameType == 11) gameMode = new YejiHuntGameMode_OnlinePK(this);
  74. if (gameType == 12) gameMode = new WolfHuntGameMode_OnlinePK(this);
  75. }
  76. public void StopGame() {
  77. gameOver = true;
  78. if (BowCamera.ins) BowCamera.ins.enabled = false;
  79. Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
  80. foreach(var arrow in arrows)
  81. {
  82. Destroy(arrow);
  83. }
  84. }
  85. bool guideFinish = false;
  86. public void CheckGuide() {
  87. if (gameType > 0) {
  88. if (!UserSettings.ins.deviceCalibrateGuideFinish) {
  89. DeviceCalibrateView.Create();
  90. return;
  91. }
  92. if (gameType < 3) {
  93. if (!UserSettings.ins.gameRuleGuideFinish.Contains(gameType)) {
  94. GameRuleView.Create();
  95. return;
  96. }
  97. }
  98. }
  99. guideFinish = true;
  100. DoGameModeStart();
  101. }
  102. public void FinishGameRuleGuide() {
  103. if (guideFinish) return;
  104. UserSettings.ins.gameRuleGuideFinish.Add(gameType);
  105. UserSettings.ins.Save();
  106. CheckGuide();
  107. }
  108. public void FinishDeviceCalibrateGuide() {
  109. if (guideFinish) return;
  110. UserSettings.ins.deviceCalibrateGuideFinish = true;
  111. UserSettings.ins.Save();
  112. CheckGuide();
  113. }
  114. void DoGameModeStart() {
  115. gameMode.Start();
  116. }
  117. HashSet<Object> gamePauseLockers = new HashSet<Object>();
  118. public bool gamePause {
  119. get {
  120. return gamePauseLockers.Count > 0;
  121. }
  122. }
  123. public void addLockerForGamePause(Object o)
  124. {
  125. gamePauseLockers.Add(o);
  126. if (gamePauseLockers.Count > 0) {
  127. Time.timeScale = 0;
  128. }
  129. }
  130. public void removeLockerForGamePause(Object o)
  131. {
  132. gamePauseLockers.Remove(o);
  133. if (gamePauseLockers.Count == 0) {
  134. Time.timeScale = 1;
  135. }
  136. }
  137. public void clearLockerForGamePause()
  138. {
  139. gamePauseLockers.Clear();
  140. Time.timeScale = 1;
  141. }
  142. //现实的计量值转游戏场景的计量值(米)
  143. public static float RealSizeToGameSize(float realSize)
  144. {
  145. // return realSize * 0.413966f;
  146. return realSize;
  147. }
  148. //游戏场景的计量值转现实的计量值(米)
  149. public static float GameSizeToRealSize(float gameSize)
  150. {
  151. // return gameSize / 0.413966f;
  152. return gameSize;
  153. }
  154. }