GameMgr.cs 5.6 KB

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