LuckyBoxStarView.cs 11 KB

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