LuckyBoxStarView.cs 13 KB

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