TimeLimitGameMode.cs 5.3 KB

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