Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / roomUI / RoomUIPanel.cs
@小李xl 小李xl on 12 Mar 2023 4 KB UiGenerator 生成图层带前缀 L_
  1. using Godot;
  2.  
  3. namespace UI;
  4.  
  5. /// <summary>
  6. /// 房间中的ui
  7. /// </summary>
  8. public partial class RoomUIPanel : RoomUI
  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. public override void OnOpen(params object[] args)
  35. {
  36. }
  37.  
  38. public override void OnClose()
  39. {
  40. }
  41. public override void _EnterTree()
  42. {
  43. InteractiveTipBar = GetNode<InteractiveTipBar>("ViewNode/InteractiveTipBar");
  44. InteractiveTipBar.Visible = false;
  45.  
  46. ReloadBar = GetNode<ReloadBar>("ViewNode/ReloadBar");
  47. ReloadBar.Visible = false;
  48. }
  49.  
  50. public override void _Ready()
  51. {
  52. //Generator.UiGenerator.GenerateUi(this, "src/game/ui/roomUI/RoomUI.cs");
  53. //将 GlobalNode 节点下的 ui 节点放入全局坐标中
  54. var globalNode = GetNode("GlobalNode");
  55. var root = GameApplication.Instance.GlobalNodeRoot;
  56. var count = globalNode.GetChildCount();
  57. for (int i = count - 1; i >= 0; i--)
  58. {
  59. var node = globalNode.GetChild(i);
  60. globalNode.RemoveChild(node);
  61. root.CallDeferred("add_child", node);
  62. }
  63. globalNode.CallDeferred("queue_free");
  64. //将 ViewNode 节点放到 SubViewport 下
  65. var viewNode = GetNode("ViewNode");
  66. var viewport = GameApplication.Instance.SubViewport;
  67. count = viewNode.GetChildCount();
  68. for (int i = count - 1; i >= 0; i--)
  69. {
  70. var node = viewNode.GetChild(i);
  71. viewNode.RemoveChild(node);
  72. viewport.CallDeferred("add_child", node);
  73. }
  74. viewNode.CallDeferred("queue_free");
  75. }
  76.  
  77. /// <summary>
  78. /// 设置最大血量
  79. /// </summary>
  80. public void SetMaxHp(int maxHp)
  81. {
  82. MaxHp = Mathf.Max(maxHp, 0);
  83. L_Control.L_HealthBar.L_HpSlot.Instance.Size = new Vector2(maxHp + 3, L_Control.L_HealthBar.L_HpSlot.Instance.Size.Y);
  84. if (Hp > maxHp)
  85. {
  86. SetHp(maxHp);
  87. }
  88. }
  89.  
  90. /// <summary>
  91. /// 设置最大护盾值
  92. /// </summary>
  93. public void SetMaxShield(int maxShield)
  94. {
  95. MaxShield = Mathf.Max(maxShield, 0);
  96. L_Control.L_HealthBar.L_ShieldSlot.Instance.Size = new Vector2(maxShield + 2, L_Control.L_HealthBar.L_ShieldSlot.Instance.Size.Y);
  97. if (Shield > MaxShield)
  98. {
  99. SetShield(maxShield);
  100. }
  101. }
  102.  
  103. /// <summary>
  104. /// 设置当前血量
  105. /// </summary>
  106. public void SetHp(int hp)
  107. {
  108. Hp = Mathf.Clamp(hp, 0, MaxHp);
  109. L_Control.L_HealthBar.L_HpSlot.Instance.Size = new Vector2(hp, L_Control.L_HealthBar.L_HpSlot.Instance.Size.Y);
  110. }
  111.  
  112. /// <summary>
  113. /// 设置护盾值
  114. /// </summary>
  115. public void SetShield(int shield)
  116. {
  117. Shield = Mathf.Clamp(shield, 0, MaxShield);
  118. L_Control.L_HealthBar.L_ShieldSlot.Instance.Size = new Vector2(shield, L_Control.L_HealthBar.L_ShieldSlot.Instance.Size.Y);
  119. }
  120.  
  121. /// <summary>
  122. /// 玩家受到伤害
  123. /// </summary>
  124. public void Hit(int num)
  125. {
  126.  
  127. }
  128.  
  129. /// <summary>
  130. /// 设置显示在 ui 上的枪的纹理
  131. /// </summary>
  132. /// <param name="gun">纹理</param>
  133. public void SetGunTexture(Texture2D gun)
  134. {
  135. if (gun != null)
  136. {
  137. L_Control.L_GunBar.L_GunSprite.Instance.Texture = gun;
  138. L_Control.L_GunBar.L_GunSprite.Instance.Visible = true;
  139. L_Control.L_GunBar.L_BulletText.Instance.Visible = true;
  140. }
  141. else
  142. {
  143. L_Control.L_GunBar.L_GunSprite.Instance.Visible = false;
  144. L_Control.L_GunBar.L_BulletText.Instance.Visible = false;
  145. }
  146. }
  147.  
  148. /// <summary>
  149. /// 设置弹药数据
  150. /// </summary>
  151. /// <param name="curr">当前弹夹弹药量</param>
  152. /// <param name="total">剩余弹药总数</param>
  153. public void SetAmmunition(int curr, int total)
  154. {
  155. L_Control.L_GunBar.L_BulletText.Instance.Text = curr + " / " + total;
  156. }
  157. }