Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / roomUI / GunBar.cs
  1. using Godot;
  2.  
  3. namespace UI.RoomUI;
  4.  
  5. public class GunBar
  6. {
  7. private RoomUI.UiNode_GunBar _gunBar;
  8.  
  9. private int _prevAmmo = -1;
  10. private int _prevResidue = -1;
  11. public GunBar(RoomUI.UiNode_GunBar gunBar)
  12. {
  13. _gunBar = gunBar;
  14. }
  15.  
  16. public void OnShow()
  17. {
  18. }
  19.  
  20. public void OnHide()
  21. {
  22. }
  23.  
  24. public void Process(float delta)
  25. {
  26. var weapon = Player.Current?.Holster.ActiveWeapon;
  27. if (weapon != null)
  28. {
  29. SetWeaponTexture(weapon.GetCurrentTexture());
  30. SetWeaponAmmunition(weapon.CurrAmmo, weapon.ResidueAmmo);
  31. }
  32. else
  33. {
  34. SetWeaponTexture(null);
  35. }
  36. }
  37.  
  38. /// <summary>
  39. /// 设置显示在 ui 上武器的纹理
  40. /// </summary>
  41. /// <param name="gun">纹理</param>
  42. public void SetWeaponTexture(Texture2D gun)
  43. {
  44. if (gun != null)
  45. {
  46. _gunBar.L_GunSprite.Instance.Texture = gun;
  47. _gunBar.L_GunSprite.Instance.Visible = true;
  48. _gunBar.L_BulletText.Instance.Visible = true;
  49. }
  50. else
  51. {
  52. _gunBar.L_GunSprite.Instance.Visible = false;
  53. _gunBar.L_BulletText.Instance.Visible = false;
  54. }
  55. }
  56. /// <summary>
  57. /// 设置弹药数据
  58. /// </summary>
  59. /// <param name="curr">当前弹夹弹药量</param>
  60. /// <param name="total">剩余弹药总数</param>
  61. public void SetWeaponAmmunition(int curr, int total)
  62. {
  63. if (curr != _prevAmmo || total != _prevResidue)
  64. {
  65. _gunBar.L_BulletText.Instance.Text = curr + " / " + total;
  66. _prevAmmo = curr;
  67. _prevResidue = total;
  68. }
  69. }
  70. }