Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / weaponRoulette / WeaponRoulettePanel.cs
  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. /// <summary>
  16. /// 选中的武器
  17. /// </summary>
  18. public Weapon ActiveWeapon;
  19. //是否展开轮盘
  20. private bool _pressRouletteFlag = false;
  21. private bool _isMagnifyRoulette = false;
  22. //所有武器插槽
  23. private List<WeaponSlotNode> _slotNodes = new List<WeaponSlotNode>();
  24.  
  25. //当前页索引
  26. private int _pageIndex = 0;
  27. //最大页数
  28. private int _maxPageIndex = 0;
  29. public override void OnCreateUi()
  30. {
  31. L_Control.Instance.Visible = false;
  32. S_Bg.Instance.Visible = false;
  33.  
  34. //创建武器插槽
  35. for (var i = 0; i < SlotCount; i++)
  36. {
  37. var angle = i * (360f / SlotCount);
  38. var clone = S_WeaponSlotNode.CloneAndPut();
  39. var collisionPolygon2D = clone.L_SlotAreaNode.L_CollisionPolygon2D.Instance;
  40. var sectorPolygon = Utils.CreateSectorPolygon(0, 100, 360f / SlotCount, 4);
  41. collisionPolygon2D.Polygon = sectorPolygon;
  42. clone.Instance.RotationDegrees = angle;
  43. clone.L_SlotUi.Instance.RotationDegrees = -angle;
  44. clone.L_SlotUi.L_WeaponUi.L_WeaponIcon.Instance.Material =
  45. (Material)S_WeaponSlotNode.L_SlotUi.L_WeaponUi.L_WeaponIcon.Instance.Material.Duplicate();
  46. _slotNodes.Add(clone);
  47. }
  48. S_WeaponSlotNode.QueueFree();
  49.  
  50. SetEnableSectorCollision(false);
  51. }
  52.  
  53. public override void OnDestroyUi()
  54. {
  55. }
  56.  
  57. public override void Process(float delta)
  58. {
  59. if (!InputManager.Roulette)
  60. {
  61. _pressRouletteFlag = false;
  62. }
  63.  
  64. //按下地图按键
  65. if (InputManager.Roulette && !_isMagnifyRoulette) //打开轮盘
  66. {
  67. if (UiManager.GetUiInstanceCount(UiManager.UiNames.PauseMenu) == 0 && !InputManager.Map)
  68. {
  69. ExpandRoulette();
  70. }
  71. }
  72. else if (!InputManager.Roulette && _isMagnifyRoulette) //关闭轮盘
  73. {
  74. ShrinkRoulette();
  75. }
  76.  
  77. //已经打开地图
  78. if (InputManager.Roulette)
  79. {
  80. S_MouseArea.Instance.GlobalPosition = GetGlobalMousePosition();
  81.  
  82. if (_maxPageIndex > 0)
  83. {
  84. if (InputManager.ExchangeWeapon) //上一页
  85. {
  86. _pageIndex--;
  87. if (_pageIndex < 0)
  88. {
  89. _pageIndex = _maxPageIndex;
  90. }
  91. RefreshPageLabel();
  92. RefreshWeapon();
  93. }
  94. else if (InputManager.Interactive) //下一页
  95. {
  96. _pageIndex++;
  97. if (_pageIndex > _maxPageIndex)
  98. {
  99. _pageIndex = 0;
  100. }
  101. RefreshPageLabel();
  102. RefreshWeapon();
  103. }
  104. }
  105. }
  106. else
  107. {
  108. ActiveWeapon = null;
  109. }
  110. }
  111. //打开轮盘
  112. private void ExpandRoulette()
  113. {
  114. World.Current.Pause = true;
  115. _pressRouletteFlag = true;
  116. _isMagnifyRoulette = true;
  117. L_Control.Instance.Visible = true;
  118. S_Bg.Instance.Visible = true;
  119. SetEnableSectorCollision(true);
  120. RefreshSlotPage();
  121. RefreshPageLabel();
  122. RefreshWeapon();
  123. }
  124. //关闭轮盘
  125. private void ShrinkRoulette()
  126. {
  127. L_Control.Instance.Visible = false;
  128. S_Bg.Instance.Visible = false;
  129. _isMagnifyRoulette = false;
  130. World.Current.Pause = false;
  131. SetEnableSectorCollision(false);
  132.  
  133. //如果选中了物体
  134. if (ActiveWeapon != null)
  135. {
  136. Player.Current.ExchangeWeaponByIndex(ActiveWeapon.PackageIndex);
  137. }
  138. }
  139. //设置是否启用扇形碰撞检测
  140. private void SetEnableSectorCollision(bool enable)
  141. {
  142. S_MouseArea.Instance.Monitorable = enable;
  143. foreach (var weaponSlotNode in _slotNodes)
  144. {
  145. weaponSlotNode.L_SlotAreaNode.Instance.Monitorable = enable;
  146. }
  147. }
  148.  
  149. //刷新页码文本
  150. private void RefreshPageLabel()
  151. {
  152. S_PageLabel.Instance.Text = $"{_pageIndex + 1}/{_maxPageIndex + 1}";
  153. }
  154. //刷新页码
  155. private void RefreshSlotPage()
  156. {
  157. var current = Player.Current;
  158. if (current == null)
  159. {
  160. return;
  161. }
  162.  
  163. var weapons = current.WeaponPack.ItemSlot;
  164. //判断是否显示上一页下一页提示
  165. var lastIndex = 0;
  166. for (var i = weapons.Length - 1; i >= 0; i--)
  167. {
  168. if (weapons[i] != null)
  169. {
  170. lastIndex = i;
  171. break;
  172. }
  173. }
  174. _maxPageIndex = Mathf.CeilToInt((lastIndex + 1f) / SlotCount) - 1;
  175. S_ColorRect.Instance.Visible = _maxPageIndex > 0;
  176. if (_pageIndex > _maxPageIndex)
  177. {
  178. _pageIndex = _maxPageIndex;
  179. }
  180. }
  181. //更新显示的武器
  182. private void RefreshWeapon()
  183. {
  184. var current = Player.Current;
  185. if (current == null) //没有玩家对象,这是异常情况
  186. {
  187. foreach (var slotNode in _slotNodes)
  188. {
  189. slotNode.L_SlotUi.Instance.Visible = false;
  190. }
  191.  
  192. return;
  193. }
  194.  
  195. var weapons = current.WeaponPack.ItemSlot;
  196. for (var i = 0; i < _slotNodes.Count; i++)
  197. {
  198. var slotNode = _slotNodes[i];
  199. slotNode.L_SlotUi.Instance.Visible = true;
  200. slotNode.L_SlotUi.L_LockSprite.Instance.Visible = false;
  201. var weaponIndex = i + _pageIndex * SlotCount;
  202. if (weapons.Length > weaponIndex)
  203. {
  204. var weapon = weapons[weaponIndex];
  205. if (weapon != null) //有武器
  206. {
  207. slotNode.L_SlotUi.L_WeaponUi.Instance.Visible = true;
  208. slotNode.L_SlotUi.L_WeaponUi.L_WeaponIcon.Instance.Texture = weapon.GetDefaultTexture();
  209. slotNode.L_SlotUi.L_WeaponUi.L_AmmoLabel.Instance.Text =
  210. (weapon.CurrAmmo + weapon.ResidueAmmo).ToString() + "/" + weapon.Attribute.MaxAmmoCapacity;
  211. slotNode.Instance.SetWeapon(weapon);
  212. slotNode.L_SlotAreaNode.Instance.Monitoring = true;
  213. }
  214. else //已经解锁,但是没有武器
  215. {
  216. slotNode.L_SlotUi.L_WeaponUi.Instance.Visible = false;
  217. slotNode.L_SlotAreaNode.Instance.Monitoring = false;
  218. slotNode.Instance.ClearWeapon();
  219. }
  220. }
  221. else //未解锁
  222. {
  223. slotNode.L_SlotUi.L_LockSprite.Instance.Visible = true;
  224. slotNode.L_SlotUi.L_WeaponUi.Instance.Visible = false;
  225. slotNode.L_SlotAreaNode.Instance.Monitoring = false;
  226. slotNode.Instance.ClearWeapon();
  227. }
  228. }
  229. }
  230. }