Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / RoomUI.cs
  1. using Godot;
  2.  
  3. /// <summary>
  4. /// 房间中的ui
  5. /// </summary>
  6. public partial class RoomUI : Control
  7. {
  8. //public static RoomUI Current { get; private set; }
  9.  
  10. /// <summary>
  11. /// 当前血量
  12. /// </summary>
  13. public int Hp { get; private set; }
  14. /// <summary>
  15. /// 最大血量
  16. /// </summary>
  17. public int MaxHp { get; private set; }
  18. /// <summary>
  19. /// 当前护盾值
  20. /// </summary>
  21. public int Shield { get; private set; }
  22. /// <summary>
  23. /// 最大护盾值
  24. /// </summary>
  25. public int MaxShield { get; private set; }
  26. /// <summary>
  27. /// 互动提示组件
  28. /// </summary>
  29. public InteractiveTipBar InteractiveTipBar { get; private set; }
  30. /// <summary>
  31. /// 换弹进度组件
  32. /// </summary>
  33. public ReloadBar ReloadBar { get; private set; }
  34.  
  35. private NinePatchRect hpSlot;
  36. private NinePatchRect shieldSlot;
  37. private TextureRect hpBar;
  38. private TextureRect shieldBar;
  39. private Label bulletText;
  40. private TextureRect gunSprite;
  41.  
  42. public override void _EnterTree()
  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. InteractiveTipBar = GetNode<InteractiveTipBar>("ViewNode/InteractiveTipBar");
  53. InteractiveTipBar.Visible = false;
  54.  
  55. ReloadBar = GetNode<ReloadBar>("ViewNode/ReloadBar");
  56. ReloadBar.Visible = false;
  57. }
  58.  
  59. public override void _Ready()
  60. {
  61. //将 GlobalNode 节点下的 ui 节点放入全局坐标中
  62. var globalNode = GetNode("GlobalNode");
  63. var root = GameApplication.Instance.GlobalNodeRoot;
  64. var count = globalNode.GetChildCount();
  65. for (int i = count - 1; i >= 0; i--)
  66. {
  67. var node = globalNode.GetChild(i);
  68. globalNode.RemoveChild(node);
  69. root.CallDeferred("add_child", node);
  70. }
  71. globalNode.CallDeferred("queue_free");
  72. //将 ViewNode 节点放到 SubViewport 下
  73. var viewNode = GetNode("ViewNode");
  74. var viewport = GameApplication.Instance.SubViewport;
  75. count = viewNode.GetChildCount();
  76. for (int i = count - 1; i >= 0; i--)
  77. {
  78. var node = viewNode.GetChild(i);
  79. viewNode.RemoveChild(node);
  80. viewport.CallDeferred("add_child", node);
  81. }
  82. viewNode.CallDeferred("queue_free");
  83. }
  84.  
  85. public override void _Process(double delta)
  86. {
  87. }
  88.  
  89. public override void _PhysicsProcess(double delta)
  90. {
  91. // var colorRect = GetNode<ColorRect>("ColorRect");
  92. // var pos = GameApplication.Instance.ViewToGlobalPosition(GameApplication.Instance.Node3D.Player.GlobalPosition);
  93. // colorRect.SetGlobalPosition(pos);
  94. //GD.Print("pos: " + pos + ", " + colorRect.GlobalPosition);
  95. }
  96.  
  97. /// <summary>
  98. /// 设置最大血量
  99. /// </summary>
  100. public void SetMaxHp(int maxHp)
  101. {
  102. MaxHp = Mathf.Max(maxHp, 0);
  103. hpSlot.Size = new Vector2(maxHp + 3, hpSlot.Size.Y);
  104. if (Hp > maxHp)
  105. {
  106. SetHp(maxHp);
  107. }
  108. }
  109.  
  110. /// <summary>
  111. /// 设置最大护盾值
  112. /// </summary>
  113. public void SetMaxShield(int maxShield)
  114. {
  115. MaxShield = Mathf.Max(maxShield, 0); ;
  116. shieldSlot.Size = new Vector2(maxShield + 2, shieldSlot.Size.Y);
  117. if (Shield > MaxShield)
  118. {
  119. SetShield(maxShield);
  120. }
  121. }
  122.  
  123. /// <summary>
  124. /// 设置当前血量
  125. /// </summary>
  126. public void SetHp(int hp)
  127. {
  128. Hp = Mathf.Clamp(hp, 0, MaxHp);
  129. hpBar.Size = new Vector2(hp, hpBar.Size.Y);
  130. }
  131.  
  132. /// <summary>
  133. /// 设置护盾值
  134. /// </summary>
  135. public void SetShield(int shield)
  136. {
  137. Shield = Mathf.Clamp(shield, 0, MaxShield);
  138. shieldBar.Size = new Vector2(shield, shieldBar.Size.Y);
  139. }
  140.  
  141. /// <summary>
  142. /// 玩家受到伤害
  143. /// </summary>
  144. public void Hit(int num)
  145. {
  146.  
  147. }
  148.  
  149. /// <summary>
  150. /// 设置显示在 ui 上的枪的纹理
  151. /// </summary>
  152. /// <param name="gun">纹理</param>
  153. public void SetGunTexture(Texture2D gun)
  154. {
  155. if (gun != null)
  156. {
  157. gunSprite.Texture = gun;
  158. gunSprite.Visible = true;
  159. bulletText.Visible = true;
  160. }
  161. else
  162. {
  163. gunSprite.Visible = false;
  164. bulletText.Visible = false;
  165. }
  166. }
  167.  
  168. /// <summary>
  169. /// 设置弹药数据
  170. /// </summary>
  171. /// <param name="curr">当前弹夹弹药量</param>
  172. /// <param name="total">剩余弹药总数</param>
  173. public void SetAmmunition(int curr, int total)
  174. {
  175. bulletText.Text = curr + " / " + total;
  176. }
  177. }