GameMgr.cs 14 KB

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