GameMgr.cs 5.0 KB

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