GameMgr.cs 15 KB

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