GameMgr.cs 5.4 KB

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