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. S_Root.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. //扔掉武器
  83. if (ActiveWeapon != null && InputManager.MeleeAttack)
  84. {
  85. ActiveWeapon.Master.ThrowWeapon(ActiveWeapon.PackageIndex);
  86. ActiveWeapon = null;
  87. RefreshWeapon();
  88. }
  89. else
  90. {
  91. //上一页/下一页
  92. if (_maxPageIndex > 0)
  93. {
  94. if (InputManager.ExchangeWeapon) //上一页
  95. {
  96. _pageIndex--;
  97. if (_pageIndex < 0)
  98. {
  99. _pageIndex = _maxPageIndex;
  100. }
  101. RefreshPageLabel();
  102. RefreshWeapon();
  103. }
  104. else if (InputManager.Interactive) //下一页
  105. {
  106. _pageIndex++;
  107. if (_pageIndex > _maxPageIndex)
  108. {
  109. _pageIndex = 0;
  110. }
  111. RefreshPageLabel();
  112. RefreshWeapon();
  113. }
  114. }
  115. }
  116.  
  117. //扔掉武器提示
  118. S_DownBar.Instance.Visible = ActiveWeapon != null;
  119. }
  120. else
  121. {
  122. ActiveWeapon = null;
  123. }
  124. }
  125. //打开轮盘
  126. private void ExpandRoulette()
  127. {
  128. World.Current.Pause = true;
  129. _pressRouletteFlag = true;
  130. _isMagnifyRoulette = true;
  131. S_Root.Instance.Visible = true;
  132. S_Bg.Instance.Visible = true;
  133. SetEnableSectorCollision(true);
  134. RefreshSlotPage();
  135. RefreshPageLabel();
  136. RefreshWeapon();
  137. }
  138. //关闭轮盘
  139. private void ShrinkRoulette()
  140. {
  141. S_Root.Instance.Visible = false;
  142. S_Bg.Instance.Visible = false;
  143. _isMagnifyRoulette = false;
  144. World.Current.Pause = false;
  145. SetEnableSectorCollision(false);
  146.  
  147. //如果选中了物体
  148. if (ActiveWeapon != null)
  149. {
  150. Player.Current.ExchangeWeaponByIndex(ActiveWeapon.PackageIndex);
  151. }
  152. }
  153. //设置是否启用扇形碰撞检测
  154. private void SetEnableSectorCollision(bool enable)
  155. {
  156. S_MouseArea.Instance.Monitorable = enable;
  157. foreach (var weaponSlotNode in _slotNodes)
  158. {
  159. weaponSlotNode.L_SlotAreaNode.Instance.Monitorable = enable;
  160. }
  161. }
  162.  
  163. //刷新页码文本
  164. private void RefreshPageLabel()
  165. {
  166. S_PageLabel.Instance.Text = $"{_pageIndex + 1}/{_maxPageIndex + 1}";
  167. }
  168. //刷新页码
  169. private void RefreshSlotPage()
  170. {
  171. var current = Player.Current;
  172. if (current == null)
  173. {
  174. return;
  175. }
  176.  
  177. var weapons = current.WeaponPack.ItemSlot;
  178. //判断是否显示上一页下一页提示
  179. var lastIndex = 0;
  180. for (var i = weapons.Length - 1; i >= 0; i--)
  181. {
  182. if (weapons[i] != null)
  183. {
  184. lastIndex = i;
  185. break;
  186. }
  187. }
  188. _maxPageIndex = Mathf.CeilToInt((lastIndex + 1f) / SlotCount) - 1;
  189. S_UpBar.Instance.Visible = _maxPageIndex > 0;
  190. if (_pageIndex > _maxPageIndex)
  191. {
  192. _pageIndex = _maxPageIndex;
  193. }
  194. }
  195. //更新显示的武器
  196. private void RefreshWeapon()
  197. {
  198. var current = Player.Current;
  199. if (current == null) //没有玩家对象,这是异常情况
  200. {
  201. foreach (var slotNode in _slotNodes)
  202. {
  203. slotNode.L_SlotUi.Instance.Visible = false;
  204. }
  205.  
  206. return;
  207. }
  208.  
  209. var weapons = current.WeaponPack.ItemSlot;
  210. for (var i = 0; i < _slotNodes.Count; i++)
  211. {
  212. var slotNode = _slotNodes[i];
  213. slotNode.L_SlotUi.Instance.Visible = true;
  214. slotNode.L_SlotUi.L_LockSprite.Instance.Visible = false;
  215. var weaponIndex = i + _pageIndex * SlotCount;
  216. if (weapons.Length > weaponIndex)
  217. {
  218. var weapon = weapons[weaponIndex];
  219. if (weapon != null) //有武器
  220. {
  221. slotNode.L_SlotUi.L_WeaponUi.Instance.Visible = true;
  222. slotNode.L_SlotUi.L_WeaponUi.L_WeaponIcon.Instance.Texture = weapon.GetDefaultTexture();
  223. slotNode.L_SlotUi.L_WeaponUi.L_AmmoLabel.Instance.Text =
  224. (weapon.CurrAmmo + weapon.ResidueAmmo).ToString() + "/" + weapon.Attribute.MaxAmmoCapacity;
  225. slotNode.Instance.SetWeapon(weapon);
  226. slotNode.L_SlotAreaNode.Instance.Monitoring = true;
  227. }
  228. else //已经解锁,但是没有武器
  229. {
  230. slotNode.L_SlotUi.L_WeaponUi.Instance.Visible = false;
  231. slotNode.L_SlotAreaNode.Instance.Monitoring = false;
  232. slotNode.Instance.ClearWeapon();
  233. }
  234. }
  235. else //未解锁
  236. {
  237. slotNode.L_SlotUi.L_LockSprite.Instance.Visible = true;
  238. slotNode.L_SlotUi.L_WeaponUi.Instance.Visible = false;
  239. slotNode.L_SlotAreaNode.Instance.Monitoring = false;
  240. slotNode.Instance.ClearWeapon();
  241. }
  242. }
  243. }
  244. }