GameMgr.cs 14 KB

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