TimeLimitGameMode.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /**单人限时模式 */
  5. public class TimeLimitGameMode : GameMode {
  6. public static int insCount; //被实例化的次数
  7. public static int[] distanceCanSelected = {10, 20, 30, 50, 70};
  8. public static int distance = 10;
  9. public static int insCountWillTryAgain; //哪一次的实例化会以再次挑战进行----用于再次挑战按钮
  10. public float score = 0;
  11. int oneStarScore = 10;
  12. float time = 60;
  13. TargetBody targetBody;
  14. public TimeLimitGameMode(GameMgr gameMgr) : base(gameMgr) {
  15. insCount++;
  16. //记录可射击的靶子
  17. targetBody = GameObject.Find("GameArea/TargetObject/TargetBody").GetComponent<TargetBody>();
  18. GameObject.FindObjectOfType<ArmBow>().validTargets.Add(targetBody);
  19. //添加游戏界面
  20. GameObject view = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameView");
  21. GameObject.Instantiate(view);
  22. BanBowReady();
  23. }
  24. public override void Start()
  25. {
  26. UnbanBowReady();
  27. if (insCount == insCountWillTryAgain) {
  28. ConfirmSelectedTargetDistance();
  29. } else {
  30. GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameDistanceSelectView"));
  31. }
  32. }
  33. public void ConfirmSelectedTargetDistance()
  34. {
  35. targetBody.SetDistance(distance);
  36. TargetView.ins.Show(true);
  37. gameMgr.StartCoroutine(RenderHighestScoreByDistance());
  38. }
  39. private IEnumerator RenderHighestScoreByDistance() {
  40. yield return null;
  41. yield return null;
  42. if (TimeLimitGameView.ins) TimeLimitGameView.ins.RenderHighestScoreByDistance(distance);
  43. }
  44. public override void HitTarget(float score) {
  45. this.score += score;
  46. HitTargetNumber.Create(score);
  47. }
  48. public override bool DoNextShoot() {
  49. return !GameMgr.ins.gameOver;
  50. }
  51. public override object[] Settle() {
  52. int starCount = Mathf.FloorToInt(this.score / this.oneStarScore);
  53. float highestScore = 0;
  54. string distanceStr = distance.ToString();
  55. if (LoginMgr.myUserInfo.timeLimitGameScores.ContainsKey(distanceStr)) {
  56. highestScore = LoginMgr.myUserInfo.timeLimitGameScores[distanceStr];
  57. }
  58. if (this.score > highestScore) {
  59. LoginMgr.myUserInfo.timeLimitGameScores.Remove(distanceStr);
  60. LoginMgr.myUserInfo.timeLimitGameScores.Add(distanceStr, this.score);
  61. LoginMgr.myUserInfo.Save();
  62. }
  63. return new object[]{starCount, this.score};
  64. }
  65. public override void Update() {
  66. #if UNITY_EDITOR
  67. if (Input.GetKey(KeyCode.W) && this.time > 0) { //win
  68. Debug.LogWarning("debug-win");
  69. this.score = 100;
  70. this.time = 0;
  71. }
  72. if (Input.GetKey(KeyCode.F) && this.time > 0) { //fail
  73. Debug.LogWarning("debug-fail");
  74. this.score = 10;
  75. this.time = 0;
  76. }
  77. #endif
  78. if (gameMgr.gameOver || pauseTimeCounting) return;
  79. if (this.time > 0) {
  80. if (GlobalData.pkMatchType == PKMatchType.None && UserSettings.ins.trainMode) {
  81. //单人且为训练模式,就不要倒计时了
  82. } else {
  83. this.time -= Time.deltaTime;
  84. }
  85. } else {
  86. this.time = 0;
  87. gameMgr.StopGame();
  88. //添加结算界面
  89. GameObject view = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameSettleView");
  90. GameObject.Instantiate(view);
  91. }
  92. }
  93. public string GetTimeStr()
  94. {
  95. int seconds = (int) Mathf.Ceil(this.time);
  96. string str = "";
  97. int m = seconds / 60;
  98. if (m < 10) {
  99. str += 0;
  100. }
  101. str += m;
  102. str += " : ";
  103. int s = seconds % 60;
  104. if (s < 10)
  105. {
  106. str += 0;
  107. }
  108. str += s;
  109. return str;
  110. }
  111. }