LuckyBoxStarView.cs 8.3 KB

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