LuckyBoxStarView.cs 13 KB

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