LuckyBoxStarView.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System.Collections;
  2. using UnityEngine;
  3. using UI.LuckyBox;
  4. using System.Collections.Generic;
  5. using FairyGUI;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace GFGGame
  9. {
  10. public class LuckyBoxStarView : BaseWindow
  11. {
  12. private UI_LuckyBoxStarUI _ui;
  13. private List<GObject> comStars = new List<GObject>();
  14. private GObject curComStar;//当前选中的星星
  15. private Vector2 lastPos;//鼠标的上一个位置,每颗星星初始时默认为Vector2.right;
  16. private List<ItemData> _rewardList;
  17. private const int checkDistance = 40;//鼠标靠近星星周围加减40都算选中星星
  18. private const int imgLineWidth = 10;//线的原始长度
  19. private bool isGuide = false;
  20. public override void Dispose()
  21. {
  22. base.Dispose();
  23. }
  24. protected override void OnInit()
  25. {
  26. base.OnInit();
  27. _ui = UI_LuckyBoxStarUI.Create();
  28. this.viewCom = _ui.target;
  29. isfullScreen = true;
  30. _ui.m_btnBack.onClick.Add(OnClickBtnBack);
  31. }
  32. protected override void OnShown()
  33. {
  34. base.OnShown();
  35. _ui.target.onTouchBegin.Add(OnClickUIBegin);
  36. _ui.target.onTouchMove.Add(OnClickUIMove);
  37. _ui.target.onTouchEnd.Add(OnClickUIEnd);
  38. _rewardList = LuckyBoxDataManager.Instance.RewardList;
  39. _ui.m_ctrlBuyType.selectedIndex = _rewardList != null && _rewardList.Count > 1 ? 1 : 0;
  40. _ui.m_ctrlRewardsType.selectedIndex = (int)viewData;
  41. ResetStartView();
  42. }
  43. private void ResetStartView()
  44. {
  45. isGuide = GuideController.TryGuideLuckyBoxStar();
  46. curComStar = null;
  47. comStars.Clear();
  48. int index = 0;
  49. GObject star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  50. while (star != null && star.visible == true)
  51. {
  52. UI_ComStar comStar = UI_ComStar.Proxy(star);
  53. comStar.m_c1.selectedIndex = 0;
  54. comStar.m_imgLine.width = imgLineWidth;
  55. comStar.m_imgLine.rotation = 0;
  56. star.data = new Vector2(comStar.target.x, comStar.target.y);
  57. comStars.Add(star);
  58. index++;
  59. star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  60. }
  61. }
  62. private void OnClickUIBegin(EventContext context)
  63. {
  64. context.CaptureTouch();
  65. InputEvent inputEvent = (InputEvent)context.data;
  66. Vector2 mousePos = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
  67. CheckNearbyPos(mousePos);
  68. }
  69. private void OnClickUIMove(EventContext context)
  70. {
  71. InputEvent inputEvent = (InputEvent)context.data;
  72. Vector2 mousePos = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
  73. CheckNearbyPos(mousePos);
  74. if (comStars.Count == 0)//所有星星都点亮了主动结束
  75. {
  76. this.OnClickUIEnd();
  77. }
  78. }
  79. //检测鼠标附近的星星
  80. private void CheckNearbyPos(Vector2 mousePos)
  81. {
  82. for (int i = comStars.Count - 1; i >= 0; i--)
  83. {
  84. Vector2 comStarPos = (Vector2)comStars[i].data;
  85. if (IsSamePos(mousePos, comStarPos))
  86. {
  87. if (curComStar != null)
  88. {
  89. SetCurComStarTransfrom(comStarPos);
  90. }
  91. UI_ComStar comStar = UI_ComStar.Proxy(comStars[i]);
  92. comStar.m_c1.selectedIndex = 1;
  93. curComStar = comStars[i];
  94. lastPos = Vector2.right;
  95. comStars.RemoveAt(i);
  96. }
  97. else
  98. {
  99. if (curComStar != null)
  100. {
  101. Vector2 curPos = mousePos - (Vector2)curComStar.data;
  102. SetCurComStarTransfrom(mousePos);
  103. lastPos = curPos;
  104. }
  105. }
  106. }
  107. }
  108. private void SetCurComStarTransfrom(Vector2 targetPos)
  109. {
  110. Vector2 curPos = targetPos - (Vector2)curComStar.data;
  111. float angle = Vector3.Angle(lastPos, curPos); //求出两向量之间的夹角
  112. Vector3 normal = Vector3.Cross(lastPos, curPos);//叉乘求出法线向量
  113. angle *= Mathf.Sign(Vector3.Dot(normal, Vector3.forward)); //Mathf.Sign()求符号,Vector3.Dot()求方向,求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向
  114. UI_ComStar comStar = UI_ComStar.Proxy(curComStar);
  115. comStar.m_imgLine.rotation += angle;
  116. comStar.m_imgLine.width = Vector2.Distance(targetPos, (Vector2)curComStar.data);
  117. }
  118. private void OnClickUIEnd()
  119. {
  120. if (isGuide && comStars.Count > 0)
  121. {
  122. ResetStartView();
  123. }
  124. else
  125. {
  126. Timers.inst.Add(0.3f, 1, ClickUIEnd);
  127. GuideController.HideGuide();
  128. GuideController.TryCompleteGuide(ConstGuideId.MAIN_UI_BTN_ZHAI_XING);
  129. }
  130. }
  131. private void ClickUIEnd(object param)
  132. {
  133. if (curComStar != null)
  134. {
  135. RemoveListener();
  136. ViewManager.Show(ViewName.LUCKY_BOX_CARD_VIEW, new object[] { _rewardList, FirstGetCardViewType.JUMP });
  137. this.Hide();
  138. }
  139. }
  140. private bool IsSamePos(Vector2 mousePos, Vector2 comStarPos)
  141. {
  142. return (mousePos.x < comStarPos.x + checkDistance) && (mousePos.x > comStarPos.x - checkDistance) && (mousePos.y < comStarPos.y + checkDistance) && (mousePos.y > comStarPos.y - checkDistance);
  143. }
  144. private void RemoveListener()
  145. {
  146. _ui.target.onTouchBegin.Remove(OnClickUIBegin);
  147. _ui.target.onTouchMove.Remove(OnClickUIMove);
  148. _ui.target.onTouchEnd.Remove(OnClickUIEnd);
  149. }
  150. private void OnClickBtnBack()
  151. {
  152. this.Hide();
  153. RemoveListener();
  154. ViewManager.Show(ViewName.LUCKY_BOX_VIEW);
  155. }
  156. protected override void OnHide()
  157. {
  158. base.OnHide();
  159. }
  160. }
  161. }