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