GameMgr.cs 5.8 KB

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