ItemWheel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace HQFPSWeapons.UserInterface
  5. {
  6. public class ItemWheel : UserInterfaceBehaviour
  7. {
  8. public enum ItemWheelState { SelectItems, InsertItems}
  9. [BHeader("GENERAL", true)]
  10. [SerializeField]
  11. private Panel m_Panel = null;
  12. [SerializeField]
  13. private RectTransform m_WheelArrow = null;
  14. [Range(0f, 2f)]
  15. private float m_WheelToggleCooldown = 0.25f;
  16. [SerializeField]
  17. private string m_ContainerName = string.Empty;
  18. [SerializeField]
  19. private float m_Sensitivity = 3f;
  20. [SerializeField]
  21. private float m_Range = 3f;
  22. [SerializeField]
  23. private Text m_DescriptionText = null;
  24. [SerializeField]
  25. private Text m_ItemNameText = null;
  26. [BHeader("Slot Positioning...")]
  27. [SerializeField]
  28. private float m_RadialSpacing = 45f;
  29. [SerializeField]
  30. private float m_RadialDistance = 255f;
  31. [SerializeField]
  32. private float m_RadialOffset = 90f;
  33. private Dictionary<UI_WheelSlot, ItemSlot> m_SlotDictionary = new Dictionary<UI_WheelSlot, ItemSlot>();
  34. private UI_WheelSlot[] m_WheelSlots;
  35. private ItemContainer m_HolsterContainer;
  36. private int m_LastHighlightedSlot = -1;
  37. private int m_LastSelectedSlot = -1;
  38. private Vector2 m_CursorPos;
  39. private Vector2 m_DirectionOfSelection;
  40. private float m_NextTimeCanToggleWheel;
  41. private bool m_IsVisible;
  42. private ItemWheelState m_ItemWheelState;
  43. public void SetItemWheelState(int state)
  44. {
  45. if (state == 0)
  46. m_ItemWheelState = ItemWheelState.SelectItems;
  47. else if (state == 1)
  48. {
  49. m_ItemWheelState = ItemWheelState.InsertItems;
  50. foreach (UI_WheelSlot slot in m_WheelSlots)
  51. {
  52. slot.Deselect();
  53. slot.SetSlotHighlights(UI_WheelSlot.SelectionGraphicState.Normal);
  54. }
  55. m_LastHighlightedSlot = -1;
  56. m_LastSelectedSlot = -1;
  57. }
  58. }
  59. public void PositionSlots()
  60. {
  61. var wheelSlots = GetComponentsInChildren<UI_WheelSlot>();
  62. for(int i = 0;i < wheelSlots.Length;i++)
  63. {
  64. float angle = Mathf.Deg2Rad * (m_RadialSpacing * i + m_RadialOffset);
  65. RectTransform rectTransf = wheelSlots[i].GetComponent<RectTransform>();
  66. Vector2 positionOnCircle = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * m_RadialDistance;
  67. rectTransf.anchoredPosition = positionOnCircle;
  68. rectTransf.up = positionOnCircle;
  69. // Icon
  70. RectTransform icon = rectTransf.Find("Icon").GetComponent<RectTransform>();
  71. icon.up = Vector3.up;
  72. }
  73. }
  74. public override void OnAttachment()
  75. {
  76. m_HolsterContainer = Player.Inventory.GetContainerWithName(m_ContainerName);
  77. if(m_HolsterContainer != null)
  78. {
  79. for(int i = 0;i < m_HolsterContainer.Count;i++)
  80. {
  81. m_SlotDictionary.Add(m_WheelSlots[i], m_HolsterContainer[i]);
  82. m_WheelSlots[i].LinkToSlot(m_HolsterContainer[i]);
  83. }
  84. }
  85. Manager.ItemWheel.AddStartTryer(TryStart_ItemWheelInspection);
  86. Manager.ItemWheel.AddStopTryer(TryStop_ItemWheelInspection);
  87. m_HolsterContainer.Changed.AddListener(ChangedHolsterContainer);
  88. }
  89. private void ChangedHolsterContainer(ItemSlot slot)
  90. {
  91. if (slot.Item != null)
  92. {
  93. int indexOfChangedSlot = IndexOfSlot(slot);
  94. HandleSlotHighlighting(indexOfChangedSlot);
  95. HandleSlotSelection(indexOfChangedSlot);
  96. }
  97. }
  98. private int IndexOfSlot(ItemSlot slot)
  99. {
  100. for (int i = 0; i < m_HolsterContainer.Slots.Length; i++)
  101. {
  102. if (m_HolsterContainer[i] == slot)
  103. return i;
  104. }
  105. return -1;
  106. }
  107. private void Update()
  108. {
  109. if (!m_Panel.IsVisible)
  110. {
  111. m_IsVisible = false;
  112. return;
  113. }
  114. if (!m_IsVisible)
  115. {
  116. if (m_LastSelectedSlot != -1)
  117. {
  118. TryShowSlotInfo(m_WheelSlots[m_LastSelectedSlot]);
  119. if (!m_WheelSlots[m_LastSelectedSlot].HasItem)
  120. {
  121. m_WheelSlots[m_LastSelectedSlot].Deselect();
  122. m_WheelSlots[m_LastSelectedSlot].SetSlotHighlights(UI_WheelSlot.SelectionGraphicState.Normal);
  123. m_LastSelectedSlot = -1;
  124. }
  125. }
  126. m_IsVisible = true;
  127. }
  128. if (m_ItemWheelState == ItemWheelState.InsertItems)
  129. return;
  130. int highlightedSlot = GetHighlightedSlot();
  131. if(highlightedSlot != m_LastHighlightedSlot)
  132. HandleSlotHighlighting(highlightedSlot);
  133. }
  134. private bool TryStart_ItemWheelInspection()
  135. {
  136. if(!Player.Aim.Active && Time.time > m_NextTimeCanToggleWheel && !Player.Healing.Active)
  137. {
  138. m_Panel.TryShow(true);
  139. m_NextTimeCanToggleWheel = Time.time + m_WheelToggleCooldown;
  140. Cursor.lockState = CursorLockMode.Locked;
  141. Cursor.visible = false;
  142. return true;
  143. }
  144. return false;
  145. }
  146. private bool TryStop_ItemWheelInspection()
  147. {
  148. if(Time.time > m_NextTimeCanToggleWheel)
  149. {
  150. m_Panel.TryShow(false);
  151. HandleSlotSelection(m_LastHighlightedSlot);
  152. m_NextTimeCanToggleWheel = Time.time + m_WheelToggleCooldown;
  153. Cursor.lockState = CursorLockMode.Locked;
  154. Cursor.visible = false;
  155. return true;
  156. }
  157. return false;
  158. }
  159. private void Awake()
  160. {
  161. m_ItemWheelState = ItemWheelState.SelectItems;
  162. m_WheelSlots = GetComponentsInChildren<UI_WheelSlot>();
  163. }
  164. private int GetHighlightedSlot()
  165. {
  166. Vector2 directionOfSelection = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")).normalized * m_Range;
  167. if(directionOfSelection != Vector2.zero)
  168. m_DirectionOfSelection = Vector2.Lerp(m_DirectionOfSelection, directionOfSelection, Time.deltaTime * m_Sensitivity);
  169. m_CursorPos = m_DirectionOfSelection;
  170. float angle = -Vector2.SignedAngle(Vector2.up, m_CursorPos);
  171. if (angle < 0)
  172. angle = 360f - Mathf.Abs(angle);
  173. m_WheelArrow.rotation = Quaternion.Euler(0f, 0f, -angle);
  174. angle = 360f - angle;
  175. float angleBetweenSlots = 360f / m_WheelSlots.Length;
  176. angle -= angleBetweenSlots / 2;
  177. if (angle > 360f)
  178. angle = angle - 360f;
  179. if (!(angle + angleBetweenSlots / 2 > 360 - angleBetweenSlots / 2))
  180. return Mathf.Clamp(Mathf.RoundToInt((angle + angleBetweenSlots / 2) / angleBetweenSlots), 0, m_WheelSlots.Length - 1);
  181. else
  182. return 0;
  183. }
  184. private void HandleSlotHighlighting(int highlightedSlot)
  185. {
  186. m_WheelSlots[highlightedSlot].SetSlotHighlights(UI_WheelSlot.SelectionGraphicState.Highlighted);
  187. m_WheelSlots[highlightedSlot].Select();
  188. if (m_LastHighlightedSlot != -1)
  189. {
  190. if (m_LastSelectedSlot != m_LastHighlightedSlot)
  191. m_WheelSlots[m_LastHighlightedSlot].Deselect();
  192. m_WheelSlots[m_LastHighlightedSlot].SetSlotHighlights(UI_WheelSlot.SelectionGraphicState.Normal);
  193. }
  194. m_LastHighlightedSlot = highlightedSlot;
  195. TryShowSlotInfo(m_WheelSlots[highlightedSlot]);
  196. }
  197. private void HandleSlotSelection(int highlightedSlot)
  198. {
  199. int currentSelectedSlot = highlightedSlot;
  200. var slot = m_SlotDictionary[m_WheelSlots[highlightedSlot]];
  201. Player.EquipItem.Try(slot.Item, false);
  202. m_HolsterContainer.SelectedSlot = currentSelectedSlot;
  203. //Selection Graphics
  204. if (currentSelectedSlot != m_LastSelectedSlot)
  205. {
  206. if (m_LastSelectedSlot != -1)
  207. {
  208. if (m_WheelSlots[currentSelectedSlot].Item == null)
  209. {
  210. m_WheelSlots[m_LastSelectedSlot].Deselect();
  211. m_WheelSlots[m_LastSelectedSlot].SetSlotHighlights(UI_WheelSlot.SelectionGraphicState.Normal);
  212. m_LastSelectedSlot = -1;
  213. }
  214. else
  215. {
  216. m_WheelSlots[currentSelectedSlot].Select();
  217. m_WheelSlots[currentSelectedSlot].SetSlotHighlights(UI_WheelSlot.SelectionGraphicState.Highlighted);
  218. m_WheelSlots[m_LastSelectedSlot].Deselect();
  219. m_WheelSlots[m_LastSelectedSlot].SetSlotHighlights(UI_WheelSlot.SelectionGraphicState.Normal);
  220. m_LastSelectedSlot = currentSelectedSlot;
  221. }
  222. }
  223. else if(m_WheelSlots[currentSelectedSlot].Item != null)
  224. {
  225. m_WheelSlots[currentSelectedSlot].Select();
  226. m_WheelSlots[currentSelectedSlot].SetSlotHighlights(UI_WheelSlot.SelectionGraphicState.Highlighted);
  227. m_LastSelectedSlot = currentSelectedSlot;
  228. }
  229. }
  230. }
  231. private void TryShowSlotInfo(UI_WheelSlot slot)
  232. {
  233. ItemSlot itemSlot;
  234. if (m_SlotDictionary.TryGetValue(slot, out itemSlot))
  235. {
  236. if (itemSlot != null && itemSlot.HasItem)
  237. {
  238. m_ItemNameText.text = itemSlot.Item.Name;
  239. if(itemSlot.Item.Data.Descriptions.Length > 0)
  240. m_DescriptionText.text = itemSlot.Item.Data.Descriptions[0].Description;
  241. }
  242. else
  243. {
  244. m_ItemNameText.text = "";
  245. m_DescriptionText.text = "";
  246. }
  247. }
  248. }
  249. }
  250. }