TimeLimitGameMode.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. HomeView.bOpenOtherView = true;
  62. HomeView.openName = "DeviceView";
  63. UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single);
  64. }
  65. }
  66. public override bool DoNextShoot() {
  67. return !GameMgr.ins.gameOver;
  68. }
  69. public override object[] Settle() {
  70. int starCount = Mathf.FloorToInt(this.score / this.oneStarScore);
  71. float highestScore = 0;
  72. string distanceStr = distance.ToString();
  73. if (LoginMgr.myUserInfo.timeLimitGameScores.ContainsKey(distanceStr)) {
  74. highestScore = LoginMgr.myUserInfo.timeLimitGameScores[distanceStr];
  75. }
  76. if (this.score > highestScore) {
  77. LoginMgr.myUserInfo.timeLimitGameScores.Remove(distanceStr);
  78. LoginMgr.myUserInfo.timeLimitGameScores.Add(distanceStr, this.score);
  79. LoginMgr.myUserInfo.Save();
  80. }
  81. return new object[]{starCount, this.score};
  82. }
  83. public override void Update() {
  84. #if UNITY_EDITOR
  85. if (Input.GetKey(KeyCode.W) && this.time > 0) { //win
  86. Debug.LogWarning("debug-win");
  87. this.score = 100;
  88. this.time = 0;
  89. }
  90. if (Input.GetKey(KeyCode.F) && this.time > 0) { //fail
  91. Debug.LogWarning("debug-fail");
  92. this.score = 10;
  93. this.time = 0;
  94. }
  95. #endif
  96. if (gameMgr.gameOver || pauseTimeCounting) return;
  97. //不进入计时器
  98. if (GameMgr.turnOffTimer) return;
  99. if (this.time > 0) {
  100. if (GlobalData.pkMatchType == PKMatchType.None && UserSettings.ins.trainMode) {
  101. //单人且为训练模式,就不要倒计时了
  102. } else {
  103. this.time -= Time.deltaTime;
  104. }
  105. } else {
  106. this.time = 0;
  107. gameMgr.StopGame();
  108. //添加结算界面
  109. GameObject view = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameSettleView");
  110. GameObject.Instantiate(view);
  111. }
  112. }
  113. public string GetTimeStr()
  114. {
  115. int seconds = (int) Mathf.Ceil(this.time);
  116. string str = "";
  117. int m = seconds / 60;
  118. if (m < 10) {
  119. str += 0;
  120. }
  121. str += m;
  122. str += " : ";
  123. int s = seconds % 60;
  124. if (s < 10)
  125. {
  126. str += 0;
  127. }
  128. str += s;
  129. return str;
  130. }
  131. }