GameMgr.cs 16 KB

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