LuckyBoxStarView.cs 14 KB

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