GameMgr.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 = 9;
  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. this.time -= Time.deltaTime;
  261. } else {
  262. this.time = 0;
  263. gameMgr.StopGame();
  264. //添加结算界面
  265. GameObject view = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameSettleView");
  266. GameObject.Instantiate(view);
  267. }
  268. }
  269. public string GetTimeStr()
  270. {
  271. int seconds = (int) Mathf.Ceil(this.time);
  272. string str = "";
  273. int m = seconds / 60;
  274. if (m < 10) {
  275. str += 0;
  276. }
  277. str += m;
  278. str += " : ";
  279. int s = seconds % 60;
  280. if (s < 10)
  281. {
  282. str += 0;
  283. }
  284. str += s;
  285. return str;
  286. }
  287. }
  288. /**双人PK模式 */
  289. public class PKGameMode : GameMode {
  290. public int currentPlayerIndex = 0;
  291. public int[] totalScores = {0, 0};
  292. public float[] currentScores = {0, 0};
  293. public int round = 1;
  294. public int showRoundValue = 0; //回合开始提示值,如果已经提示,则showRoundValue == round
  295. float[] targetDistancesOnRound = {10, 20, 30, 50, 70, 70};
  296. int maxRound = 5;
  297. int[] shootCount = {0, 0};
  298. int maxShootCount = 3;
  299. public float singleShootReadyTime = 20;
  300. public float singleShootReadyMaxTime = 20;
  301. bool singleShootTimeRunning = false;
  302. public static int[] playerRoleIDs = {1, 2};
  303. string[] gameRes = {"平局", "平局"};
  304. //玩家出场顺序
  305. Queue<int> appearPlayerIndexes = new Queue<int>();
  306. //记录可射击的靶子
  307. TargetBody targetBody;
  308. public PKGameMode(GameMgr gameMgr) : base(gameMgr) {
  309. playerRoleIDs = GlobalData.localPK_playerRoleIDs;
  310. InitAppearPlayerIndexes();
  311. currentPlayerIndex = appearPlayerIndexes.Dequeue();
  312. //记录可射击的靶子
  313. targetBody = GameObject.Find("GameArea/TargetObject/TargetBody").GetComponent<TargetBody>();
  314. GameObject.FindObjectOfType<ArmBow>().validTargets.Add(targetBody);
  315. targetBody.SetDistance(targetDistancesOnRound[round - 1]);
  316. //添加游戏界面
  317. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameView");
  318. GameObject.Instantiate(view);
  319. //禁止动作-相机和手臂
  320. BanBowReady();
  321. }
  322. public override void Start() {
  323. TargetView.ins.Show(true);
  324. //添加预备界面
  325. AddReadyView();
  326. }
  327. int[] sequencePlayerIndexes = new int[]{0, 1};
  328. void InitAppearPlayerIndexes()
  329. {
  330. if (round >= 2)
  331. {
  332. if (totalScores[0] < totalScores[1])
  333. {
  334. sequencePlayerIndexes = new int[]{0, 1};
  335. }
  336. else if (totalScores[1] < totalScores[0])
  337. {
  338. sequencePlayerIndexes = new int[]{1, 0};
  339. }
  340. }
  341. for (int i = 0; i < maxShootCount; i++) {
  342. appearPlayerIndexes.Enqueue(sequencePlayerIndexes[0]);
  343. appearPlayerIndexes.Enqueue(sequencePlayerIndexes[1]);
  344. }
  345. }
  346. void AddReadyView()
  347. {
  348. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameReadyView");
  349. GameObject.Instantiate(view);
  350. }
  351. public override void HitTarget(float score) {
  352. currentScores[currentPlayerIndex] += score;
  353. shootCount[currentPlayerIndex]++;
  354. HitTargetNumber.Create(score);
  355. }
  356. public override bool DoNextShoot() {
  357. if (gameMgr.gameOver) return false;
  358. bool nextRound = false;
  359. bool gameEnd = false; //游戏是否结束
  360. if (shootCount[0] == maxShootCount && shootCount[1] == maxShootCount ) {
  361. shootCount = new int[]{0, 0};
  362. nextRound = true;
  363. //更新总比分
  364. if (currentScores[0] == currentScores[1]) {
  365. totalScores[0] += 1;
  366. totalScores[1] += 1;
  367. } else if (currentScores[0] > currentScores[1]) {
  368. totalScores[0] += 2;
  369. } else if (currentScores[0] < currentScores[1]) {
  370. totalScores[1] += 2;
  371. }
  372. //根据总比分判断游戏是否结束
  373. if (totalScores[0] == totalScores[1]) {
  374. if (round == maxRound) {
  375. if (round == 5) {
  376. maxShootCount = 1;
  377. maxRound = 6;
  378. } else {
  379. gameEnd = true;
  380. gameRes = new string[]{"平局", "平局"};
  381. }
  382. }
  383. } else if (totalScores[0] >= 6 && totalScores[0] > totalScores[1]) {
  384. gameEnd = true;
  385. gameRes = new string[]{"胜利", "失败"};
  386. } else if (totalScores[1] >= 6 && totalScores[1] > totalScores[0]) {
  387. gameEnd = true;
  388. gameRes = new string[]{"失败", "胜利"};
  389. }
  390. }
  391. if (gameEnd) {
  392. gameMgr.StopGame();
  393. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameSettleView");
  394. GameObject.Instantiate(view);
  395. return false;
  396. } else {
  397. //进入下一回合?
  398. if (nextRound) {
  399. round++;
  400. currentScores[0] = currentScores[1] = 0;
  401. InitAppearPlayerIndexes();
  402. targetBody.SetDistance(targetDistancesOnRound[round - 1]);
  403. }
  404. //本轮玩家登记
  405. currentPlayerIndex = appearPlayerIndexes.Dequeue();
  406. //准备切换玩家
  407. BanBowReady();
  408. AddReadyView();
  409. //清除箭矢
  410. Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
  411. foreach (var arrow in arrows)
  412. {
  413. try {
  414. GameObject.Destroy(arrow.gameObject);
  415. } catch (UnityException e) {
  416. Debug.Log("Delete Arrow Error\n" + e.Message);
  417. }
  418. }
  419. }
  420. return true;
  421. }
  422. public override object[] Settle() {
  423. return gameRes;
  424. }
  425. public override void Update() {
  426. if (singleShootTimeRunning && !pauseTimeCounting) {
  427. singleShootReadyTime -= Time.deltaTime;
  428. if (singleShootReadyTime <= 0) {
  429. singleShootReadyTime = 0;
  430. singleShootTimeRunning = false;
  431. HitTarget(0);
  432. BanBowReady();
  433. //超时显示
  434. Text timeoutText = PKGameView.ins.transform.Find("TimeoutText").GetComponent<Text>();
  435. Sequence seq = DOTween.Sequence();
  436. seq.Append(timeoutText.DOFade(1, 0.5f));
  437. seq.AppendInterval(1);
  438. seq.Append(timeoutText.DOFade(0, 0.5f));
  439. seq.AppendCallback(delegate(){
  440. if (DoNextShoot()) {
  441. UnbanBowReady();
  442. }
  443. });
  444. }
  445. }
  446. }
  447. public string GetTimeStr()
  448. {
  449. int seconds = (int) Mathf.Ceil(this.singleShootReadyTime);
  450. string str = "";
  451. int m = seconds / 60;
  452. if (m < 10) {
  453. str += 0;
  454. }
  455. str += m;
  456. str += " : ";
  457. int s = seconds % 60;
  458. if (s < 10)
  459. {
  460. str += 0;
  461. }
  462. str += s;
  463. return str;
  464. }
  465. public override void onBowReady() {
  466. singleShootReadyTime = singleShootReadyMaxTime;
  467. singleShootTimeRunning = true;
  468. }
  469. public override void onBowShoot() {
  470. singleShootTimeRunning = false;
  471. }
  472. }