Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / roomUI / ActivePropBar.cs
@lijincheng lijincheng on 11 Jul 2023 5 KB 调整生成Ui的代码
  1. using Godot;
  2.  
  3. namespace UI.RoomUI;
  4.  
  5. public class ActivePropBar
  6. {
  7. private RoomUI.RoomUI_ActivePropBar _activePropBar;
  8. private ShaderMaterial _shaderMaterial;
  9. private Vector2 _startCooldownPos;
  10. private Vector2 _startCooldownSize;
  11. private Vector2 _startChargePos;
  12. private Rect2 _startChargeRect;
  13.  
  14. private bool _initCooldown = false;
  15.  
  16. public ActivePropBar(RoomUI.RoomUI_ActivePropBar activePropBar)
  17. {
  18. _activePropBar = activePropBar;
  19. _shaderMaterial = (ShaderMaterial)_activePropBar.L_ActivePropSprite.Instance.Material;
  20. _startCooldownPos = _activePropBar.L_CooldownProgress.Instance.Position;
  21. _startCooldownSize = _activePropBar.L_CooldownProgress.Instance.Scale;
  22. _startChargePos = _activePropBar.L_ChargeProgress.Instance.Position;
  23. _startChargeRect = _activePropBar.L_ChargeProgress.Instance.RegionRect;
  24.  
  25. SetActivePropTexture(null);
  26. SetChargeProgress(1);
  27. }
  28. public void OnShow()
  29. {
  30. }
  31.  
  32. public void OnHide()
  33. {
  34. }
  35.  
  36. public void Process(float delta)
  37. {
  38. var prop = Player.Current?.ActivePropsPack.ActiveItem;
  39. if (prop != null)
  40. {
  41. SetActivePropCount(prop.Count);
  42. SetActivePropTexture(prop.GetCurrentTexture());
  43.  
  44. //是否可以使用该道具
  45. if (prop.CheckUse())
  46. {
  47. _shaderMaterial.SetShaderParameter("schedule", 0);
  48. }
  49. else
  50. {
  51. _shaderMaterial.SetShaderParameter("schedule", 0.6f);
  52. }
  53. //冷却
  54. SetCooldownProgress(prop.GetCooldownProgress());
  55. //充能进度
  56. SetChargeProgress(prop.ChargeProgress);
  57. }
  58. else
  59. {
  60. SetActivePropTexture(null);
  61. }
  62. }
  63. /// <summary>
  64. /// 设置道具图标
  65. /// </summary>
  66. public void SetActivePropTexture(Texture2D texture)
  67. {
  68. if (texture != null)
  69. {
  70. _activePropBar.L_ActivePropSprite.Instance.Texture = texture;
  71. _activePropBar.Instance.Visible = true;
  72. }
  73. else
  74. {
  75. _activePropBar.Instance.Visible = false;
  76. }
  77. }
  78.  
  79. /// <summary>
  80. /// 设置道具数量
  81. /// </summary>
  82. public void SetActivePropCount(int count)
  83. {
  84. if (count > 1)
  85. {
  86. _activePropBar.L_ActivePropCount.Instance.Visible = true;
  87. _activePropBar.L_ActivePropCount.Instance.Text = count.ToString();
  88. }
  89. else
  90. {
  91. _activePropBar.L_ActivePropCount.Instance.Visible = false;
  92. }
  93. }
  94.  
  95. /// <summary>
  96. /// 设置道具冷却进度
  97. /// </summary>
  98. /// <param name="progress">进度: 0 - 1</param>
  99. public void SetCooldownProgress(float progress)
  100. {
  101. progress = 1 - progress;
  102. var colorRect = _activePropBar.L_CooldownProgress.Instance;
  103. if (progress <= 0)
  104. {
  105. colorRect.Visible = false;
  106. }
  107. else
  108. {
  109. colorRect.Visible = true;
  110.  
  111. //调整蒙板高度
  112. var rect = colorRect.RegionRect;
  113. var size = rect.Size;
  114. size.Y = progress;
  115. rect.Size = size;
  116. colorRect.RegionRect = rect;
  117.  
  118. //调整蒙板位置
  119. var height = _startCooldownSize.Y * progress;
  120. var position = colorRect.Position;
  121. position.Y = _startCooldownPos.Y + (_startCooldownSize.Y - height);
  122. colorRect.Position = position;
  123. }
  124. }
  125.  
  126. /// <summary>
  127. /// 设置充能进度条是否显示
  128. /// </summary>
  129. public void SetChargeProgressVisible(bool visible)
  130. {
  131. var ninePatchRect = _activePropBar.L_ChargeProgressBar.Instance;
  132. _activePropBar.L_ChargeProgress.Instance.Visible = visible;
  133. if (ninePatchRect.Visible == visible && _initCooldown)
  134. {
  135. return;
  136. }
  137.  
  138. _initCooldown = true;
  139.  
  140. var sprite = _activePropBar.L_CooldownProgress.Instance;
  141. ninePatchRect.Visible = visible;
  142. //调整冷却蒙板大小
  143. if (visible)
  144. {
  145. var rect = ninePatchRect.GetRect();
  146. var position = sprite.Position;
  147. position.X = _startCooldownPos.X + rect.Size.X - 1;
  148. sprite.Position = position;
  149.  
  150. var scale = sprite.Scale;
  151. scale.X = _startCooldownSize.X - rect.Size.X + 1;
  152. sprite.Scale = scale;
  153. }
  154. else
  155. {
  156. sprite.Position = _startCooldownPos;
  157. sprite.Scale = _startCooldownSize;
  158. }
  159. }
  160.  
  161. /// <summary>
  162. /// 设置充能进度
  163. /// </summary>
  164. /// <param name="progress">进度: 0 - 1</param>
  165. public void SetChargeProgress(float progress)
  166. {
  167. if (progress >= 1)
  168. {
  169. SetChargeProgressVisible(false);
  170. }
  171. else
  172. {
  173. SetChargeProgressVisible(true);
  174.  
  175. var height = _startChargeRect.Size.Y * progress;
  176. var rectY = _startChargeRect.Size.Y * (1 - progress);
  177. var posY = _startChargePos.Y + rectY;
  178.  
  179. var sprite = _activePropBar.L_ChargeProgress.Instance;
  180. var position = sprite.Position;
  181. position.Y = posY;
  182. sprite.Position = position;
  183.  
  184. var rect = sprite.RegionRect;
  185. var rectPosition = rect.Position;
  186. rectPosition.Y = rectY;
  187. rect.Position = rectPosition;
  188.  
  189. var rectSize = rect.Size;
  190. rectSize.Y = height;
  191. rect.Size = rectSize;
  192.  
  193. sprite.RegionRect = rect;
  194. }
  195. }
  196. }