GameMgr.cs 16 KB

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