LuckyBoxStarView.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. ResetStartView();
  68. }
  69. private void ResetStartView()
  70. {
  71. showGuide = GuideDataManager.IsGuideFinish(ConstGuideId.LUCKY_BOX_LINE) <= 0;
  72. curComStar = null;
  73. comStars.Clear();
  74. int index = 0;
  75. GObject star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  76. while (star != null && star.visible == true)
  77. {
  78. UI_ComStar comStar = UI_ComStar.Proxy(star);
  79. comStar.m_imgLine.visible = true;
  80. comStar.m_c1.selectedIndex = 0;
  81. comStar.m_imgLine.width = imgLineWidth;
  82. comStar.m_imgLine.rotation = 0;
  83. GComponent gcom = CreateEffect(comStar, 1, "ui_ck"); ;
  84. gcom.visible = true;
  85. comStar.target.AddChildAt(gcom, 1);
  86. GComponent gcom1 = CreateEffect(comStar, 2, "ui_ck_dj");
  87. gcom1.visible = false;
  88. comStar.target.AddChildAt(gcom1, 2);
  89. GComponent gcom2 = CreateEffect(comStar, 3, "ui_ck_xs");
  90. gcom2.visible = false;
  91. comStar.target.AddChildAt(gcom2, 3);
  92. GComponent gcom3 = CreateEffect(comStar, 4, "ui_ck_dj_2");
  93. gcom3.visible = false;
  94. comStar.target.AddChildAt(gcom3, 4);
  95. star.data = new Vector2(comStar.target.x, comStar.target.y);
  96. comStars.Add(star);
  97. index++;
  98. star = _ui.target.GetChild(string.Format("comStar{0}_{1}_{2}", _ui.m_ctrlBuyType.selectedIndex, _ui.m_ctrlRewardsType.selectedIndex, index));
  99. }
  100. }
  101. private GComponent CreateEffect(UI_ComStar comStar, int index, string name)
  102. {
  103. GComponent gcom;
  104. if (comStar.target.numChildren > index)
  105. {
  106. gcom = comStar.target.GetChildAt(index).asCom;
  107. }
  108. else
  109. {
  110. gcom = UIPackage.CreateObject("LuckyBox", "ComHolder").asCom;
  111. string resPath = ResPathUtil.GetViewEffectPath("ui_ck", name);
  112. SceneController.AddObjectToView(null, null, gcom.GetChild("holder").asGraph, resPath, out GameObject gameObject, out GoWrapper wrapper);
  113. if (!dicGameobj.ContainsKey(index))
  114. {
  115. dicGameobj.Add(index, new List<GameObject>());
  116. }
  117. dicGameobj[index].Add(gameObject);
  118. }
  119. return gcom;
  120. }
  121. private void OnClickUIBegin(EventContext context)
  122. {
  123. context.CaptureTouch();
  124. InputEvent inputEvent = (InputEvent)context.data;
  125. Vector2 mousePos = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
  126. CheckNearbyPos(mousePos);
  127. }
  128. private void OnClickUIMove(EventContext context)
  129. {
  130. InputEvent inputEvent = (InputEvent)context.data;
  131. Vector2 mousePos = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
  132. CheckNearbyPos(mousePos);
  133. if (comStars.Count == 0)//所有星星都点亮了主动结束
  134. {
  135. this.OnClickUIEnd();
  136. }
  137. }
  138. //检测鼠标附近的星星
  139. private void CheckNearbyPos(Vector2 mousePos)
  140. {
  141. for (int i = comStars.Count - 1; i >= 0; i--)
  142. {
  143. Vector2 comStarPos = (Vector2)comStars[i].data;
  144. if (IsSamePos(mousePos, comStarPos))
  145. {
  146. if (curComStar != null)
  147. {
  148. SetCurComStarTransfrom(comStarPos);
  149. }
  150. UI_ComStar comStar = UI_ComStar.Proxy(comStars[i]);
  151. comStar.target.GetChildAt(2).asCom.visible = true;
  152. comStar.target.GetChildAt(1).asCom.visible = false;
  153. // comStar.m_c1.selectedIndex = 1;
  154. curComStar = comStars[i];
  155. lastPos = Vector2.right;
  156. clickComStars.Add(comStars[i]);
  157. comStars.RemoveAt(i);
  158. }
  159. else
  160. {
  161. if (curComStar != null)
  162. {
  163. Vector2 curPos = mousePos - (Vector2)curComStar.data;
  164. SetCurComStarTransfrom(mousePos);
  165. lastPos = curPos;
  166. }
  167. }
  168. }
  169. }
  170. private void SetCurComStarTransfrom(Vector2 targetPos)
  171. {
  172. Vector2 curPos = targetPos - (Vector2)curComStar.data;
  173. float angle = Vector3.Angle(lastPos, curPos); //求出两向量之间的夹角
  174. Vector3 normal = Vector3.Cross(lastPos, curPos);//叉乘求出法线向量
  175. angle *= Mathf.Sign(Vector3.Dot(normal, Vector3.forward)); //Mathf.Sign()求符号,Vector3.Dot()求方向,求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向
  176. UI_ComStar comStar = UI_ComStar.Proxy(curComStar);
  177. comStar.m_imgLine.rotation += angle;
  178. comStar.m_imgLine.width = Vector2.Distance(targetPos, (Vector2)curComStar.data);
  179. }
  180. private void OnClickUIEnd()
  181. {
  182. if (clickComStars.Count <= 0) return;
  183. UI_ComStar comStar = UI_ComStar.Proxy(clickComStars[clickComStars.Count - 1]);
  184. comStar.m_imgLine.visible = false;
  185. if (showGuide && clickComStars.Count < 2)
  186. {
  187. ResetStartView();
  188. }
  189. else
  190. {
  191. CheckNotClickComStar();
  192. SetComStarDarken();
  193. RemoveListener();
  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. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX_LINE);
  267. GuideController.TryCompleteGuide(ConstGuideId.LUCKY_BOX, 4);
  268. GuideController.TryGuide(null, ConstGuideId.LUCKY_BOX_LINE, 1, "点击将星星链接在一起", -1, true, (int)(this.viewCom.height - 150));
  269. TryCompleteGuide();
  270. }
  271. protected override void TryCompleteGuide()
  272. {
  273. if (clickComStars.Count >= 2)
  274. {
  275. GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.LUCKY_BOX_LINE);
  276. GuideController.TryCompleteGuideIndex(cfg.id, 1);
  277. GuideController.TryCompleteGuide(ConstGuideId.LUCKY_BOX_LINE, 1);
  278. }
  279. else
  280. {
  281. GuideDataManager.SetGuideIndexState(GuideDataManager.currentGuideId, GuideDataManager.currentGuideIdIndex, 0);
  282. GuideDataManager.currentGuideIdIndex = 3;
  283. }
  284. }
  285. }
  286. }