Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / weaponRoulette / WeaponRoulettePanel.cs
@小李xl 小李xl on 5 Mar 2024 4 KB 武器轮盘开发中
  1. using System.Collections.Generic;
  2. using Godot;
  3.  
  4. namespace UI.WeaponRoulette;
  5.  
  6. /// <summary>
  7. /// 武器轮盘
  8. /// </summary>
  9. public partial class WeaponRoulettePanel : WeaponRoulette
  10. {
  11. /// <summary>
  12. /// 武器槽数量
  13. /// </summary>
  14. public const int SlotCount = 6;
  15. //是否展开轮盘
  16. private bool _pressRouletteFlag = false;
  17. private bool _isMagnifyRoulette = false;
  18. //所有武器插槽
  19. private List<WeaponSlotNode> _slotNodes = new List<WeaponSlotNode>();
  20.  
  21. public override void OnCreateUi()
  22. {
  23. S_RouletteBg.Instance.Visible = false;
  24. S_Bg.Instance.Visible = false;
  25.  
  26. //创建武器插槽
  27. for (var i = 0; i < SlotCount; i++)
  28. {
  29. var angle = i * (360f / SlotCount);
  30. var clone = S_WeaponSlotNode.CloneAndPut();
  31. var collisionPolygon2D = clone.L_SlotAreaNode.L_CollisionPolygon2D.Instance;
  32. var sectorPolygon = Utils.CreateSectorPolygon(0, 100, 360f / SlotCount, 4);
  33. collisionPolygon2D.Polygon = sectorPolygon;
  34. clone.Instance.RotationDegrees = angle;
  35. clone.L_Control.Instance.RotationDegrees = -angle;
  36. clone.L_Control.L_WeaponIcon.Instance.Material = (Material)S_WeaponSlotNode.L_Control.L_WeaponIcon.Instance.Material.Duplicate();
  37. _slotNodes.Add(clone);
  38. }
  39. S_WeaponSlotNode.QueueFree();
  40.  
  41. SetEnableSectorCollision(false);
  42. }
  43.  
  44. public override void OnDestroyUi()
  45. {
  46. }
  47.  
  48. public override void Process(float delta)
  49. {
  50. if (!InputManager.Roulette)
  51. {
  52. _pressRouletteFlag = false;
  53. }
  54.  
  55. //按下地图按键
  56. if (InputManager.Roulette && !_isMagnifyRoulette) //打开轮盘
  57. {
  58. if (UiManager.GetUiInstanceCount(UiManager.UiNames.PauseMenu) == 0)
  59. {
  60. ExpandRoulette();
  61. }
  62. }
  63. else if (!InputManager.Roulette && _isMagnifyRoulette) //缩小轮盘
  64. {
  65. ShrinkRoulette();
  66. }
  67.  
  68. if (InputManager.Roulette)
  69. {
  70. S_MouseArea.Instance.GlobalPosition = GetGlobalMousePosition();
  71. }
  72. }
  73. private void ExpandRoulette()
  74. {
  75. World.Current.Pause = true;
  76. _pressRouletteFlag = true;
  77. _isMagnifyRoulette = true;
  78. S_RouletteBg.Instance.Visible = true;
  79. S_Bg.Instance.Visible = true;
  80. SetEnableSectorCollision(true);
  81. RefreshWeapon();
  82. }
  83. private void ShrinkRoulette()
  84. {
  85. S_RouletteBg.Instance.Visible = false;
  86. S_Bg.Instance.Visible = false;
  87. _isMagnifyRoulette = false;
  88. World.Current.Pause = false;
  89. SetEnableSectorCollision(false);
  90. }
  91. //设置是否启用扇形碰撞检测
  92. private void SetEnableSectorCollision(bool enable)
  93. {
  94. S_MouseArea.Instance.Monitorable = enable;
  95. foreach (var weaponSlotNode in _slotNodes)
  96. {
  97. weaponSlotNode.L_SlotAreaNode.Instance.Monitorable = enable;
  98. }
  99. }
  100. //更新显示的武器
  101. private void RefreshWeapon()
  102. {
  103. var current = Player.Current;
  104. if (current == null)
  105. {
  106. foreach (var slotNode in _slotNodes)
  107. {
  108. slotNode.L_Control.Instance.Visible = false;
  109. }
  110.  
  111. return;
  112. }
  113.  
  114. var weapons = current.WeaponPack.ItemSlot;
  115. for (var i = 0; i < _slotNodes.Count; i++)
  116. {
  117. var slotNode = _slotNodes[i];
  118. slotNode.L_Control.Instance.Visible = true;
  119. if (weapons.Length > i)
  120. {
  121. var weapon = weapons[i];
  122. if (weapon != null)
  123. {
  124. slotNode.L_Control.Instance.Visible = true;
  125. slotNode.L_Control.L_WeaponIcon.Instance.Texture = weapon.GetDefaultTexture();
  126. slotNode.L_Control.L_AmmoLabel.Instance.Text =
  127. (weapon.CurrAmmo + weapon.ResidueAmmo).ToString() + "/" + weapon.Attribute.MaxAmmoCapacity;
  128. }
  129. else
  130. {
  131. slotNode.L_Control.Instance.Visible = false;
  132. }
  133. }
  134. else
  135. {
  136. slotNode.L_Control.Instance.Visible = false;
  137. }
  138. }
  139. }
  140. }