HunterGameView.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class HunterGameView : MonoBehaviour
  6. {
  7. Text t1;
  8. Text t2;
  9. Text t3;
  10. ChallengeGameMode gameMode;
  11. WolfHuntGameMode gameMode1;
  12. void Start()
  13. {
  14. t1 = this.transform.Find("T1").GetComponent<Text>();
  15. t2 = this.transform.Find("T2").GetComponent<Text>();
  16. t3 = this.transform.Find("T3").GetComponent<Text>();
  17. gameMode = (ChallengeGameMode) GameMgr.ins.gameMode;
  18. if (GameMgr.gameType == 5) {
  19. gameMode1 = (WolfHuntGameMode) gameMode;
  20. t2.gameObject.SetActive(false);
  21. t3.gameObject.SetActive(false);
  22. } else {
  23. hpUI.SetActive(false);
  24. }
  25. }
  26. void FixedUpdate()
  27. {
  28. RenderAnimalCount();
  29. if (GameMgr.gameType == 5) {
  30. RenderHP();
  31. } else {
  32. RenderArrowCount();
  33. RenderTime();
  34. }
  35. if (GameMgr.ins.gameOver) {
  36. Destroy(this.gameObject);
  37. }
  38. }
  39. string[] animalNames = {"野兔", "野鸡", "野狼"};
  40. void RenderAnimalCount() {
  41. string animalName = animalNames[gameMode.animalTypeID];
  42. t1.text = $"剩余{animalName}:{gameMode.animalCount}";
  43. }
  44. void RenderArrowCount() {
  45. t2.text = $"剩余箭矢:{gameMode.arrowCount}";
  46. }
  47. int lastRenderTime = -1;
  48. void RenderTime() {
  49. int curTime = Mathf.CeilToInt(gameMode.time);
  50. if (curTime != lastRenderTime) {
  51. lastRenderTime = curTime;
  52. t3.text = GetTimeStr(curTime);
  53. }
  54. }
  55. string GetTimeStr(int second) {
  56. string str = "";
  57. int m = second / 60;
  58. if (m < 10) {
  59. str += 0;
  60. }
  61. str += m;
  62. str += " : ";
  63. int s = second % 60;
  64. if (s < 10)
  65. {
  66. str += 0;
  67. }
  68. str += s;
  69. return str;
  70. }
  71. [SerializeField] GameObject hpUI;
  72. [SerializeField] Image hpBar;
  73. [SerializeField] Text hpText;
  74. void RenderHP() {
  75. hpBar.fillAmount = Mathf.Lerp(hpBar.fillAmount, (float) gameMode1.hp / gameMode1.hpMax, Time.deltaTime * 6);
  76. hpText.text = $"{gameMode1.hp}/{gameMode1.hpMax}";
  77. }
  78. }