LuckyBoxStarView.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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<GObject> clickComStars = new List<GObject>();
  16. private List<GObject> notClickComStars = new List<GObject>();
  17. private List<GameObject> _gameObjects = new List<GameObject>();
  18. private List<GoWrapper> _wrappers = new List<GoWrapper>();
  19. private List<GameObject> _gameObjects1 = new List<GameObject>();
  20. private List<GoWrapper> _wrappers1 = new List<GoWrapper>();
  21. private Dictionary<int, List<GameObject>> dicGameobj = new Dictionary<int, List<GameObject>>();
  22. private GObject curComStar;//当前选中的星星
  23. private Vector2 lastPos;//鼠标的上一个位置,每颗星星初始时默认为Vector2.right;
  24. private List<ItemData> _rewardList;
  25. private const int checkDistance = 40;//鼠标靠近星星周围加减40都算选中星星
  26. private const int imgLineWidth = 10;//线的原始长度
  27. private bool showGuide = false;
  28. public override void Dispose()
  29. {
  30. base.Dispose();
  31. }
  32. protected override void OnHide()
  33. {
  34. int index = 0;
  35. GObject star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  36. notClickComStars.Clear();
  37. clickComStars.Clear();
  38. dicGameobj.Clear();
  39. foreach (List<GameObject> value in dicGameobj.Values)
  40. {
  41. for (int i = 0; i < value.Count; i++)
  42. {
  43. SceneController.DestroyObjectFromView(value[i]);
  44. }
  45. }
  46. }
  47. protected override void OnInit()
  48. {
  49. base.OnInit();
  50. _ui = UI_LuckyBoxStarUI.Create();
  51. this.viewCom = _ui.target;
  52. isfullScreen = true;
  53. _ui.m_btnBack.visible = false;
  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. _ui.m_effEnd.visible = false;
  63. _rewardList = LuckyBoxDataManager.Instance.RewardList;
  64. _ui.m_ctrlBuyType.selectedIndex = _rewardList != null && _rewardList.Count > 1 ? 1 : 0;
  65. _ui.m_ctrlRewardsType.selectedIndex = LuckyBoxDataManager.Instance.currentBoxId - 1; Array.IndexOf(LuckyBoxDataManager.Instance.luckyBoxIds, LuckyBoxDataManager.Instance.currentBoxId);
  66. _ui.m_bg.url = ResPathUtil.GetBgImgPath("cj_tp_" + LuckyBoxDataManager.Instance.currentBoxId);
  67. TryCompleteGuide();
  68. ResetStartView();
  69. }
  70. private void ResetStartView()
  71. {
  72. showGuide = GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX_LINE) <= 0;
  73. curComStar = null;
  74. comStars.Clear();
  75. int index = 0;
  76. GObject star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  77. while (star != null && star.visible == true)
  78. {
  79. UI_ComStar comStar = UI_ComStar.Proxy(star);
  80. comStar.m_imgLine.visible = true;
  81. comStar.m_c1.selectedIndex = 0;
  82. comStar.m_imgLine.width = imgLineWidth;
  83. comStar.m_imgLine.rotation = 0;
  84. GComponent gcom = CreateEffect(comStar, 1, "ui_ck"); ;
  85. gcom.visible = true;
  86. comStar.target.AddChildAt(gcom, 1);
  87. GComponent gcom1 = CreateEffect(comStar, 2, "ui_ck_dj");
  88. gcom1.visible = false;
  89. comStar.target.AddChildAt(gcom1, 2);
  90. GComponent gcom2 = CreateEffect(comStar, 3, "ui_ck_xs");
  91. gcom2.visible = false;
  92. comStar.target.AddChildAt(gcom2, 3);
  93. GComponent gcom3 = CreateEffect(comStar, 4, "ui_ck_dj_2");
  94. gcom3.visible = false;
  95. comStar.target.AddChildAt(gcom3, 4);
  96. star.data = new Vector2(comStar.target.x, comStar.target.y);
  97. comStars.Add(star);
  98. index++;
  99. star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  100. }
  101. }
  102. private GComponent CreateEffect(UI_ComStar comStar, int index, string name)
  103. {
  104. GComponent gcom;
  105. if (comStar.target.numChildren > index)
  106. {
  107. gcom = comStar.target.GetChildAt(index).asCom;
  108. }
  109. else
  110. {
  111. gcom = UIPackage.CreateObject("LuckyBox", "ComHolder").asCom;
  112. string resPath = ResPathUtil.GetViewEffectPath("ui_ck", name);
  113. SceneController.AddObjectToView(null, null, gcom.GetChild("holder").asGraph, resPath, out GameObject gameObject, out GoWrapper wrapper);
  114. if (!dicGameobj.ContainsKey(index))
  115. {
  116. dicGameobj.Add(index, new List<GameObject>());
  117. }
  118. dicGameobj[index].Add(gameObject);
  119. }
  120. return gcom;
  121. }
  122. private void OnClickUIBegin(EventContext context)
  123. {
  124. context.CaptureTouch();
  125. InputEvent inputEvent = (InputEvent)context.data;
  126. Vector2 mousePos = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
  127. CheckNearbyPos(mousePos);
  128. }
  129. private void OnClickUIMove(EventContext context)
  130. {
  131. InputEvent inputEvent = (InputEvent)context.data;
  132. Vector2 mousePos = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
  133. CheckNearbyPos(mousePos);
  134. if (comStars.Count == 0)//所有星星都点亮了主动结束
  135. {
  136. this.OnClickUIEnd();
  137. }
  138. }
  139. //检测鼠标附近的星星
  140. private void CheckNearbyPos(Vector2 mousePos)
  141. {
  142. for (int i = comStars.Count - 1; i >= 0; i--)
  143. {
  144. Vector2 comStarPos = (Vector2)comStars[i].data;
  145. if (IsSamePos(mousePos, comStarPos))
  146. {
  147. if (curComStar != null)
  148. {
  149. SetCurComStarTransfrom(comStarPos);
  150. }
  151. UI_ComStar comStar = UI_ComStar.Proxy(comStars[i]);
  152. comStar.target.GetChildAt(2).asCom.visible = true;
  153. comStar.target.GetChildAt(1).asCom.visible = false;
  154. // comStar.m_c1.selectedIndex = 1;
  155. curComStar = comStars[i];
  156. lastPos = Vector2.right;
  157. clickComStars.Add(comStars[i]);
  158. comStars.RemoveAt(i);
  159. }
  160. else
  161. {
  162. if (curComStar != null)
  163. {
  164. Vector2 curPos = mousePos - (Vector2)curComStar.data;
  165. SetCurComStarTransfrom(mousePos);
  166. lastPos = curPos;
  167. }
  168. }
  169. }
  170. }
  171. private void SetCurComStarTransfrom(Vector2 targetPos)
  172. {
  173. Vector2 curPos = targetPos - (Vector2)curComStar.data;
  174. float angle = Vector3.Angle(lastPos, curPos); //求出两向量之间的夹角
  175. Vector3 normal = Vector3.Cross(lastPos, curPos);//叉乘求出法线向量
  176. angle *= Mathf.Sign(Vector3.Dot(normal, Vector3.forward)); //Mathf.Sign()求符号,Vector3.Dot()求方向,求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向
  177. UI_ComStar comStar = UI_ComStar.Proxy(curComStar);
  178. comStar.m_imgLine.rotation += angle;
  179. comStar.m_imgLine.width = Vector2.Distance(targetPos, (Vector2)curComStar.data);
  180. }
  181. private void OnClickUIEnd()
  182. {
  183. if (clickComStars.Count <= 0) return;
  184. UI_ComStar comStar = UI_ComStar.Proxy(clickComStars[clickComStars.Count - 1]);
  185. comStar.m_imgLine.visible = false;
  186. if (showGuide && clickComStars.Count < 2)
  187. {
  188. ResetStartView();
  189. }
  190. else
  191. {
  192. CheckNotClickComStar();
  193. SetComStarDarken();
  194. Timers.inst.Add(1f, 1, SetClickComStarAni);
  195. }
  196. }
  197. private void CheckNotClickComStar()
  198. {
  199. int index = 0;
  200. GObject star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  201. while (star != null && star.visible == true)
  202. {
  203. if (clickComStars.IndexOf(star) < 0)
  204. {
  205. notClickComStars.Add(star);
  206. }
  207. index++;
  208. star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  209. }
  210. }
  211. private void SetComStarDarken()
  212. {
  213. for (int i = 0; i < notClickComStars.Count; i++)
  214. {
  215. UI_ComStar notClickComStar = UI_ComStar.Proxy(notClickComStars[i]);
  216. notClickComStar.m_imgLine.visible = false;
  217. notClickComStar.target.GetChildAt(3).asCom.visible = true;
  218. notClickComStar.target.GetChildAt(1).asCom.visible = false;
  219. }
  220. }
  221. private void SetClickComStarAni(object param)
  222. {
  223. for (int i = 0; i < clickComStars.Count; i++)
  224. {
  225. UI_ComStar comStar = UI_ComStar.Proxy(clickComStars[i]);
  226. comStar.target.GetChildAt(4).asCom.visible = true;
  227. }
  228. Timers.inst.Add(0.5f, 1, SetEndEffect);
  229. }
  230. private void SetEndEffect(object param)
  231. {
  232. _ui.m_effEnd.visible = true;
  233. _ui.m_effEnd.SetPlaySettings(0, -1, 1, -1);
  234. Timers.inst.Add(0.6f, 1, ClickUIEnd);
  235. // ClickUIEnd(null);
  236. TryCompleteGuide();
  237. }
  238. private void ClickUIEnd(object param)
  239. {
  240. if (curComStar != null)
  241. {
  242. RemoveListener();
  243. ViewManager.Show(ViewName.LUCKY_BOX_CARD_VIEW, new object[] { _rewardList, FirstGetCardViewType.JUMP });
  244. this.Hide();
  245. }
  246. }
  247. private bool IsSamePos(Vector2 mousePos, Vector2 comStarPos)
  248. {
  249. return (mousePos.x < comStarPos.x + checkDistance) && (mousePos.x > comStarPos.x - checkDistance) && (mousePos.y < comStarPos.y + checkDistance) && (mousePos.y > comStarPos.y - checkDistance);
  250. }
  251. private void RemoveListener()
  252. {
  253. _ui.target.onTouchBegin.Remove(OnClickUIBegin);
  254. _ui.target.onTouchMove.Remove(OnClickUIMove);
  255. _ui.target.onTouchEnd.Remove(OnClickUIEnd);
  256. }
  257. private void OnClickBtnBack()
  258. {
  259. this.Hide();
  260. RemoveListener();
  261. ViewManager.Show(ViewName.LUCKY_BOX_VIEW);
  262. }
  263. protected override void UpdateToCheckGuide(object param)
  264. {
  265. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  266. GuideController.TryGuide(null, ConstGuideId.LUCKY_BOX_LINE, 1, "点击将星星链接在一起", -1, true, (int)(this.viewCom.height - 150));
  267. TryCompleteGuide();
  268. }
  269. protected override void TryCompleteGuide()
  270. {
  271. if (clickComStars.Count >= 2)
  272. {
  273. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX_LINE);
  274. GuideController.TryCompleteGuideIndex(cfg.id, 1);
  275. GuideController.TryCompleteGuide(ConstGuideId.LUCKY_BOX_LINE, 1);
  276. }
  277. else
  278. {
  279. GuideDataManager.SetGuideIndexState(GuideDataManager.currentGuideId, GuideDataManager.currentGuideIdIndex, 0);
  280. GuideDataManager.currentGuideIdIndex = 3;
  281. }
  282. GuideController.TryCompleteGuide(ConstGuideId.LUCKY_BOX, 4);
  283. }
  284. }
  285. }