LuckyBoxStarView.cs 6.6 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. public override void Dispose()
  20. {
  21. base.Dispose();
  22. }
  23. protected override void OnInit()
  24. {
  25. base.OnInit();
  26. _ui = UI_LuckyBoxStarUI.Create();
  27. this.viewCom = _ui.target;
  28. isfullScreen = true;
  29. _ui.m_btnBack.onClick.Add(OnClickBtnBack);
  30. }
  31. protected override void OnShown()
  32. {
  33. base.OnShown();
  34. _ui.target.onTouchBegin.Add(OnClickUIBegin);
  35. _ui.target.onTouchMove.Add(OnClickUIMove);
  36. _ui.target.onTouchEnd.Add(OnClickUIEnd);
  37. _rewardList = LuckyBoxDataManager.Instance.RewardList;
  38. _ui.m_ctrlBuyType.selectedIndex = _rewardList != null && _rewardList.Count > 1 ? 1 : 0;
  39. _ui.m_ctrlRewardsType.selectedIndex = (int)viewData;
  40. ResetStartView();
  41. }
  42. private void ResetStartView()
  43. {
  44. curComStar = null;
  45. comStars.Clear();
  46. int index = 0;
  47. GObject star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  48. while (star != null && star.visible == true)
  49. {
  50. UI_ComStar comStar = UI_ComStar.Proxy(star);
  51. comStar.m_c1.selectedIndex = 0;
  52. comStar.m_imgLine.width = imgLineWidth;
  53. comStar.m_imgLine.rotation = 0;
  54. star.data = new Vector2(comStar.target.x, comStar.target.y);
  55. comStars.Add(star);
  56. index++;
  57. star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  58. }
  59. }
  60. private void OnClickUIBegin(EventContext context)
  61. {
  62. context.CaptureTouch();
  63. InputEvent inputEvent = (InputEvent)context.data;
  64. Vector2 mousePos = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
  65. CheckNearbyPos(mousePos);
  66. }
  67. private void OnClickUIMove(EventContext context)
  68. {
  69. InputEvent inputEvent = (InputEvent)context.data;
  70. Vector2 mousePos = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
  71. CheckNearbyPos(mousePos);
  72. if (comStars.Count == 0)//所有星星都点亮了主动结束
  73. {
  74. this.OnClickUIEnd();
  75. }
  76. }
  77. //检测鼠标附近的星星
  78. private void CheckNearbyPos(Vector2 mousePos)
  79. {
  80. for (int i = comStars.Count - 1; i >= 0; i--)
  81. {
  82. Vector2 comStarPos = (Vector2)comStars[i].data;
  83. if (IsSamePos(mousePos, comStarPos))
  84. {
  85. if (curComStar != null)
  86. {
  87. SetCurComStarTransfrom(comStarPos);
  88. }
  89. UI_ComStar comStar = UI_ComStar.Proxy(comStars[i]);
  90. comStar.m_c1.selectedIndex = 1;
  91. curComStar = comStars[i];
  92. lastPos = Vector2.right;
  93. comStars.RemoveAt(i);
  94. }
  95. else
  96. {
  97. if (curComStar != null)
  98. {
  99. Vector2 curPos = mousePos - (Vector2)curComStar.data;
  100. SetCurComStarTransfrom(mousePos);
  101. lastPos = curPos;
  102. }
  103. }
  104. }
  105. }
  106. private void SetCurComStarTransfrom(Vector2 targetPos)
  107. {
  108. Vector2 curPos = targetPos - (Vector2)curComStar.data;
  109. float angle = Vector3.Angle(lastPos, curPos); //求出两向量之间的夹角
  110. Vector3 normal = Vector3.Cross(lastPos, curPos);//叉乘求出法线向量
  111. angle *= Mathf.Sign(Vector3.Dot(normal, Vector3.forward)); //Mathf.Sign()求符号,Vector3.Dot()求方向,求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向
  112. UI_ComStar comStar = UI_ComStar.Proxy(curComStar);
  113. comStar.m_imgLine.rotation += angle;
  114. comStar.m_imgLine.width = Vector2.Distance(targetPos, (Vector2)curComStar.data);
  115. }
  116. //private async void OnClickUIEnd()
  117. //{
  118. // if (curComStar != null)
  119. // {
  120. // if (curComStar != null)
  121. // {
  122. // await Task.Delay(1000);
  123. // RemoveListener();
  124. // ViewManager.Show(ViewName.LUCKY_BOX_CARD_VIEW, new object[] { _rewardList, 0 });
  125. // this.Hide();
  126. // }
  127. // }
  128. //}
  129. private void OnClickUIEnd()
  130. {
  131. Timers.inst.Add(0.3f, 1, ClickUIEnd);
  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. }