Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / RoomUI.cs
@小李xl 小李xl on 29 Oct 2022 4 KB 小修改
  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. /// <summary>
  29. /// 互动提示组件
  30. /// </summary>
  31. public InteractiveTipBar InteractiveTipBar { get; private set; }
  32. /// <summary>
  33. /// 换弹进度组件
  34. /// </summary>
  35. public ReloadBar ReloadBar { get; private set; }
  36.  
  37. private NinePatchRect hpSlot;
  38. private NinePatchRect shieldSlot;
  39. private TextureRect hpBar;
  40. private TextureRect shieldBar;
  41. private Label bulletText;
  42. private TextureRect gunSprite;
  43.  
  44. public override void _EnterTree()
  45. {
  46. Current = this;
  47. }
  48.  
  49. public override void _Ready()
  50. {
  51. hpSlot = GetNode<NinePatchRect>("Control/HealthBar/HpSlot");
  52. shieldSlot = GetNode<NinePatchRect>("Control/HealthBar/ShieldSlot");
  53. hpBar = GetNode<TextureRect>("Control/HealthBar/HpSlot/HpBar");
  54. shieldBar = GetNode<TextureRect>("Control/HealthBar/ShieldSlot/ShieldBar");
  55.  
  56. bulletText = GetNode<Label>("Control/GunBar/BulletText");
  57. gunSprite = GetNode<TextureRect>("Control/GunBar/GunSprite");
  58.  
  59. InteractiveTipBar = GetNode<InteractiveTipBar>("GlobalNode/InteractiveTipBar");
  60. InteractiveTipBar.Visible = false;
  61.  
  62. ReloadBar = GetNode<ReloadBar>("GlobalNode/ReloadBar");
  63. ReloadBar.Visible = false;
  64.  
  65. //将 GlobalNode 节点下的 ui 节点放入全局坐标中
  66. var tempNode = GetNode("GlobalNode");
  67. var root = RoomManager.Current;
  68. var count = tempNode.GetChildCount();
  69. for (int i = count - 1; i >= 0; i--)
  70. {
  71. var node = tempNode.GetChild(i);
  72. tempNode.RemoveChild(node);
  73. root.CallDeferred("add_child", node);
  74. }
  75. tempNode.CallDeferred("queue_free");
  76. }
  77.  
  78. /// <summary>
  79. /// 设置最大血量
  80. /// </summary>
  81. public void SetMaxHp(int maxHp)
  82. {
  83. MaxHp = Mathf.Max(maxHp, 0);
  84. hpSlot.RectSize = new Vector2(maxHp + 3, hpSlot.RectSize.y);
  85. if (Hp > maxHp)
  86. {
  87. SetHp(maxHp);
  88. }
  89. }
  90.  
  91. /// <summary>
  92. /// 设置最大护盾值
  93. /// </summary>
  94. public void SetMaxShield(int maxShield)
  95. {
  96. MaxShield = Mathf.Max(maxShield, 0); ;
  97. shieldSlot.RectSize = new Vector2(maxShield + 2, shieldSlot.RectSize.y);
  98. if (Shield > MaxShield)
  99. {
  100. SetShield(maxShield);
  101. }
  102. }
  103.  
  104. /// <summary>
  105. /// 设置当前血量
  106. /// </summary>
  107. public void SetHp(int hp)
  108. {
  109. Hp = Mathf.Clamp(hp, 0, MaxHp);
  110. hpBar.RectSize = new Vector2(hp, hpBar.RectSize.y);
  111. }
  112.  
  113. /// <summary>
  114. /// 设置护盾值
  115. /// </summary>
  116. public void SetShield(int shield)
  117. {
  118. Shield = Mathf.Clamp(shield, 0, MaxShield);
  119. shieldBar.RectSize = new Vector2(shield, shieldBar.RectSize.y);
  120. }
  121.  
  122. /// <summary>
  123. /// 玩家受到伤害
  124. /// </summary>
  125. public void Hit(int num)
  126. {
  127.  
  128. }
  129.  
  130. /// <summary>
  131. /// 设置显示在 ui 上的枪的纹理
  132. /// </summary>
  133. /// <param name="gun">纹理</param>
  134. public void SetGunTexture(Texture gun)
  135. {
  136. if (gun != null)
  137. {
  138. gunSprite.Texture = gun;
  139. gunSprite.Visible = true;
  140. bulletText.Visible = true;
  141. }
  142. else
  143. {
  144. gunSprite.Visible = false;
  145. bulletText.Visible = false;
  146. }
  147. }
  148.  
  149. /// <summary>
  150. /// 设置弹药数据
  151. /// </summary>
  152. /// <param name="curr">当前弹夹弹药量</param>
  153. /// <param name="total">剩余弹药总数</param>
  154. public void SetAmmunition(int curr, int total)
  155. {
  156. bulletText.Text = curr + " / " + total;
  157. }
  158. }