LuckyBoxStarView.cs 14 KB

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