TimeLimitGameMode.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using AppUI.Bluetooth;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. /**单人限时模式 */
  7. public class TimeLimitGameMode : GameMode {
  8. public static int insCount; //被实例化的次数
  9. public static int[] distanceCanSelected = {10, 20, 30, 50, 70};
  10. public static int distance = 10;
  11. public static int insCountWillTryAgain; //哪一次的实例化会以再次挑战进行----用于再次挑战按钮
  12. public float score = 0;
  13. int oneStarScore = 10;
  14. float time = 60;
  15. TargetBody targetBody;
  16. public Action onHitTargetEvent;
  17. public TimeLimitGameMode(GameMgr gameMgr) : base(gameMgr) {
  18. insCount++;
  19. //记录可射击的靶子
  20. targetBody = GameObject.Find("GameArea/TargetObject/TargetBody").GetComponent<TargetBody>();
  21. GameObject.FindObjectOfType<ArmBow>().validTargets.Add(targetBody);
  22. //添加游戏界面
  23. GameObject view = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameView");
  24. GameObject.Instantiate(view);
  25. BanBowReady();
  26. }
  27. public override void Start()
  28. {
  29. UnbanBowReady();
  30. if (insCount == insCountWillTryAgain) {
  31. ConfirmSelectedTargetDistance();
  32. } else {
  33. if (GameMgr.bShowDistance) {
  34. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameDistanceSelectView"));
  35. }
  36. }
  37. }
  38. public void ConfirmSelectedTargetDistance()
  39. {
  40. targetBody.SetDistance(distance);
  41. TargetView.ins.Show(true);
  42. gameMgr.StartCoroutine(RenderHighestScoreByDistance());
  43. }
  44. private IEnumerator RenderHighestScoreByDistance() {
  45. yield return null;
  46. yield return null;
  47. if (TimeLimitGameView.ins) TimeLimitGameView.ins.RenderHighestScoreByDistance(distance);
  48. }
  49. public override void HitTarget(float score) {
  50. this.score += score;
  51. HitTargetNumber.Create(score);
  52. onHitTargetEvent?.Invoke();
  53. if(score != 0)
  54. gameMgr.StartCoroutine(NavDeviceView());
  55. }
  56. //跳回homeView 后,跳转到deviceview
  57. IEnumerator NavDeviceView() {
  58. yield return new WaitForSeconds(1.0f);
  59. if (GameMgr.bNavBack)
  60. {
  61. InfraredGuider _infraredGuider = UnityEngine.Object.FindObjectOfType<InfraredGuider>();
  62. if (_infraredGuider != null)
  63. {
  64. //红外光引导射击,如果是显示这个状态
  65. if (_infraredGuider.mTipStep == TipStep.Finish)
  66. {
  67. GameMgr.bNavBack = false;
  68. //如果是红外教程成功
  69. PlayerPrefs.SetInt(SmartBowDeviceHub.ins.Ble.GetInfraredGuiderKey(), 1);
  70. //直接回到主页
  71. UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single);
  72. }
  73. }
  74. else {
  75. GameMgr.bNavBack = false;
  76. //普通引导时候射击
  77. HomeView.bOpenOtherView = true;
  78. HomeView.openName = "DeviceView";
  79. UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single);
  80. }
  81. }
  82. }
  83. public override bool DoNextShoot() {
  84. return !GameMgr.ins.gameOver;
  85. }
  86. public override object[] Settle() {
  87. int starCount = Mathf.FloorToInt(this.score / this.oneStarScore);
  88. float highestScore = 0;
  89. string distanceStr = distance.ToString();
  90. if (LoginMgr.myUserInfo.timeLimitGameScores.ContainsKey(distanceStr)) {
  91. highestScore = LoginMgr.myUserInfo.timeLimitGameScores[distanceStr];
  92. }
  93. if (this.score > highestScore) {
  94. LoginMgr.myUserInfo.timeLimitGameScores.Remove(distanceStr);
  95. LoginMgr.myUserInfo.timeLimitGameScores.Add(distanceStr, this.score);
  96. LoginMgr.myUserInfo.Save();
  97. }
  98. return new object[]{starCount, this.score};
  99. }
  100. public override void Update() {
  101. #if UNITY_EDITOR
  102. if (Input.GetKey(KeyCode.W) && this.time > 0) { //win
  103. Debug.LogWarning("debug-win");
  104. this.score = 100;
  105. this.time = 0;
  106. }
  107. if (Input.GetKey(KeyCode.F) && this.time > 0) { //fail
  108. Debug.LogWarning("debug-fail");
  109. this.score = 10;
  110. this.time = 0;
  111. }
  112. #endif
  113. if (gameMgr.gameOver || pauseTimeCounting) return;
  114. //不进入计时器
  115. if (GameMgr.turnOffTimer) return;
  116. if (this.time > 0) {
  117. if (GlobalData.pkMatchType == PKMatchType.None && UserSettings.ins.trainMode) {
  118. //单人且为训练模式,就不要倒计时了
  119. } else {
  120. this.time -= Time.deltaTime;
  121. }
  122. if (Billboard.ins && Billboard.ins.bulletManager.NumberOfShotsFired()) {
  123. //超过射击次数,设置运行时间0,等待结束游戏
  124. this.time = 0;
  125. }
  126. } else {
  127. this.time = 0;
  128. gameMgr.StopGame();
  129. //添加结算界面
  130. GameObject view = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameSettleView");
  131. GameObject.Instantiate(view);
  132. }
  133. }
  134. public string GetTimeStr()
  135. {
  136. int seconds = (int) Mathf.Ceil(this.time);
  137. string str = "";
  138. int m = seconds / 60;
  139. if (m < 10) {
  140. str += 0;
  141. }
  142. str += m;
  143. str += " : ";
  144. int s = seconds % 60;
  145. if (s < 10)
  146. {
  147. str += 0;
  148. }
  149. str += s;
  150. return str;
  151. }
  152. }