TimeLimitGameMode.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 (gameMgr.gameOver || pauseTimeCounting) return;
  67. if (this.time > 0) {
  68. if (GlobalData.pkMatchType == PKMatchType.None && UserSettings.ins.trainMode) {
  69. //单人且为训练模式,就不要倒计时了
  70. } else {
  71. this.time -= Time.deltaTime;
  72. }
  73. } else {
  74. this.time = 0;
  75. gameMgr.StopGame();
  76. //添加结算界面
  77. GameObject view = Resources.Load<GameObject>("Prefabs/Views/TimeLimitGameSettleView");
  78. GameObject.Instantiate(view);
  79. }
  80. }
  81. public string GetTimeStr()
  82. {
  83. int seconds = (int) Mathf.Ceil(this.time);
  84. string str = "";
  85. int m = seconds / 60;
  86. if (m < 10) {
  87. str += 0;
  88. }
  89. str += m;
  90. str += " : ";
  91. int s = seconds % 60;
  92. if (s < 10)
  93. {
  94. str += 0;
  95. }
  96. str += s;
  97. return str;
  98. }
  99. }