GameMgr.cs 14 KB

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