GameMgr.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. using UnityEngine.UI;
  6. using UnityEngine.SceneManagement;
  7. /* 游戏管理者(游戏模式:单人模式、PK模式) */
  8. public class GameMgr : MonoBehaviour
  9. {
  10. public static bool debugInEditor = false;
  11. public static int gameType = 0;
  12. public GameMode gameMode;
  13. [System.NonSerialized] public bool gameOver = false;
  14. public static GameMgr ins;
  15. void Awake()
  16. {
  17. ins = this;
  18. OnlinePKTest.Init();
  19. HomeMgr.HideCacheViews();
  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. if (!BluetoothStatus.IsAllConnected()) {
  31. GameObject view = DeviceReconnectView.Show();
  32. if (view) {
  33. view.GetComponent<DeviceReconnectView>().onComplete = CheckGuide;
  34. }
  35. } else {
  36. this.CheckGuide();
  37. }
  38. }
  39. // GameSceneLoadBtns.Create();
  40. UserGameAnalyse.CreateWhenGameStart(gameType);
  41. }
  42. void Start()
  43. {
  44. try {
  45. GlobalEventCenter.ins.onGameSceneLoad?.Invoke();
  46. } catch (System.Exception e) { Debug.LogError(e.Message); }
  47. if (ShootCheck.ins) ShootCheck.ins.AdjustNormalOrHightMode();
  48. }
  49. void OnDestroy() {
  50. try {
  51. GlobalEventCenter.ins.onGameSceneDestroy?.Invoke();
  52. } catch (System.Exception e) { Debug.LogError(e.Message); }
  53. if (AimHandler.ins) AimHandler.ins.Ban9AxisCalculate(false);
  54. clearLockerForGamePause();
  55. }
  56. void FixedUpdate()
  57. {
  58. gameMode.Update();
  59. }
  60. void Update()
  61. {
  62. gameMode.FrameUpdate();
  63. }
  64. void InitGameMode() {
  65. if (gameType == 0) gameMode = new GameModeTest(this);
  66. if (gameType == 1) gameMode = new TimeLimitGameMode(this);
  67. if (gameType == 2) gameMode = new PKGameMode(this);
  68. if (gameType == 3) gameMode = new RabbitHuntGameMode(this);
  69. if (gameType == 4) gameMode = new YejiHuntGameMode(this);
  70. if (gameType == 5) gameMode = new WolfHuntGameMode(this);
  71. if (gameType == 6) gameMode = new RabbitHuntGameMode_LocalPK(this);
  72. if (gameType == 7) gameMode = new YejiHuntGameMode_LocalPK(this);
  73. if (gameType == 8) gameMode = new WolfHuntGameMode_LocalPK(this);
  74. if (gameType == 9) gameMode = new PKGameMode_OnlinePK(this);
  75. if (gameType == 10) gameMode = new RabbitHuntGameMode_OnlinePK(this);
  76. if (gameType == 11) gameMode = new YejiHuntGameMode_OnlinePK(this);
  77. if (gameType == 12) gameMode = new WolfHuntGameMode_OnlinePK(this);
  78. }
  79. public void StopGame() {
  80. gameOver = true;
  81. if (BowCamera.ins) BowCamera.ins.enabled = false;
  82. Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
  83. foreach(var arrow in arrows)
  84. {
  85. Destroy(arrow);
  86. }
  87. }
  88. bool guideFinish = false;
  89. public void CheckGuide() {
  90. if (gameType > 0) {
  91. if (!UserSettings.ins.deviceCalibrateGuideFinish) {
  92. DeviceCalibrateView.Create();
  93. return;
  94. }
  95. if (gameType < 3) {
  96. if (!UserSettings.ins.gameRuleGuideFinish.Contains(gameType)) {
  97. GameRuleView.Create();
  98. return;
  99. }
  100. }
  101. }
  102. guideFinish = true;
  103. DoGameModeStart();
  104. }
  105. public void FinishGameRuleGuide() {
  106. if (guideFinish) return;
  107. UserSettings.ins.gameRuleGuideFinish.Add(gameType);
  108. UserSettings.ins.Save();
  109. CheckGuide();
  110. }
  111. public void FinishDeviceCalibrateGuide() {
  112. if (guideFinish) return;
  113. UserSettings.ins.deviceCalibrateGuideFinish = true;
  114. UserSettings.ins.Save();
  115. CheckGuide();
  116. }
  117. void DoGameModeStart() {
  118. gameMode.Start();
  119. }
  120. HashSet<Object> gamePauseLockers = new HashSet<Object>();
  121. public bool gamePause {
  122. get {
  123. return gamePauseLockers.Count > 0;
  124. }
  125. }
  126. public void addLockerForGamePause(Object o)
  127. {
  128. gamePauseLockers.Add(o);
  129. if (gamePauseLockers.Count > 0) {
  130. Time.timeScale = 0;
  131. }
  132. }
  133. public void removeLockerForGamePause(Object o)
  134. {
  135. gamePauseLockers.Remove(o);
  136. if (gamePauseLockers.Count == 0) {
  137. Time.timeScale = 1;
  138. }
  139. }
  140. public void clearLockerForGamePause()
  141. {
  142. gamePauseLockers.Clear();
  143. Time.timeScale = 1;
  144. }
  145. //现实的计量值转游戏场景的计量值(米)
  146. public static float RealSizeToGameSize(float realSize)
  147. {
  148. // return realSize * 0.413966f;
  149. return realSize;
  150. }
  151. //游戏场景的计量值转现实的计量值(米)
  152. public static float GameSizeToRealSize(float gameSize)
  153. {
  154. // return gameSize / 0.413966f;
  155. return gameSize;
  156. }
  157. }
  158. public abstract class GameMode
  159. {
  160. public GameMgr gameMgr;
  161. public bool pauseTimeCounting {
  162. get {
  163. return timeCountingPauseLockers.Count > 0;
  164. }
  165. }
  166. HashSet<System.Object> timeCountingPauseLockers = new HashSet<System.Object>();
  167. public void PauseTimeCounting(System.Object o)
  168. {
  169. timeCountingPauseLockers.Add(o);
  170. }
  171. public void ResumeTimeCounting(System.Object o)
  172. {
  173. timeCountingPauseLockers.Remove(o);
  174. }
  175. public GameMode(GameMgr gameMgr) {
  176. this.gameMgr = gameMgr;
  177. }
  178. public virtual void HitTarget(float score) {}
  179. public virtual bool DoNextShoot() { return true; }
  180. public virtual object[] Settle() {return null; }
  181. public virtual void Start() {}
  182. public virtual void Update() {} //fixedUpdate
  183. public virtual void FrameUpdate() {}
  184. public virtual void onBowReady() {}
  185. public virtual void onBowShoot() {}
  186. public void BanBowReady() {
  187. PauseTimeCounting(this);
  188. ArmBow armBow = GameObject.FindObjectOfType<ArmBow>();
  189. armBow.banReady = true;
  190. armBow.banShoot = true;
  191. }
  192. public void UnbanBowReady() {
  193. ResumeTimeCounting(this);
  194. ArmBow armBow = GameObject.FindObjectOfType<ArmBow>();
  195. armBow.banReady = false;
  196. armBow.banShoot = false;
  197. armBow.readyShoot();
  198. }
  199. }
  200. public class GameModeTest : GameMode {
  201. public GameModeTest(GameMgr gameMgr) : base(gameMgr) {
  202. //记录可射击的靶子
  203. if (SceneManager.GetActiveScene().name == "Game") {
  204. TargetBody targetBody = GameObject.Find("GameArea/TargetObject/TargetBody").GetComponent<TargetBody>();
  205. GameObject.FindObjectOfType<ArmBow>().validTargets.Add(targetBody);
  206. }
  207. }
  208. public override void HitTarget(float score) {
  209. HitTargetNumber.Create(score);
  210. }
  211. }
  212. /**单人限时模式 */
  213. public class TimeLimitGameMode : GameMode {
  214. public static int[] distanceCanSelected = {10, 20, 30, 50, 70};
  215. public static int distance = 10;
  216. public float score = 0;
  217. int oneStarScore = 10;
  218. float time = 60;
  219. TargetBody targetBody;
  220. public TimeLimitGameMode(GameMgr gameMgr) : base(gameMgr) {
  221. //记录可射击的靶子
  222. targetBody = GameObject.Find("GameArea/TargetObject/TargetBody").GetComponent<TargetBody>();
  223. GameObject.FindObjectOfType<ArmBow>().validTargets.Add(targetBody);
  224. //添加游戏界面
  225. GameObject view = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameView");
  226. GameObject.Instantiate(view);
  227. BanBowReady();
  228. }
  229. public override void Start()
  230. {
  231. UnbanBowReady();
  232. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameDistanceSelectView"));
  233. }
  234. public void ConfirmSelectedTargetDistance()
  235. {
  236. targetBody.SetDistance(distance);
  237. if (TimeLimitGameView.ins) TimeLimitGameView.ins.RenderHighestScoreByDistance(distance);
  238. TargetView.ins.Show(true);
  239. }
  240. public override void HitTarget(float score) {
  241. this.score += score;
  242. HitTargetNumber.Create(score);
  243. }
  244. public override bool DoNextShoot() {
  245. return !GameMgr.ins.gameOver;
  246. }
  247. public override object[] Settle() {
  248. int starCount = Mathf.FloorToInt(this.score / this.oneStarScore);
  249. float highestScore = 0;
  250. string distanceStr = distance.ToString();
  251. if (LoginMgr.myUserInfo.timeLimitGameScores.ContainsKey(distanceStr)) {
  252. highestScore = LoginMgr.myUserInfo.timeLimitGameScores[distanceStr];
  253. }
  254. if (this.score > highestScore) {
  255. LoginMgr.myUserInfo.timeLimitGameScores.Remove(distanceStr);
  256. LoginMgr.myUserInfo.timeLimitGameScores.Add(distanceStr, this.score);
  257. LoginMgr.myUserInfo.Save();
  258. }
  259. return new object[]{starCount, this.score};
  260. }
  261. public override void Update() {
  262. if (gameMgr.gameOver || pauseTimeCounting) return;
  263. if (this.time > 0) {
  264. if (GlobalData.pkMatchType == PKMatchType.None && UserSettings.ins.trainMode) {
  265. //单人且为训练模式,就不要倒计时了
  266. } else {
  267. this.time -= Time.deltaTime;
  268. }
  269. } else {
  270. this.time = 0;
  271. gameMgr.StopGame();
  272. //添加结算界面
  273. GameObject view = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameSettleView");
  274. GameObject.Instantiate(view);
  275. }
  276. }
  277. public string GetTimeStr()
  278. {
  279. int seconds = (int) Mathf.Ceil(this.time);
  280. string str = "";
  281. int m = seconds / 60;
  282. if (m < 10) {
  283. str += 0;
  284. }
  285. str += m;
  286. str += " : ";
  287. int s = seconds % 60;
  288. if (s < 10)
  289. {
  290. str += 0;
  291. }
  292. str += s;
  293. return str;
  294. }
  295. }
  296. /**双人PK模式 */
  297. public class PKGameMode : GameMode {
  298. public int currentPlayerIndex = 0;
  299. public int[] totalScores = {0, 0};
  300. public float[] currentScores = {0, 0};
  301. public int round = 1;
  302. public int showRoundValue = 0; //回合开始提示值,如果已经提示,则showRoundValue == round
  303. float[] targetDistancesOnRound = {10, 20, 30, 50, 70, 70};
  304. int maxRound = 5;
  305. int[] shootCount = {0, 0};
  306. int maxShootCount = 3;
  307. public float singleShootReadyTime = 20;
  308. public float singleShootReadyMaxTime = 20;
  309. bool singleShootTimeRunning = false;
  310. public static int[] playerRoleIDs = {1, 2};
  311. string[] gameRes = {"平局", "平局"};
  312. //玩家出场顺序
  313. Queue<int> appearPlayerIndexes = new Queue<int>();
  314. //记录可射击的靶子
  315. TargetBody targetBody;
  316. public PKGameMode(GameMgr gameMgr) : base(gameMgr) {
  317. playerRoleIDs = GlobalData.localPK_playerRoleIDs;
  318. InitAppearPlayerIndexes();
  319. currentPlayerIndex = appearPlayerIndexes.Dequeue();
  320. //记录可射击的靶子
  321. targetBody = GameObject.Find("GameArea/TargetObject/TargetBody").GetComponent<TargetBody>();
  322. GameObject.FindObjectOfType<ArmBow>().validTargets.Add(targetBody);
  323. targetBody.SetDistance(targetDistancesOnRound[round - 1]);
  324. //添加游戏界面
  325. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameView");
  326. GameObject.Instantiate(view);
  327. //禁止动作-相机和手臂
  328. BanBowReady();
  329. }
  330. public override void Start() {
  331. TargetView.ins.Show(true);
  332. //添加预备界面
  333. AddReadyView();
  334. }
  335. int[] sequencePlayerIndexes = new int[]{0, 1};
  336. void InitAppearPlayerIndexes()
  337. {
  338. if (round >= 2)
  339. {
  340. if (totalScores[0] < totalScores[1])
  341. {
  342. sequencePlayerIndexes = new int[]{0, 1};
  343. }
  344. else if (totalScores[1] < totalScores[0])
  345. {
  346. sequencePlayerIndexes = new int[]{1, 0};
  347. }
  348. }
  349. for (int i = 0; i < maxShootCount; i++) {
  350. appearPlayerIndexes.Enqueue(sequencePlayerIndexes[0]);
  351. appearPlayerIndexes.Enqueue(sequencePlayerIndexes[1]);
  352. }
  353. }
  354. void AddReadyView()
  355. {
  356. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameReadyView");
  357. GameObject.Instantiate(view);
  358. }
  359. public override void HitTarget(float score) {
  360. currentScores[currentPlayerIndex] += score;
  361. shootCount[currentPlayerIndex]++;
  362. HitTargetNumber.Create(score);
  363. }
  364. public override bool DoNextShoot() {
  365. if (gameMgr.gameOver) return false;
  366. bool nextRound = false;
  367. bool gameEnd = false; //游戏是否结束
  368. if (shootCount[0] == maxShootCount && shootCount[1] == maxShootCount ) {
  369. shootCount = new int[]{0, 0};
  370. nextRound = true;
  371. //更新总比分
  372. if (currentScores[0] == currentScores[1]) {
  373. totalScores[0] += 1;
  374. totalScores[1] += 1;
  375. } else if (currentScores[0] > currentScores[1]) {
  376. totalScores[0] += 2;
  377. } else if (currentScores[0] < currentScores[1]) {
  378. totalScores[1] += 2;
  379. }
  380. //根据总比分判断游戏是否结束
  381. if (totalScores[0] == totalScores[1]) {
  382. if (round == maxRound) {
  383. if (round == 5) {
  384. maxShootCount = 1;
  385. maxRound = 6;
  386. } else {
  387. gameEnd = true;
  388. gameRes = new string[]{"平局", "平局"};
  389. }
  390. }
  391. } else if (totalScores[0] >= 6 && totalScores[0] > totalScores[1]) {
  392. gameEnd = true;
  393. gameRes = new string[]{"胜利", "失败"};
  394. } else if (totalScores[1] >= 6 && totalScores[1] > totalScores[0]) {
  395. gameEnd = true;
  396. gameRes = new string[]{"失败", "胜利"};
  397. }
  398. }
  399. if (gameEnd) {
  400. gameMgr.StopGame();
  401. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameSettleView");
  402. GameObject.Instantiate(view);
  403. return false;
  404. } else {
  405. //进入下一回合?
  406. if (nextRound) {
  407. round++;
  408. currentScores[0] = currentScores[1] = 0;
  409. InitAppearPlayerIndexes();
  410. targetBody.SetDistance(targetDistancesOnRound[round - 1]);
  411. }
  412. //本轮玩家登记
  413. currentPlayerIndex = appearPlayerIndexes.Dequeue();
  414. //准备切换玩家
  415. BanBowReady();
  416. AddReadyView();
  417. //清除箭矢
  418. Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
  419. foreach (var arrow in arrows)
  420. {
  421. try {
  422. GameObject.Destroy(arrow.gameObject);
  423. } catch (UnityException e) {
  424. Debug.Log("Delete Arrow Error\n" + e.Message);
  425. }
  426. }
  427. }
  428. return true;
  429. }
  430. public override object[] Settle() {
  431. return gameRes;
  432. }
  433. public override void Update() {
  434. if (singleShootTimeRunning && !pauseTimeCounting) {
  435. singleShootReadyTime -= Time.deltaTime;
  436. if (singleShootReadyTime <= 0) {
  437. singleShootReadyTime = 0;
  438. singleShootTimeRunning = false;
  439. HitTarget(0);
  440. BanBowReady();
  441. //超时显示
  442. Text timeoutText = PKGameView.ins.transform.Find("TimeoutText").GetComponent<Text>();
  443. Sequence seq = DOTween.Sequence();
  444. seq.Append(timeoutText.DOFade(1, 0.5f));
  445. seq.AppendInterval(1);
  446. seq.Append(timeoutText.DOFade(0, 0.5f));
  447. seq.AppendCallback(delegate(){
  448. if (DoNextShoot()) {
  449. UnbanBowReady();
  450. }
  451. });
  452. }
  453. }
  454. }
  455. public string GetTimeStr()
  456. {
  457. int seconds = (int) Mathf.Ceil(this.singleShootReadyTime);
  458. string str = "";
  459. int m = seconds / 60;
  460. if (m < 10) {
  461. str += 0;
  462. }
  463. str += m;
  464. str += " : ";
  465. int s = seconds % 60;
  466. if (s < 10)
  467. {
  468. str += 0;
  469. }
  470. str += s;
  471. return str;
  472. }
  473. public override void onBowReady() {
  474. singleShootReadyTime = singleShootReadyMaxTime;
  475. singleShootTimeRunning = true;
  476. }
  477. public override void onBowShoot() {
  478. singleShootTimeRunning = false;
  479. }
  480. }