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