GameMgr.cs 16 KB

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