GameMgr.cs 16 KB

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