TimeLimitGameMode.cs 5.6 KB

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