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.UiNode15_GunBar _gunBar;
  8. private EventBinder _binder;
  9.  
  10. private int _prevAmmo = -1;
  11. private int _prevResidue = -1;
  12. public GunBar(RoomUI.UiNode15_GunBar gunBar)
  13. {
  14. _gunBar = gunBar;
  15. }
  16.  
  17. public void OnOpen()
  18. {
  19. _binder = EventManager.AddEventListener(EventEnum.OnPlayerRefreshWeaponTexture, OnPlayerRefreshWeaponTexture);
  20. }
  21.  
  22. public void OnClose()
  23. {
  24. _binder.RemoveEventListener();
  25. }
  26.  
  27. public void Process(float delta)
  28. {
  29. var weapon = Player.Current.Holster.ActiveWeapon;
  30. if (weapon != null)
  31. {
  32. SetWeaponAmmunition(weapon.CurrAmmo, weapon.ResidueAmmo);
  33. }
  34. }
  35.  
  36. /// <summary>
  37. /// 设置显示在 ui 上武器的纹理
  38. /// </summary>
  39. /// <param name="gun">纹理</param>
  40. public void SetWeaponTexture(Texture2D gun)
  41. {
  42. if (gun != null)
  43. {
  44. _gunBar.L_GunSprite.Instance.Texture = gun;
  45. _gunBar.L_GunSprite.Instance.Visible = true;
  46. _gunBar.L_BulletText.Instance.Visible = true;
  47. }
  48. else
  49. {
  50. _gunBar.L_GunSprite.Instance.Visible = false;
  51. _gunBar.L_BulletText.Instance.Visible = false;
  52. }
  53. }
  54. /// <summary>
  55. /// 设置弹药数据
  56. /// </summary>
  57. /// <param name="curr">当前弹夹弹药量</param>
  58. /// <param name="total">剩余弹药总数</param>
  59. public void SetWeaponAmmunition(int curr, int total)
  60. {
  61. if (curr != _prevAmmo || total != _prevResidue)
  62. {
  63. _gunBar.L_BulletText.Instance.Text = curr + " / " + total;
  64. _prevAmmo = curr;
  65. _prevResidue = total;
  66. }
  67. }
  68.  
  69. private void OnPlayerRefreshWeaponTexture(object o)
  70. {
  71. if (o == null)
  72. {
  73. SetWeaponTexture(null);
  74. }
  75. else
  76. {
  77. SetWeaponTexture((Texture2D)o);
  78. }
  79. }
  80. }