LuckyBoxStarView.cs 8.1 KB

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