Newer
Older
DungeonShooting / DungeonShooting_Godot / src / ui / RoomUI.cs
  1. using System;
  2. using Godot;
  3.  
  4. /// <summary>
  5. /// 房间中的ui
  6. /// </summary>
  7. public class RoomUI : Control
  8. {
  9.  
  10. public static RoomUI Current { get; private set; }
  11.  
  12. /// <summary>
  13. /// 当前血量
  14. /// </summary>
  15. public int Hp { get; private set; }
  16. /// <summary>
  17. /// 最大血量
  18. /// </summary>
  19. public int MaxHp { get; private set; }
  20. /// <summary>
  21. /// 当前护盾值
  22. /// </summary>
  23. public int Shield { get; private set; }
  24. /// <summary>
  25. /// 最大护盾值
  26. /// </summary>
  27. public int MaxShield { get; private set; }
  28.  
  29.  
  30. private NinePatchRect hpSlot;
  31. private NinePatchRect shieldSlot;
  32. private TextureRect hpBar;
  33. private TextureRect shieldBar;
  34. private Label bulletText;
  35. private TextureRect gunSprite;
  36.  
  37. public override void _EnterTree()
  38. {
  39. Current = this;
  40. }
  41.  
  42. public override void _Ready()
  43. {
  44. hpSlot = GetNode<NinePatchRect>("Control/HealthBar/HpSlot");
  45. shieldSlot = GetNode<NinePatchRect>("Control/HealthBar/ShieldSlot");
  46. hpBar = GetNode<TextureRect>("Control/HealthBar/HpSlot/HpBar");
  47. shieldBar = GetNode<TextureRect>("Control/HealthBar/ShieldSlot/ShieldBar");
  48.  
  49. bulletText = GetNode<Label>("Control/GunBar/BulletText");
  50. gunSprite = GetNode<TextureRect>("Control/GunBar/GunSprite");
  51. }
  52.  
  53. /// <summary>
  54. /// 设置最大血量
  55. /// </summary>
  56. public void SetMaxHp(int maxHp)
  57. {
  58. MaxHp = Mathf.Max(maxHp, 0);
  59. hpSlot.RectSize = new Vector2(maxHp + 3, hpSlot.RectSize.y);
  60. if (Hp > maxHp)
  61. {
  62. SetHp(maxHp);
  63. }
  64. }
  65.  
  66. /// <summary>
  67. /// 设置最大护盾值
  68. /// </summary>
  69. public void SetMaxShield(int maxShield)
  70. {
  71. MaxShield = Mathf.Max(maxShield, 0); ;
  72. shieldSlot.RectSize = new Vector2(maxShield + 2, shieldSlot.RectSize.y);
  73. if (Shield > MaxShield)
  74. {
  75. SetShield(maxShield);
  76. }
  77. }
  78.  
  79. /// <summary>
  80. /// 设置当前血量
  81. /// </summary>
  82. public void SetHp(int hp)
  83. {
  84. Hp = Mathf.Clamp(hp, 0, MaxHp);
  85. hpBar.RectSize = new Vector2(hp, hpBar.RectSize.y);
  86. }
  87.  
  88. /// <summary>
  89. /// 设置护盾值
  90. /// </summary>
  91. public void SetShield(int shield)
  92. {
  93. Shield = Mathf.Clamp(shield, 0, MaxShield);
  94. shieldBar.RectSize = new Vector2(shield, shieldBar.RectSize.y);
  95. }
  96.  
  97. /// <summary>
  98. /// 玩家受到伤害
  99. /// </summary>
  100. public void Hit(int num)
  101. {
  102.  
  103. }
  104.  
  105. /// <summary>
  106. /// 设置显示在 ui 上的枪的纹理
  107. /// </summary>
  108. /// <param name="gun">纹理</param>
  109. public void SetGunTexture(Texture gun)
  110. {
  111. if (gun != null)
  112. {
  113. gunSprite.Texture = gun;
  114. gunSprite.Visible = true;
  115. bulletText.Visible = true;
  116. }
  117. else
  118. {
  119. gunSprite.Visible = false;
  120. bulletText.Visible = false;
  121. }
  122. }
  123.  
  124. /// <summary>
  125. /// 设置弹药数据
  126. /// </summary>
  127. /// <param name="curr">当前弹夹弹药量</param>
  128. /// <param name="total">剩余弹药总数</param>
  129. public void SetAmmunition(int curr, int total)
  130. {
  131. bulletText.Text = curr + " / " + total;
  132. }
  133. }