LuckyBoxStarView.cs 6.6 KB

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