Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / ReloadBar.cs
  1. using Godot;
  2.  
  3. /// <summary>
  4. /// 换弹进度组件
  5. /// </summary>
  6. public class ReloadBar : Node2D
  7. {
  8. private Sprite slot;
  9. private Sprite block;
  10.  
  11. private int width;
  12. private float startX;
  13.  
  14. public override void _Ready()
  15. {
  16. slot = GetNode<Sprite>("Slot");
  17. block = GetNode<Sprite>("Slot/Block");
  18. width = slot.Texture.GetWidth();
  19. startX = -(width - 3) / 2f;
  20. }
  21.  
  22. /// <summary>
  23. /// 隐藏换弹进度组件
  24. /// </summary>
  25. public void HideBar()
  26. {
  27. Visible = false;
  28. }
  29.  
  30. /// <summary>
  31. /// 显示换弹进度组件
  32. /// </summary>
  33. /// <param name="position">坐标</param>
  34. /// <param name="progress">进度, 0 - 1</param>
  35. public void ShowBar(Vector2 position, float progress)
  36. {
  37. Visible = true;
  38. GlobalPosition = position;
  39. progress = Mathf.Clamp(progress, 0, 1);
  40. block.Position = new Vector2(startX + (width - 3) * progress, 0);
  41. }
  42. }