GameMgr.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 ConfirmSelectedTargetDistance()
  192. {
  193. targetBody.SetDistance(distance);
  194. if (TimeLimitGameView.ins) TimeLimitGameView.ins.RenderHighestScoreByDistance(distance);
  195. TargetView.ins.Show(true);
  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 = 0;
  207. string distanceStr = distance.ToString();
  208. System.Object highestScoreObj = LoginMgr.myUserInfo.timeLimitGameHighestScores[distanceStr];
  209. if (highestScoreObj != null) highestScore = int.Parse(highestScoreObj.ToString());
  210. if (this.score > highestScore) {
  211. LoginMgr.myUserInfo.timeLimitGameHighestScores.Remove(distanceStr);
  212. LoginMgr.myUserInfo.timeLimitGameHighestScores.Add(distanceStr, this.score);
  213. LoginMgr.myUserInfo.Save();
  214. }
  215. return new object[]{starCount, this.score};
  216. }
  217. public override void Update() {
  218. if (gameMgr.gameOver || pauseTimeCounting) return;
  219. if (this.time > 0) {
  220. this.time -= Time.deltaTime;
  221. } else {
  222. this.time = 0;
  223. gameMgr.gameOver = true;
  224. gameMgr.StopGame();
  225. //添加结算界面
  226. GameObject view = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameSettleView");
  227. GameObject.Instantiate(view);
  228. }
  229. }
  230. public string GetTimeStr()
  231. {
  232. int seconds = (int) Mathf.Ceil(this.time);
  233. string str = "";
  234. int m = seconds / 60;
  235. if (m < 10) {
  236. str += 0;
  237. }
  238. str += m;
  239. str += " : ";
  240. int s = seconds % 60;
  241. if (s < 10)
  242. {
  243. str += 0;
  244. }
  245. str += s;
  246. return str;
  247. }
  248. }
  249. /**双人PK模式 */
  250. public class PKGameMode : GameMode {
  251. public int currentPlayerIndex = 0;
  252. public int[] totalScores = {0, 0};
  253. public int[] currentScores = {0, 0};
  254. public int round = 1;
  255. public int showRoundValue = 0; //回合开始提示值,如果已经提示,则showRoundValue == round
  256. float[] targetDistancesOnRound = {10, 20, 30, 50, 70, 70};
  257. int maxRound = 5;
  258. int[] shootCount = {0, 0};
  259. int maxShootCount = 3;
  260. public float singleShootReadyTime = 20;
  261. public float singleShootReadyMaxTime = 20;
  262. bool singleShootTimeRunning = false;
  263. public static int[] playerRoleIDs = {1, 2};
  264. string[] gameRes = {"平局", "平局"};
  265. //玩家出场顺序
  266. Queue<int> appearPlayerIndexes = new Queue<int>();
  267. //记录可射击的靶子
  268. TargetBody targetBody;
  269. public PKGameMode(GameMgr gameMgr) : base(gameMgr) {
  270. InitAppearPlayerIndexes();
  271. currentPlayerIndex = appearPlayerIndexes.Dequeue();
  272. //记录可射击的靶子
  273. targetBody = GameObject.Find("GameArea/010/TargetBody").GetComponent<TargetBody>();
  274. GameObject.Find("Main Camera/ArmBow").GetComponent<ArmBow>().validTargets.Add(targetBody);
  275. targetBody.SetDistance(targetDistancesOnRound[round - 1]);
  276. //添加游戏界面
  277. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameView");
  278. GameObject.Instantiate(view);
  279. //禁止动作-相机和手臂
  280. BanBowReady();
  281. }
  282. public override void Start() {
  283. TargetView.ins.Show(true);
  284. //添加预备界面
  285. AddReadyView();
  286. }
  287. int[] sequencePlayerIndexes = new int[]{0, 1};
  288. void InitAppearPlayerIndexes()
  289. {
  290. if (round >= 2)
  291. {
  292. if (totalScores[0] < totalScores[1])
  293. {
  294. sequencePlayerIndexes = new int[]{0, 1};
  295. }
  296. else if (totalScores[1] < totalScores[0])
  297. {
  298. sequencePlayerIndexes = new int[]{1, 0};
  299. }
  300. }
  301. for (int i = 0; i < maxShootCount; i++) {
  302. appearPlayerIndexes.Enqueue(sequencePlayerIndexes[0]);
  303. appearPlayerIndexes.Enqueue(sequencePlayerIndexes[1]);
  304. }
  305. }
  306. void AddReadyView()
  307. {
  308. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameReadyView");
  309. GameObject.Instantiate(view);
  310. }
  311. public override void HitTarget(int score) {
  312. currentScores[currentPlayerIndex] += score;
  313. shootCount[currentPlayerIndex]++;
  314. HitTargetNumber.Create(score);
  315. }
  316. public override bool DoNextShoot() {
  317. if (gameMgr.gameOver) return false;
  318. bool nextRound = false;
  319. bool gameEnd = false; //游戏是否结束
  320. if (shootCount[0] == maxShootCount && shootCount[1] == maxShootCount ) {
  321. shootCount = new int[]{0, 0};
  322. nextRound = true;
  323. //更新总比分
  324. if (currentScores[0] == currentScores[1]) {
  325. totalScores[0] += 1;
  326. totalScores[1] += 1;
  327. } else if (currentScores[0] > currentScores[1]) {
  328. totalScores[0] += 2;
  329. } else if (currentScores[0] < currentScores[1]) {
  330. totalScores[1] += 2;
  331. }
  332. //根据总比分判断游戏是否结束
  333. if (totalScores[0] == totalScores[1]) {
  334. if (round == maxRound) {
  335. if (round == 5) {
  336. maxShootCount = 1;
  337. maxRound = 6;
  338. } else {
  339. gameEnd = true;
  340. gameRes = new string[]{"平局", "平局"};
  341. }
  342. }
  343. } else if (totalScores[0] >= 6 && totalScores[0] > totalScores[1]) {
  344. gameEnd = true;
  345. gameRes = new string[]{"胜利", "失败"};
  346. } else if (totalScores[1] >= 6 && totalScores[1] > totalScores[0]) {
  347. gameEnd = true;
  348. gameRes = new string[]{"失败", "胜利"};
  349. }
  350. }
  351. if (gameEnd) {
  352. gameMgr.gameOver = true;
  353. gameMgr.StopGame();
  354. GameObject view = Resources.Load<GameObject>("Prefabs/Views/PKGameSettleView");
  355. GameObject.Instantiate(view);
  356. return false;
  357. } else {
  358. //进入下一回合?
  359. if (nextRound) {
  360. round++;
  361. currentScores[0] = currentScores[1] = 0;
  362. InitAppearPlayerIndexes();
  363. targetBody.SetDistance(targetDistancesOnRound[round - 1]);
  364. }
  365. //本轮玩家登记
  366. currentPlayerIndex = appearPlayerIndexes.Dequeue();
  367. //准备切换玩家
  368. BanBowReady();
  369. AddReadyView();
  370. //清除箭矢
  371. Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
  372. foreach (var arrow in arrows)
  373. {
  374. try {
  375. GameObject.Destroy(arrow.gameObject);
  376. } catch (UnityException e) {
  377. Debug.Log("Delete Arrow Error\n" + e.Message);
  378. }
  379. }
  380. }
  381. return true;
  382. }
  383. public override object[] Settle() {
  384. return gameRes;
  385. }
  386. public override void Update() {
  387. if (singleShootTimeRunning && !pauseTimeCounting) {
  388. singleShootReadyTime -= Time.deltaTime;
  389. if (singleShootReadyTime <= 0) {
  390. singleShootReadyTime = 0;
  391. singleShootTimeRunning = false;
  392. HitTarget(0);
  393. BanBowReady();
  394. //超时显示
  395. Text timeoutText = PKGameView.ins.transform.Find("TimeoutText").GetComponent<Text>();
  396. Sequence seq = DOTween.Sequence();
  397. seq.Append(timeoutText.DOFade(1, 0.5f));
  398. seq.AppendInterval(1);
  399. seq.Append(timeoutText.DOFade(0, 0.5f));
  400. seq.AppendCallback(delegate(){
  401. if (DoNextShoot()) {
  402. UnbanBowReady();
  403. }
  404. });
  405. }
  406. }
  407. }
  408. public string GetTimeStr()
  409. {
  410. int seconds = (int) Mathf.Ceil(this.singleShootReadyTime);
  411. string str = "";
  412. int m = seconds / 60;
  413. if (m < 10) {
  414. str += 0;
  415. }
  416. str += m;
  417. str += " : ";
  418. int s = seconds % 60;
  419. if (s < 10)
  420. {
  421. str += 0;
  422. }
  423. str += s;
  424. return str;
  425. }
  426. public override void onBowReady() {
  427. singleShootReadyTime = singleShootReadyMaxTime;
  428. singleShootTimeRunning = true;
  429. }
  430. public override void onBowShoot() {
  431. singleShootTimeRunning = false;
  432. }
  433. }