TimeLimitGameMode.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single);
  69. }
  70. }
  71. else {
  72. GameMgr.bNavBack = false;
  73. //普通引导时候射击
  74. HomeView.bOpenOtherView = true;
  75. HomeView.openName = "DeviceView";
  76. UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single);
  77. }
  78. }
  79. }
  80. public override bool DoNextShoot() {
  81. return !GameMgr.ins.gameOver;
  82. }
  83. public override object[] Settle() {
  84. int starCount = Mathf.FloorToInt(this.score / this.oneStarScore);
  85. float highestScore = 0;
  86. string distanceStr = distance.ToString();
  87. if (LoginMgr.myUserInfo.timeLimitGameScores.ContainsKey(distanceStr)) {
  88. highestScore = LoginMgr.myUserInfo.timeLimitGameScores[distanceStr];
  89. }
  90. if (this.score > highestScore) {
  91. LoginMgr.myUserInfo.timeLimitGameScores.Remove(distanceStr);
  92. LoginMgr.myUserInfo.timeLimitGameScores.Add(distanceStr, this.score);
  93. LoginMgr.myUserInfo.Save();
  94. }
  95. return new object[]{starCount, this.score};
  96. }
  97. public override void Update() {
  98. #if UNITY_EDITOR
  99. if (Input.GetKey(KeyCode.W) && this.time > 0) { //win
  100. Debug.LogWarning("debug-win");
  101. this.score = 100;
  102. this.time = 0;
  103. }
  104. if (Input.GetKey(KeyCode.F) && this.time > 0) { //fail
  105. Debug.LogWarning("debug-fail");
  106. this.score = 10;
  107. this.time = 0;
  108. }
  109. #endif
  110. if (gameMgr.gameOver || pauseTimeCounting) return;
  111. //不进入计时器
  112. if (GameMgr.turnOffTimer) return;
  113. if (this.time > 0) {
  114. if (GlobalData.pkMatchType == PKMatchType.None && UserSettings.ins.trainMode) {
  115. //单人且为训练模式,就不要倒计时了
  116. } else {
  117. this.time -= Time.deltaTime;
  118. }
  119. if (Billboard.ins && Billboard.ins.bulletManager.NumberOfShotsFired()) {
  120. //超过射击次数,设置运行时间0,等待结束游戏
  121. this.time = 0;
  122. }
  123. } else {
  124. this.time = 0;
  125. gameMgr.StopGame();
  126. //添加结算界面
  127. GameObject view = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameSettleView");
  128. GameObject.Instantiate(view);
  129. }
  130. }
  131. public string GetTimeStr()
  132. {
  133. int seconds = (int) Mathf.Ceil(this.time);
  134. string str = "";
  135. int m = seconds / 60;
  136. if (m < 10) {
  137. str += 0;
  138. }
  139. str += m;
  140. str += " : ";
  141. int s = seconds % 60;
  142. if (s < 10)
  143. {
  144. str += 0;
  145. }
  146. str += s;
  147. return str;
  148. }
  149. }