GameMgr.cs 15 KB

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