GameMgr.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 static bool bNavBack = false;
  14. public static bool bShowDistance = true;
  15. #region 隐藏光标 和 弓,手部分
  16. public GameObject[] bowObjs;
  17. public bool bShowBow = false;
  18. public void onChangeCrossHair() {
  19. bool onlyShow =!CrossHair.ins.GetOnlyShow();
  20. CrossHair.ins.SetOnlyShow(onlyShow);
  21. if (onlyShow)
  22. {
  23. ScreenLocate.Main.mUIManagerSingle?.onShowCrosshair();
  24. }
  25. else
  26. {
  27. ScreenLocate.Main.mUIManagerSingle?.onHideCrosshair();
  28. }
  29. }
  30. public void onSetCrossHair(bool open)
  31. {
  32. CrossHair.ins.SetOnlyShow(open);
  33. }
  34. public void onChangeBow() {
  35. setBowState(!bShowBow);
  36. }
  37. void setBowState(bool _show) {
  38. bShowBow = _show;
  39. PlayerPrefs.SetInt("bShowBow", bShowBow ? 1 : 0);
  40. for (int i = 0; i < bowObjs.Length; i++)
  41. {
  42. bowObjs[i].SetActive(bShowBow);
  43. }
  44. }
  45. #endregion
  46. void Awake()
  47. {
  48. ins = this;
  49. // try { //隐藏了手臂和弓,且拉弓搭箭速度提高10倍
  50. // transform.Find("Main Camera").GetComponent<Camera>().cullingMask = -1033;
  51. // transform.Find("Main Camera/ArmBow").GetComponent<AnimationPlayer>().speed = 20;
  52. // } catch (System.Exception) {}
  53. OnlinePKTest.Init();
  54. //如果
  55. if (Application.platform == RuntimePlatform.WindowsEditor || InfraredManager.ConnetDevicesSingle.ins)
  56. {
  57. debugInEditor = true;
  58. }
  59. AudioMgr.Init();
  60. this.InitGameMode();
  61. if (debugInEditor) {
  62. guideFinish = true;
  63. DoGameModeStart();
  64. } else {
  65. CheckGuide();
  66. }
  67. }
  68. void Start()
  69. {
  70. try {
  71. GlobalEventCenter.ins.onGameSceneLoad?.Invoke();
  72. } catch (System.Exception e) { Debug.LogError(e.Message); }
  73. //if (ShootCheck.ins) {
  74. // ShootCheck.ins.AdjustNormalOrHightMode();
  75. //}
  76. //判断射箭光标
  77. //Debug.Log(PlayerPrefs.GetInt("CrossHairImageActive", 1));
  78. onSetCrossHair(PlayerPrefs.GetInt("CrossHairImageActive", 1) == 1);
  79. //默认隐藏手臂和弓
  80. setBowState(PlayerPrefs.GetInt("bShowBow", 0) == 1);
  81. }
  82. void OnDestroy() {
  83. //此脚本删除后重新设置 bShowDistance bNavBack
  84. bShowDistance = true;
  85. bNavBack = false;
  86. try
  87. {
  88. GlobalEventCenter.ins.onGameSceneDestroy?.Invoke();
  89. } catch (System.Exception e) { Debug.LogError(e.Message); }
  90. //if (AimHandler.ins) {
  91. // AimHandler.ins.Ban9AxisCalculate(false);
  92. // AimHandler.ins.bInitOne = false;
  93. //}
  94. clearLockerForGamePause();
  95. if (ins == this) ins = null;
  96. }
  97. void FixedUpdate()
  98. {
  99. gameMode.Update();
  100. }
  101. void Update()
  102. {
  103. gameMode.FrameUpdate();
  104. }
  105. void InitGameMode() {
  106. if (gameType == 0) gameMode = new GameModeTest(this);
  107. if (gameType == 1) gameMode = new TimeLimitGameMode(this);
  108. if (gameType == 2) gameMode = new PKGameMode(this);
  109. if (gameType == 9) gameMode = new PKGameMode_OnlinePK(this);
  110. }
  111. public void StopGame() {
  112. gameOver = true;
  113. if (BowCamera.ins) BowCamera.ins.enabled = false;
  114. Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
  115. foreach(var arrow in arrows)
  116. {
  117. Destroy(arrow);
  118. }
  119. }
  120. bool guideFinish = false;
  121. public void CheckGuide() {
  122. //地磁计跳进来不需要初始化任务
  123. if (bShowDistance) {
  124. if (gameType > 0)
  125. {
  126. if (!UserSettings.ins.deviceCalibrateGuideFinish)
  127. {
  128. return;
  129. }
  130. if (gameType < 3)
  131. {
  132. if (!UserSettings.ins.gameRuleGuideFinish.Contains(gameType))
  133. {
  134. GameRuleView.Create();
  135. return;
  136. }
  137. }
  138. }
  139. }
  140. guideFinish = true;
  141. DoGameModeStart();
  142. }
  143. public void FinishGameRuleGuide() {
  144. if (guideFinish) return;
  145. UserSettings.ins.gameRuleGuideFinish.Add(gameType);
  146. UserSettings.ins.Save();
  147. CheckGuide();
  148. }
  149. public void FinishDeviceCalibrateGuide() {
  150. if (guideFinish) return;
  151. UserSettings.ins.deviceCalibrateGuideFinish = true;
  152. UserSettings.ins.Save();
  153. CheckGuide();
  154. }
  155. void DoGameModeStart() {
  156. gameMode.Start();
  157. SimulateMouseController.ins?.RemoveOpenLocker("NotGame");
  158. }
  159. HashSet<Object> gamePauseLockers = new HashSet<Object>();
  160. public bool gamePause {
  161. get {
  162. return gamePauseLockers.Count > 0;
  163. }
  164. }
  165. public void addLockerForGamePause(Object o)
  166. {
  167. gamePauseLockers.Add(o);
  168. if (gamePauseLockers.Count > 0) {
  169. Time.timeScale = 0;
  170. }
  171. }
  172. public void removeLockerForGamePause(Object o)
  173. {
  174. gamePauseLockers.Remove(o);
  175. if (gamePauseLockers.Count == 0) {
  176. Time.timeScale = 1;
  177. }
  178. }
  179. public void clearLockerForGamePause()
  180. {
  181. gamePauseLockers.Clear();
  182. Time.timeScale = 1;
  183. }
  184. //现实的计量值转游戏场景的计量值(米)
  185. public static float RealSizeToGameSize(float realSize)
  186. {
  187. // return realSize * 0.413966f;
  188. return realSize;
  189. }
  190. //游戏场景的计量值转现实的计量值(米)
  191. public static float GameSizeToRealSize(float gameSize)
  192. {
  193. // return gameSize / 0.413966f;
  194. return gameSize;
  195. }
  196. }