LuckyBoxStarView.cs 10 KB

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